Chapter 9 Flashcards
What does inheritance mean?
Inheritance is the act of deriving a new class from an existing one.
What’s the difference between parent and child class?
The parent class inherits the methods and data of the parent class, except the costructor.
What’s the benifit of using inheritance?
Inheritance allows the reuse of already prexisting code. This doesn’t ease only the coding itself, but also the testing and the structure of the software.
What does the protected modifier do?
The protected modifier allows the child class access instance data and methods of the parent class, but it allows these to remain encapsulated. The protected modifier allows access even for every class in the same package as the class it's used in.
What does the super reference do?
The super reference allows the child class to use the parent class’ constructor.
Example: parent: public Book (int numPages) { ... }
... child: public Dictionary (int numPages, int numDefinitions) { super(numPages); ... }
Does the constructor of the child need always to call the constructor of the parent?
The constructor of a sublacc always calls the constructor of the superclass.
In case the superclass doesn’t have a redefined constructor, the default constructor (with no arguments) will be called.
What is multiple inheritance? How is it used in Java?
Multiple inheritance consists of a child class being derived from two or more parent classes.
Java doesn’t implement multiple inheritance, and it’s in general not even needed.
What does overriding mean?
What determines which method is called?
Overriding consists in dealing with a method of the parent and the child class that has the same name and signature.
The type of object executing the method determines which version of the method is invoked.
What is the concept of shadowing variables?
It consists in defining variables inside the child class with the same name.
Such practice should be avoided, as it generates confusion.
What is the purpose of creating a class hiearchy?
A class hiearchy works is put on the purpose to structure parents, their childs and the child of their own.
Common features should be put as high in the hiearchy possible.
What is the uniquity of the Object class?
The Object class has no parent. In fact, every other class is a child of this class.
What methods of all existing classes are inherited from the Object class?
toString(): returns the name of the object’s class and a hash code.
equals(): returns true if two references are alieses.
clone(): clones a reference.
What is an abstract class? What is its behaviour?
An abstract class is a placeholder in a class hierarchy that represents a generic concept.
An abstract class cannot be instantiated.
An abstract class is declared using the abstract modifier.
What happens to the child of an abstract class?
The child of an abstract class must override the abstract methods of the parent.
Can an abstract class contain instance data?
Although an abstract class cannot be initiated, it can contain instance data. This data will be instantiated when any non-abstract child class will be initiated.