Inheritance is an important concept of OOP(Object-Oriented Programming). It is the mechanism by which one class is allowed to inherit the features(fields and methods) of another class.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
Inheritance is used for code reusability.
The keyword used for inheritance is extends. The extends keyword indicates that you are making a new class that derives from an existing class.
syntax:
class Subclass-name extends Superclass-name
{
//methods and fields
}
In the terminology of Java, a class which is inherited is called a parent or superclass, and the new class is called child or subclass.
Types of Inheritance in Java
On the basis of class, there can be three types of inheritance in java:
single, multilevel and hierarchical.
1. Single Inheritance: In single inheritance, subclasses inherit the features of one superclass.
In the image below , class A serves as a base class for the derived class B.
// Java program to illustrate the concept of single inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
class one {
public void print_one()
{
System.out.println("one");
}
}
class two extends one {
public void print_two() { System.out.println("two"); }
}
// Driver class
public class Main {
public static void main(String[] args)
{
two g = new two();
g.print_one();
g.print_two();
}
}
2. Multilevel Inheritance: In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C.
// Java program to illustrate the concept of Multilevel inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
class one {
public void print_one()
{
System.out.println("one");
}
}
class two extends one {
public void print_two()
{ System.out.println("two"); }
}
class three extends two {
public void print_three()
{
System.out.println("three");
}
}
public class Main {
public static void
main(String[] args)
{
three g =
new three();
g.print_one();
g.print_two();
g.print_three();
}
}
3. Hierarchical Inheritance: In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived class B, C and D.
// Java program to illustrate the concept of Hierarchical inheritance
class A {
public void print_A() { System.out.println("Class A"); }
}
class B extends A {
public void print_B() { System.out.println("Class B"); }
}
class C extends A {
public void print_C() { System.out.println("Class C"); }
}
public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();
C obj_C = new C();
obj_C.print_A();
obj_C.print_C();
}
}
Multiple inheritance
To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes. In java programming, multiple and hybrid inheritance is supported through interface
only.
// Java program to illustrate the concept of Multiple inheritance
import java.io.*;
import java.lang.*;
import java.util.*;
interface one {
public void print_g();
}
interface two {
public void print_for();
}
interface three extends one, two {
public void print_g();
}
class child implements three {
public void print_g()
{
System.out.println("multiple");
}
public void print_in() { System.out.println("inheritance"); }
}
public class Main {
public static void main(String[] args)
{
child c = new child();
c.print_g();
c.print_in();
}
}
0 comments:
Post a Comment