basics of inheritance in java 2022

Core Java inheritanc :-

Inheritance can be defined as the procedure by which one object acquires all the properties and behaviours of a parent object. when a class acquires the properties of its parent class. In the concept of inheritance a class is made then another class is extended from it then the extended class can use the properties of its parent class with the help of inherited class object or child class object.This provide code reusability also.Inheritance is also called IS-A relationship between parent class and child class.

class Employee

{

String name;

public Employee(String name) // Having same name as of the class.

{

this.name =name;

System.out.println(name);

}

public static void main(String[] args)

{

Employee e1 = new Employee("I am parameterized constructor");

}

}

Example :-

Dog

IS-A

Animal

Sparrow

IS-A

Bird

Advantages of inheritance :-

  1. Code reusability
  2. Polymorphism can be achieved


Disadvantage of inheritance

Tightly coupled

Tightly coupling means if we inherit 10 classes from a single class or one parent class then if we make changes in the parent class then all ten classes will be affected by that parent class of all those classes.

java simple inheritance program :-

class Animal

{

void eat()

{

System.out.println("I am eating");

}

}

class Dog extends Animal

{

public static void main(String[] args)

{

Dog d= new Dog();

d.eat();

}

}

⇒ Output :

I am eating.

different types of inheritance in java:-

  1. Single Inheritance
  2. Multi-Level Inheritance
  3. Heirarchial Inheritance
  4. Multiple Inheritance(NOT SUPPORTED IN JAVA
  5. Hybrid Inheritance (NOT SUPPORTED IN JAVA

⇒ Single Inheritance

single inheritance

In this type of inheritance only one parent class and then an extended class will be available.

class A

{

void showA()

{

System.out.println("a class method");

}

}

class B extends A

{

void showB()

{

System.out.println("b class method");

}

public static void main(String[] args)

{

B ob2= new B();

ob2.showA();

ob2.showB();

}

}

⇒ Output :

I am eating.

⇒ Multi-Level Inheritance

multilevel inheritance

It is a type of inheritance in which one class is made then another class is extended from the parent then again one more class is extended from previous extended class. The image given below is the example hoe multi-level inheritance works the ‘Animal class’ here acts as parent class to’Dog class’ and dog class will be parent class of ‘SmallDog’ class.

class A

{

void showA()

{

System.out.println("a class method");

}

}

class B extends A

{

void showB()

{

System.out.println("b class method");

}

}   

class C extends B

{

void showC()

{

System.out.println("C class method")

}

public static void main(String[] args)

{

C ob3=new C();

ob3.showA();

ob3.showB();

ob3.showC();

}

}

⇒ Output :

Dog is eating.

Dog is running.

⇒ Heirarchial Inheritance

Heirarchial Inheritance

In this type of inheritance one class is made from which two classes are extended.The diagram given below describes how heirarchial inheritance works here ‘Animal class’ is the parent class of both classes ‘Dog and “SmallDog”.

class A

{

void showA()

{

System.out.println("a class method");

}

}

class B extends A

{

void showB()

{

System.out.println("b class method");

}

}   

class C extends A

{

void showC()

{

System.out.println("C class method")

}

public static void main(String[] args)

{

C ob3=new C();

ob3.showA();

ob3.showC();

}

}

⇒ Output :

Dog is barking.

Dog is eating.

Note
In single inheritance constructors can never be inherited into another class.
Private accessibility can’t be inherited into another class

Every class will extend any other class and if there is a class which is not having any parent class then by default its parent class will be object class.Parent class of all classes of java is OBJECT class.One class can’t have more then one super class because java doesn’t support multiple inheritance.

Multiple inheritance in java example

Multiple inheritance in java example

(NOT SUPPORTED IN JAVA)

This type of inheritance does not support java because in this type of inheritance the class which is inheriting the two classes will be confused if two same methods will be given in those two upper classes and the child class is calling that method. This gives a compile time error.

Hybrid Inheritance in java example

Hybrid Inheritance in java example

(NOT SUPPORTED IN JAVA)

Hybrid inheritance is the inheritance in which one parent class will be there and two child classes will be inherited from that one class and then from those two classes one class will be inherited but this doesn’t support in java.

⇒ Relationship between classes in java :-

when we learn a programming language then we mostly work in single class to solve problem but in real world there are complex problem and projects are handled at a higher level then single class won’t be enough for that so there multiple classes are used and data goes from one class to another.

So data is handled in multiple classes and there is a relationship between the classes.

Types of relationship between :-

  1. IS-A(Inheritance)
  2. HAS-A(Association)

⇒ IS-A(Inheritance)

This relationship is used when we extend one class from another class. This is a tightly coupled relationship or blood relation if we make changes in parent then changes will happen in all child classes.

Example : Dog IS-A Animal



class A

{

void showA()

{

System.out.println("a class method");

}

}

class B extends A

{

void showB()

{

System.out.println("b class method");

}

public static void main(String[] args)

{

B ob2= new B();

ob2.showA();

ob2.showB();

}

}

⇒ Output :

I am eating.

⇒ HAS-A

(Association)

It is loosly coupled relationship. Their will be no blood relation between classes. If we make changes in one class then it won’t affect other child classes of it.

Example

In this relationship is like

Student HAS-A name

Student HAS-A roll_no

class Student

{

String name;

int roll_no;

}

It has two forms

  1. Aggregation form
  2. Composition form

Aggregation form

In this there is weak bonding between classes.

Example

If there is parent class “Car” and it has a subclasses “MusicPlayer”.So there is weak bonding between “MusicPlayer” and “Car” because car can work with and without “MusicPlayer”.

Composition form

In this there is Strong bonding between classes.

Example :-

class Test

{

void show(int a)

{

System.out.println("1");

}

void show()

{

System.out.println("2");

}

public static void main(String[] args)

{

Test t = new Test();

t.show(5);// It will call first function

}

}

Output:-

1

If there is parent class “Car” and it has a subclasses “Engine”.So there is Strong bonding between “Engin” and “Car” because car cannot without “Engine”.

⇒ Advantages of Relationship between classes

  1. Code reusability
  2. Cost cutting
  3. Reduce Redundancy-unnecessary code will be removed

Additional Resources

Basic Principles of OOPS (Object-Oriented Programming)

Leave a Comment