Inheritance in Java

Inheritance in Java

Inheritance is the one of the main feature of OOP (Object Oriented Programming). In Inhertiance one class aquires(inherits) the properties and behaviours(methods) of the other class.

The idea behind the inheritance is to provide reusability of code, so for the inherited class, only new methods and new properties can be written, because the inherited new class can reuse the new methods and fields of the old class. this reduces time and number of lines in the program

Types of classes in Inheritance

  • Class
  • Sub Class/ Child Class / Derived Class
  • Super Class/ Parent Class/ Base class

Class: Class is a block of code or template which is used to create objects,class dosent occupy memory when it is created

Sub Class: The class which is inherited (extended) from the another class, this sub-class has all methods and fields of the another class

Super class: The class where sub class gets inherited ,all the methods and fields of the superclass

Syntax for Inheritance in Java

To use the inheritance feature in java, you should use extends keyword to extend functionally and create a new chld class from the parent class

syntax

class cclass extends pclass
{
}

in the syntax cclass is a child class or new class,where its functionalities are extended from the pclass (parent class)

Example

class boy {
String name;
char gender;
void show(){
System.out.println("name ="+name);
System.out.println("gender ="+gender);
}
}
class employee extends boy{
int empno;
void display(){
System.out.println("name ="+name);
System.out.println("gender ="+gender);
System.out.println("empno ="+empno);
}
}
class main{
public static void main (String args[]){
boy b1=new boy();
b1.name="Alex";
b1.gender='M';
b1.show();
employee e1= new employee();
e1.name="JEssie";
e1.gender='F';
e1.empno=1234;
e1.display();
}
}

Output

name =Alex
gender =M
name =JEssie
gender =F
empno =1234

In The above example, boy class has two fields name and gender, a new class called employee is inherited from the boy class, in the inherited employee class there is no need to redeclare the fields for name and gender because parent class(boy class) already has it, now empno field is created only for the use of the employee class

Types of inheritance

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Hierarchical inheritance
  4. Hybrid Inheritance
  5. Multiple Inheritance

Single Inheritance

When one class inherits the another class it is called single inheritance. check the diagaram below class B inherits from class A

Single Inheritance in Java

Example

class A
{
public void methodA()
{
System.out.println("Base class method");
}
}
class B extends A
{
public void methodB()
{
System.out.println("Child class method");
}
}
class main{
public static void main(String args[])
{
B obj = new B();
obj.methodA(); //calling super class method
obj.methodB(); //calling local method
}
}

Output

Base class method
Child class method

Multilevel Inheritance

In Multilevel Inheritance, a new class will be derived from the child class, this new class has methods and fields of the child class and its parent class. lets look into the example for better understating

Multiple Inheritance in Java

Example

class A
{
public void methodA()
{
System.out.println("Class A method");
}
}
class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
class C extends B
{
public void methodC()
{
System.out.println("class C method");
}
}
class main2{
public static void main(String args[])
{
C obj = new C();
obj.methodA(); //calling grand parent class method
obj.methodB(); //calling parent class method
obj.methodC(); //calling local method
}
}

Output

Class A method
class B method
class C method

Hierarchical Inheritance

Hierarchical Inheritance in Java

In Hierarchical Inheritance two or more classes are inherited from one class, or if a parent class has more than 2 child classes then it is called Hierarchical Inheritance

class school
{
public void methodS()
{
System.out.println("im going to school");
}
}
class boy extends school
{
public void methodB()
{
System.out.println("Im a boy");
}
}
class girl extends school
{
public void methodG()
{
System.out.println("im a girl");
}
}
class main2{
public static void main(String args[]){
boy b=new boy();
girl g=new girl();
b.methodS();
b.methodB();
// b.methodG(); this gives error because boy is inherited from school class,it dosent contain methodG
g.methodS();
g.methodG();
// g.methodB(); this gives error because girl is inherited from school class,it dosent contain methodB
}
}

Output

im going to school
Im a boy
im going to school
im a girl

Hybrid Inheritance

Hybrid Inheritance is a combination of the above three inheritance types , Single Inheritance, Multilevel Inheritance, Hierarchical inheritance. using interfaces too you can achieve hybrid inheritance.

Hybrid Inheritance in Java

Multiple Inheritance

multiple inheritance is not possible in java because of ambiguity, If the two-parent classes have same method name, the compiler can decide which method to be inherited to the child class, so since compile-time errors are safer then run time, java doesn’t allow you to inherit from two-parent classes to a child class, check the example for better understanding

Multiple Inheritance in Java

Example (the below program throws an error)

class A
{
void methodA()
{
System.out.println("Class A method");
}
}
class B
{
void methodB()
{
System.out.println("class B method");
}
}
class C extends A,B
{
void methodC()
{
System.out.println("class C method");
}
}
class main2{
public static void main(String args[])
{
C obj = new C();
obj.methodA();
obj.methodB();
obj.methodC();
}
}
Last updated on by vishal devxo