Java Class Inheritance
Inheritance
Inheritance is one of the important concepts in object-oriented programming that creates hierarchical classification. By gathering only the common elements that multiple classes should have, it provides module reusability and code conciseness by allowing a general class to be created.
The inherited upper class is called a super class or parent class, and the lower class that receives the inheritance is called a subclass or child class.
A new class can be created by inheriting all attributes and methods from an existing parent class and adding any additional attributes and methods that are needed.
Every class in Java has a parent class. The top-level class among the classes available in Java programs is java.lang.Object. In other words, every class in a Java program is a subclass of the Object class. If a parent class is not explicitly specified, it is implicitly considered to inherit from Object.
To make a class inherit in Java, write extends after the new class name and then write the class to inherit from.
[public/final/abstract] class ClassName extends ParentClassName {
// Member variable declarations
// Constructor
// Method declarations
}
Example 1
package com.devkuma.tutorial.inheritance;
public class Parent {
int a;
int b;
public int sum() {
return a + b;
}
}
package com.devkuma.tutorial.inheritance;
public class Child extends Parent {
public int multiply() {
return a * b;
}
}
package com.devkuma.tutorial.inheritance;
public class Inheritance {
public static void main(String[] args) {
Child child = new Child();
System.out.println("sum=" + child.sum());
System.out.println("multiply=" + child.multiply());
}
}
Execution result:
sum=5
multiply=6
Looking at the example, the Child class inherits the Parent class, and Inheritance creates a Child object and displays the results of the sum and multiply methods. Since the Child class inherited the member variables a and b and the sum method from Parent, it could call the sum method, and the multiply method could use the member variables a and b from the Parent class.
super Keyword
super is a keyword used by a subclass to access members of a parent class in an inheritance relationship.
- It is used to access member variables or methods of the parent class that are hidden by the subclass.
- It is used to call the constructor of the parent class.
super.objectVariable
super.method(parameters)
Method Overriding
In an inheritance relationship, when a method in a subclass is declared with the same name as a method in the parent class, the subclass method is said to override the parent class method.
For overriding, the method in the parent class and the method in the subclass must have the same method name, parameter types and count, and return type. If the method name matches but the number or types of parameters differ, it becomes method overloading because it is still in an inheritance relationship.
If a method is overridden in classes with an inheritance relationship, the parent class method is hidden by the subclass. To use the overridden parent class method from an object of the subclass, use the super keyword.