Inheritance in JAVA
Inheritance is a mechanism wherein a new class is derived from an existing class. In Java,
classes may inherit or acquire the properties and methods of other classes.
A class derived from another class is called a subclass, whereas the class from which a subclass is
derived is called a superclass. A subclass can have only one superclass, whereas a superclass may
have one or more subclasses.
Inheritance is the process wherein characteristics are inherited from ancestors. Similarly, in
Java, a subclass inherits the characteristics (properties and methods) of its superclass (ancestor).
For example, a vehicle is a superclass and a car is a subclass. The car (subclass) inherits all of
the vehicle’s properties. The inheritance mechanism is very useful in code reuse.
The following are some limitations of Java class inheritance:
1. A subclass cannot inherit private members of its superclass.
2. Constructor and initializer blocks cannot be inherited by a subclass.
3. A subclass can have only one superclass.
The keyword “extends” is used to derive a subclass from the superclass, as illustrated by the
following syntax:
Class Name_of_subclass extends Name_of superclass { //new fields and methods that
would define the subclass go here }
If you want to derive a subclass Rectangle from a superclass Shapes, you can do it as follows:
class Rectangle extends Shapes { …. }
Program 1: - Inheritance Simple Example
1. Parent class is the super class of child class.
2. Child class is the sub-class of Parent class.
3. So child class extends the methods of Parent class.
public class Parent
{
public void P1()
{
System.out.println("This is parent class output");
}
}
public class Child extends Parent
{
public void C1()
{
System.out.println("This is child class output");
}
public static void main(String[] args)
{
Child CObj = new Child();
CObj.P1();
CObj.C1();
}
}
Result:-
This is parent class output
This is child class output
Program 2: - Inheritance Simple Example
Animal is the superclass of Bird class.
Animal is the superclass of Dog class.
Bird and Dog are subclasses of Animal class.
public class Animal
{
public void animalsleep() {
System.out.println("An animal sleeps...");
}
public void animaleat() {
System.out.println("An animal eats...");
}
}
public class Bird extends Animal
{
public void birdsleep() {
System.out.println("A bird sleeps...");
}
public void birdeat() {
System.out.println("A bird eats...");
}
}
public class Dog extends Animal
{
public void dogsleep() {
System.out.println("A dog sleeps...");
}
public void dogeat() {
System.out.println("A dog eats...");
}
}
public class MainClass
{
public static void main(String[] args)
{
Animal animal = new Animal();
Bird bird = new Bird();
Dog dog = new Dog();
System.out.println();
animal.animalsleep();
animal.animaleat();
bird.birdsleep();
bird.birdeat();
dog.dogsleep();
dog.dogeat();
}
}
Result:-
An animal sleeps...
An animal eats...
A bird sleeps...
A bird eats...
A dog sleeps...
A dog eats...
Program 3: - Inheritance Simple Example
public class InheritanceParent
{
public String abc=null;
public String xyz=null;
public void parentMethod()
{
System.out.println("Output of Inheritance :"+abc);
System.out.println("Output of Second Inheritance :"+xyz);
}
}
public class InheritanceChild extends InheritanceParent
{
public void childMethod()
{
abc="This is inheritance java program";
xyz="This is child class where extends used";
System.out.println("This is child method");
parentMethod();
}
public static void main(String[] args)
{
InheritanceChild ic=new InheritanceChild();
ic.childMethod();
}
}
Result:-
This is child method
Output of Inheritance :This is inheritance java program
Output of Second Inheritance :This is child class where extends used