Unit 4 JAVA inheritance Flashcards
How to inherit in java?
using keyword extends.
class B extends A{}
Java Inheritance:
A subclass can have only one superclass.
No class can be superclass of itself.
Member Access and Inheritance
Although a subclass includes all of the members of its superclass, it cannot access those members of the superclass that have been declared as private.
super
always refers to superclass.
supper.member
also, helps incase of same name causing hiding
Multilevel inheritance
super()
super() always refers to the constructor in the closest superclass.
Order of constructors in the class hierarchy:
make sense of it, reason it?
The answer is that in a class hierarchy, constructors complete their execution in order of derivation, from superclass to subclass.
A super to B super to C
A’s constructor first then B’s then C’
If you think about it, it makes sense that constructors
complete their execution in order of derivation. Because a
superclass has no knowledge of any subclass, any
initialization it needs to perform is separate from and
possibly prerequisite to any initialization performed by the
subclass. Therefore, it must complete its execution first.
Method overriding:
when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass.
When an overridden method is called from within its subclass, it will always refer to the version of that method defined by the subclass. The version of the method defined by the superclass will be hidden.
Will it override or overload?
Method overriding occurs only when the names and the
type signatures of the two methods are identical. If they are
not, then the two methods are simply overloaded.
Dynamic Method dispatch:
why imp?
Dynamic method dispatch is the
mechanism by which a call to an overridden method is
resolved at run time, rather than compile time. Dynamic
method dispatch is important because this is how Java
implements run-time polymorphism.
Abstract Class:
need
superclass that declares the structure of a given abstraction
without providing a complete implementation of every
method.
a
superclass that only defines a generalized form that will be
shared by all of its subclasses, leaving it to each subclass to
fill in the details