0% found this document useful (0 votes)
41 views2 pages

Inheritance in JavaBy Walter SavitchDate

This document discusses inheritance in Java and provides sample questions about inheritance concepts like overriding and overloading methods, accessing variables and methods from parent and child classes, constructors, and preventing overriding. The questions cover topics like why derived classes don't redeclare parent variables/methods, conditions for method overriding, differences between overriding and overloading, making classes or methods final to prevent overriding, accessing private members of parent classes, implicit calls to parent constructors, and valid/invalid class relationships and method calls when using inheritance.

Uploaded by

aelemayo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Inheritance in JavaBy Walter SavitchDate

This document discusses inheritance in Java and provides sample questions about inheritance concepts like overriding and overloading methods, accessing variables and methods from parent and child classes, constructors, and preventing overriding. The questions cover topics like why derived classes don't redeclare parent variables/methods, conditions for method overriding, differences between overriding and overloading, making classes or methods final to prevent overriding, accessing private members of parent classes, implicit calls to parent constructors, and valid/invalid class relationships and method calls when using inheritance.

Uploaded by

aelemayo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Inheritance in Java

By Walter Savitch
Date: Apr 19, 2002
Sample Chapter is provided courtesy of Prentice Hall.

Reading Questions
1. public class Student extends Person (5/32)
Why not mention all the instance variables and methods of the perosn class when you define the derived
student class?
2. Under what conditions does a child class method override a parent class method? Given an example from the
student class. (6/32)

3. How do method overriding and method overloading differ? (6/32)

4. Can you prevent a method from being overridden? (7/32)

5. Can a class be declared final? (7/32)

6. Why is the following definition of the reset() method in the student class
illegal ?
public void reset(String newName, int newID)
{
name = newName;
studentNumber = newID;
}

7. How can a private instance variable in a base class be accessed in a derived class? How can a private method in
a base class be accessed in a derived class? (8/32)

8. What is the first action of a derived class constructor? (8/32)

9. How does java react if there is no call to a base class constructor in the derived class? (9/32)

10 How does the writeOutput() method of the student classs compare to


the following? Which is correct? (10/32)
public void writeOutput()
{
[Link]();
[Link](Student number: + studentNumber);
}

11. How does the subclass underGraduate relate to the Person class? (Is the following class header correct?)
public class underGraduate extends Person (10/32)

12. What special description applies to the reset() method in the underGraduate class? (12/32)

13. The WriteOutput() method of the Undergraduate class illustrates method overrriding w by the Undergraduate
class when compared to the WriteOutput() method of the student class. Since the WriteOutput() method of
the student class is used by the Undergraduate class, what code modification corrects the conflict? (12/32)

14. Legal/Illegal: [Link]()

(13/32)

15. Valid/ invalid? Why?


[Link](UndergradObject, studentObject)
where
UndergradObject undergraduateObject = new Undergraduate(); and,
Student studentObject = new Student();

(14/32)

16. Given the definition Person p1, p2, are the following statements legal or illegal?
p1 = new Student();_____
P2 = new Undergraduate(); _____
Student s = new Person(); ______
Undergraduate ug = new Person(); _____
Undergraduate ugl = new Student(); _____
17. Every class inherits from the object class. Since toString() and equals() are members of the object class, every
object inherits these methods. But in most classes neither method will work correctly. Should these emthods
be overriden or overloaded? Will the change you make be implemented at compile time or run time? (16/32)

You might also like