Inheritance is the approach of sharing instance variables and methods of a class with other class(es), which means inheriting features and behaviors of a class to other class(es). Many times, multiple objects may have some common characteristics. For example, Car A shares its characteristics (Registration Number, Current Speed, Current Gear, Number of Cylinders) with Car B and Car C. And both Car B and Car C have some additional features: Car B has a Rear Camera and Car C has Front Fog Lights, these additional features make Car A, Car B and Car C different from each other. In this case, Car A becomes the Superclass of Car B and Car C, and Car B and Car C are the Subclasses of Car A.
In Java programming, A class can have one directional Superclass and each Superclass might have an unlimited number of Subclasses. Each subclass is a specialized version of its superclass.
Inheritance takes place when the definition of a class is integrated into another class;to do so in Java, you need to use the extends keyword. For example:
Code
public class Inheritance {
public static void main(String[] args) {
Math2 obj = new Math2();
obj.Add(10, 20);
obj.Subtract(80, 70);
}
}
class Math1 {
int result;
public void Add(int a, int b) { // Add two integers
result = a + b;
System.out.println(result);
}
}
class Math2 extends Math1 {
public void Subtract(int a, int b) { //Subtract a small integer from big one
if (a >= b) {
result= a - b;
} else {
result= b - a;
}
System.out.println(result);
}
}
Output
In above example, Math2 extends Math1. Therefore, variable "result" is used in Math2 without declaring it in Math2. And the Add() method of Math1 is called using the object of class Math2. It happens because the definition of Math1 is integrated into Math2 by using the extends keyword.
Types of Inheritance
Single Level
In a single level of inheritance, one class extends one class only. The above written program is a good example of a single level inheritance.
Multilevel
Multilevel Inheritance takes place when a class extends a subclass.
There could be 'n' number of levels in Multilevel Inheritance. Therefore, the lower most class has definition of all above classes. The below written program demonstrates Multilevel Inheritance.
Code
public class MultilevelInheritance {
public static void main(String[] args) {
C obj = new C();
obj.method1();
obj.method2();
obj.method3();
}
}
class A {
public void method1() {
System.out.println("You are in method1");
}
}
class B extends A {
public void method2() {
System.out.println("You are in method2");
}
}
class C extends B {
public void method3() {
System.out.println("You are in method3");
}
}
Output
You are in method1
You are in method2
You are in method3
Hierarchical
When two or more classes are derived from a super class, is called Hierarchical Inheritance.
Code
public class HierarchicalInheritance {
public static void main(String[] args) {
B obj = new B();
obj.method1();
obj.method2();
C obj1 = new C();
obj1.method1();
obj1.method3();
}
}
class A {
public void method1() {
System.out.println("You are in method1");
}
}
class B extends A {
public void method2() {
System.out.println("You are in method2");
}
}
class C extends A {
public void method3() {
System.out.println("You are in method3");
}
}
Output
You are in method1
You are in method2
You are in method1
You are in method3
Multiple (Not Supported by Java)
In Multiple Inheritance, a subclass has many superclasses, which means a class extends more than one class.
Note: Multiple Inheritance is partially achieved by using Interface.
Interface
An interface is a container of abstract methods. It allows Java to implement Multiple Inheritance, because a class can't have more than one superclass in Java, but can implements many interfaces. Methods are just declared in interface, but not defined. The class which implements an interface must have to define the method declared in the interface. Access modifiers and return type must be same as declared in the interface. Private and static methods can't be declared in the interface.
Code
interface Intr {
public void IntrMethod();
public void IntrMethod1();
}
interface Intr1 {
public void Intr1Method();
}
interface Intr2 {
public void Intr2Method();
}
public class Interface {
public static void main(String[] args) {
A obj = new A();
obj.IntrMethod();
obj.IntrMethod1();
obj.Intr1Method();
obj.Intr2Method();
}
}
class A implements Intr, Intr1, Intr2 {
public void IntrMethod() {
System.out.println("IntrMethod");
}
public void IntrMethod1() {
System.out.println("IntrMethod1");
}
public void Intr1Method() {
System.out.println("Intr1Method");
}
public void Intr2Method() {
System.out.println("Intr2Method");
}
}
Output
IntrMethod
IntrMethod1
Intr1Method
Intr2Method
How Access Specifiers effect Inheritance
There are four Access Specifiers in Java
1. Public: When a member of a class is modified by public specifier, it can be accessed by any other code.
2. Protected: Protected is only applicable in case of Inheritance. When a member of a class is modified by protected, it can only be accessed by the members of its class or subclass.
3. Private: A member of a class which is modified by private specifier, can only be accessed by the member of its class.
4. Default: When you don't specify a access specifier to a member, Java automatically specifies a default. And the members modified by default can only be accessed by any other code in the package, but can't be accessed outside of a package.
Restricting Inheritance
I hope you have learned how to implement various types of inheritance in Java. Now, what to do if you want that no other classes can extends a desired one, which means restricting inheritance.
You just need to use the "final" keyword as follows:
final class myclass{
// Insert code here
}
Now, "myclass" can't be extended by any other class.