Java Inheritance

inheritance in java

Java Inheritance is the process that enables one class to acquire the properties (methods and variables) of another. With inheritance, the information is placed in a more manageable, hierarchical order.

The class inheriting the properties of another is the subclass (also called derived class, or child class); the class whose properties are inherited is the superclass (base class, or parent class).

Inheritance represents the IS-A relationship which is also known as a <em>parent-child</em> relationship.


The syntax of Java Inheritance

class Subclass-name extends Superclass-name
{
   //methods and fields
}

The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of “extends” is to increase the functionality.

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.

Example:

class Employee{
 float salary=40000;
}
class Programmer extends Employee{
 int bonus=10000;
 public static void main(String args[]){
   Programmer p=new Programmer();
   System.out.println(“Programmer salary is:”+p.salary);
   System.out.println(“Bonus of Programmer is:”+p.bonus);
}
}

Constructors are not member methods, and so are not inherited by subclasses.

However, the constructor of the superclass is called when the subclass is instantiated.


Types of inheritance in java:

On the basis of class, there can be three types of inheritance in java:

i. single
ii. multilevel
iii. hierarchical


Single Inheritance Example:

When a class inherits another class, it is known as a <em>single inheritance</em>. In the example given below, Dog class inherits the Animal class, so there is the single inheritance.

Example:

class Animal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
void bark(){System.out.println(“barking…”);}
}
class TestInheritance{
public static void main(String args[]){
Dog d=new Dog();
d.bark();
d.eat();
}}

Multilevel Inheritance Example:

When there is a chain of inheritance, it is known as <em>multilevel inheritance</em>. As you can see in the example given below, BabyDog class inherits the Dog class which again inherits the Animal class, so there is a multilevel inheritance.

Example:

class Animal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
void bark(){System.out.println(“barking…”);}
}
class BabyDog extends Dog{
void weep(){System.out.println(“weeping…”);}
}
class TestInheritance2{
public static void main(String args[]){
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}

Hierarchical Inheritance :

When two or more classes inherits a single class, it is known as <em>hierarchical inheritance</em>. In the example given below, Dog and Cat classes inherits the Animal class, so there is hierarchical inheritance.

Example:

class Animal{
void eat(){System.out.println(“eating…”);}
}
class Dog extends Animal{
void bark(){System.out.println(“barking…”);}
}
class Cat extends Animal{
void meow(){System.out.println(“meowing…”);}
}
class TestInheritance3{
public static void main(String args[]){
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//C.T.Error
}}

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *