inheritance
can reuse the fields and methods of the existing class
can inherits all the members (fields, methods, and nested classes)
they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass
superclasses and subclasses
private keyword
- be accessed only through the public or protected methods inherited from the superclass
- when the subclass attempts to access the superclass’s private instance variables, compile error happend protected keyword
- be accessed only through the public or protected methods inherited from the superclass
Rules
| Rules of Inheritance | Permission |
|---|---|
| Multiple Inheritance | X |
| Cyclic Inheritance | X |
| Private Members | X |
| Constructors | X |
constructors
super() must be the first statement in the constructor’s body of subclass
super(): keyword which call super class’s construct
If the code does not include an explicit call to the superclass constructor, Java implicitly calls the superclass’s default or no-argument constructor.
Object
has no superclass, in other words, is the top most class is a superclass of every class
ref:
COMP229 - Centennial College