Inheritance Flashcards
What is inheritance?
A mechanism for deriving a new class from an existing one.
What is the motivation behind inheritance?
To reuse existing classes, rather than writing new ones.
What is the subclass or child or derived class?
The derived new class from a superclass. Who inherits the attributes and methods of the superclass.
It extends the superclass.
What’s the difference between public, private and protected visibilities?
Public: children classes can access them directly (except the constructor)
Private: children classes cannot access them directly.
Protected: only accessible by any class in the same package or by any subclass.
What’s the purpose of ‘super’?
It’s a reserved word used in a derived class to refer to its parent class.
Allows you to access those members of the parent class that are not inherited.
What should the first line of a child’s constructor be?
super(…);
Can a method defined with the ‘final’ modifier be overridden?
No.
Does the type of an object change?
No, it is solely determined when it is created.
What is Polymorphism?
The principle that behaviour of a method can vary, depending on the type of the object being referenced.
What is casting?
The process of making a variable behave as a variable of a another type.
When is casting allowed?
If a class shares an IS-A or inheritance relationship with another class or interface.