Java - Inheritance, Constructor Chaining
Inheritance
- Use to avoid redundancy when Classes have many common features
- Idea is to develop a subclass from a superclass through inheritance.
Java does not support multiple inheritance, that’s why can’t extend a class from two different classes at the same time.
Keyword - extends
To declare a subclass, we use extends
keyword.
- A subclass extends properties and methods from the superclass.
- Then you can add new properties, methods to subclass
- Or even override the methods of the superclass (see
super
keyword)
Lets say an example A (subclass) -> B (superclass)
Now class A :
- have all the properties, methods of B
- can declare new properties, methods which B doesn’t have
1 | public class A extends B { |
The Object Class and Its Methods
- Every class in Java is descended from the
java.lang.Object
class. - Class
Object
is the root of the class hierarchy - If no inheritance is specified when a class is defined, the superclass of the class is Object.
- you don’t have to specify in code.
class C{}
is equivalent toclass C extends Object{}
.
- you don’t have to specify in code.
Keyword - super
The keyword super
refers to the superclass of the class in which super
appears.
keyword super
can be used in two ways:
- To call a superclass constructor
- To call a superclass method
Superclass’s Constructor Is Always Invoked
- A constructor may invoke an overloaded constructor or its superclass’s constructor.
- If none of them is invoked explicitly, the compiler puts super() as the first statement in the constructor.
Lets say an example A (subclass) -> B (superclass)
1 | public A(){ |
is equivalent to
1 | public A(){ |
Calling Superclass Methods
- A subclass inherits methods from a superclass.
- static method can be inherited
Lets say an example A (subclass) -> B (superclass)
B
has a method calledsaySomething()
A
can call B’s method without declaring in A
1 | public class A extends B { |
Overriding Superclass Methods
- A subclass inherits methods from a superclass.
- Sometimes it is necessary for the subclass to modify the implementation of a method defined in the superclass.
- An instance method can be overridden only if it is accessible.
- a private method cannot be overridden, because it is not accessible outside its own class
- If a method defined in a subclass is private in its superclass, the two methods are completely unrelated
- a static method can be inherited. However, a static method cannot be overridden.
Lets say an example A (subclass) -> B (superclass)
B
has a public method calledsaySomething()
A
can have own version of B’s method by declaring the same method name (also same parameter) in A
1 | public class A extends B { |
Constructor Chaining
- Constructing an instance of a class invokes all the superclasses’ constructors along the inheritance chain.
Example : Faculty(subclass of Employee) -> Employee (subclass of Person) -> Person (superclass)
1 | public class Faculty extends Employee { |
Output:
1 | (1) Person's no-arg constructor is invoked |
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment