Refer to book chapter 8 which of the following lines are leg

Refer to book chapter 8, which of the following lines are legal ? The class undergraduate is a derived class of student, and student is derived class of Person. Does a derived class (subclass, children class) inherit any constructors from the hate class (super class, parent class)? When you design a derived class which extends from a super class, you want to use the super class constructor to construct a constructor of the derived class, what keyword you should use? Fill out the blank for the following example: Could we call interface and abstract class abstract type because both are dealing with future implementation of empty methods.

Solution

39 Sol:

30.1) Person p = new Student(); // ANS: Legal

\"Person\" is a base class where as \"Student\" is a derived class.Unless anything is specified, object p will access the properties of Student class which has overriden the properties of its own base class Person.

30.2) Person p2 = new UnderGraduate(); //ANS: Legal

Because an UnderGraduate is a Person

30.3) Student s1 = new Person(); //ANS: Illegal

30.4) Student s2 = new UnderGraduate(); //ANS:Legal

Because an undergraduate is a Student

30.5) UnderGraduate ug1 = new Person(); //ANS: Illegal

30.6) UnderGraduate ug2 = new Person(); //ANS: Illegal

30.7) Object obj = new Student(); //ANS: Legal

Because a student is an Object

30.8) Student s3 = new Object(); //ANS: Illegal

40) Does a derived class inherit any construction from the base class `

ANS: No,Constructors are different from other class methods in that they create new objects, whereas other methods are invoked by existing objects. This is one reason constructors aren’t inherited. Inheritance means a derived object can use a base-class method, but, in the case of constructors, the object doesn’t exist until after the constructor has done its work.

41) Design a derived class which extends from a super class, use the super class constructor to construct a constructor of the derived class, what keyword you should use?

public Student(String intialName, int intialStudentNumber) //constructor of the Student class

{ super(intialName)

studentNumber = intialStudentNumber:

}

ANS: Super Keyword

If we want to call parameterized contructor of base class, then we can call it using super(). The point to note is base class comstructor call must be the first line in derived class constructor.

42) No, Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behaviour.

 Refer to book chapter 8, which of the following lines are legal ? The class undergraduate is a derived class of student, and student is derived class of Person

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site