Lecture 7A Flashcards
What does transitive mean?
In Java inheritance is transitive. If class X extends class Y and class Y extends class Z, then class X also extends class Z. More: Class X is derived and inherits from class Y. Class Y generalizes class X.
A subclass inherits all fields and methods from its superclass except…
Constructors and private fields.
Why only use instanceof the odd time?
It doesn’t confirm to the principles of static typing.
When can a method not be overridden?
With a method with more restrictive accessibility. Also can’t override private method. Also, a static method cannot be overridden. If a subtype does define a static method with same signature as superclass then both coexist. Which method is called depends on compile-time type of the object.
How to forbid overriding of method?
Use Keyword final.
How to invoke overriden method?
Use keyword super.
When is overriden resolved?
At runtime using a mechanism called dynamic dispatch.
Can you declare a field in subclass with same name as field in superclass?
Yes. But it is not overriding. Both co-exist in subclass instance but the field declared by the subclass shadows/hides the superclass field. Super.fieldname to access it again.
If an explicit super class constructor call is missing what happens?
Java inserts a call super() at the beginning of your subclass constructor, but only if there is a zero parameter constructor
What is constructor chaining?
The superclass highest in the hierarchy gets processed first, then the next and so on.
What is inheritance hierarchy?
Multiple classes related by inheritance, ie an is-a relationship