Java Inheritance

Java Inheritance

Java Inheritance Concept

Inheritance is a feature of Object Oriented Programming which allows an Object to use the properties and behavior of parent class. Inheritance supports re-usability of code.

Let’s understand this concept by a real world example as below :

“Let’s take an example of a car. In day to day life, we see different cars of different models. They are different in structure, speed and facilities. But what we call them is important, we call them “Car” whether it is audi, BMW, maruti etc. They all are “Car”.So, Car is the parent of all the cars available in the market. Every Car has engine, tyres, steering, break etc and different models in the market are inheriting the properties of Car like they all have engine, tyres, gear, steering.”

inheritance in java

So as discussed in above example, all models of Car acquire the properties of parent “Car”.

In java, you can inherit a class by using extends keyword. When you extend an existing class in your child class that means all the protected and public methods and properties will automatically be acquired by child class which is called “Code Re-usability”.

Important things to remember about Java Inheritance

  1. You inherit any class by using “extends” keyword in java.
  2. You can extend only and only one class at a time.
  3. All the public and protected methods and data members will become the part of child class.
  4. Private data members and methods can not be inherited.
  5. Inheritance supports method overriding.

Advantages of Java Inheritance
  1. One of the key benefit of using inheritance in java is to minimize the amout of duplicate code in application. When you write some code in base class, we can share common code among serveral subclasses.
  2. Provides flexibility in application code which is easy to maintain.
  3. Code Reusability which means you can share your public data members and logic to child class. Child class will then use that code instead of writing from scratch.
  4. Extensibility means you can extend the code provided by the base class as per business logic.
  5. Data hiding that means you can keep some data private so that child class can not access and modify that particular information.
  6. overriding is features in java inheritance in which you can override parent class method in your child class.

Syntax and Basic Example

Let’s know the basic syntax of java inheritance and create a basic example.

Syntax

class ClassName extends ParentClass {

}

Example
class Parent {
      private int a = 10;

      public void showA() {
           System.out.println("Value of a : " + a);
      }
}

class Child extends Parent {
      
      public static void main(String arg[]) {
 
           Child c = new Child();
           c.showA(); // Will print "Value of a : 10
      }
}

All the public and protected methods will be acquired by the child class and that’s what happening above. Child class can now access the parent class method “showA()”, because it is public in parent class.

Types of Inheritance in Java

You can inherit you parent class in different ways in java. Java support single, multi level, hierarchical inheritance in java.

Java Inheritance

Single Level Inheritance

In single level inheritance, you inherit you parent class with your base class. In this process, only one parent and child classes are involved.

Syntax

class Parent { }

class child extends Parent { }



Multi Level Inheritance

In multi level inheritance, we inherit parent class with the child class and then that child class further inherited by another child class and so on. You can create level of inheritance. In this process, top class becomes the parent of child class.

Note*: In case of multi level inheritance, child class at the end will have access to the all public and protected methods and properties of every parent class.

Syntax

class Parent { }

class ChildOne extends Parent { }

class ChildTwo extends ChildOne { }


Example

class Parent {
      private int a = 10;

      public void showA() {
           System.out.println("Value of a : " + a);
      }
}

class ChildOne extends Parent {
      private int b = 20;
      public void showB() {
           System.out.println("Value of b : " + b);
      }
}

class ChildTwo extends ChildOne {
      public static void main(String arg[]) {

          ChildTwo two = new ChildTwo();
          two.showA(); // Value of a : 10
          two.showB(); // Value of b : 20
      }
}


Hierarchical Inheritance


In Hierarchical inheritance, One parent class in inherited by multiple child classes in a hierarchy. As described in graphical example above.

syntax

class Parent { }

class ChildOne extends Parent { }

class ChildTwo extends Parent { }


Example

class Parent {
      private String message = "Message from admin";

      public void getMessage() {
           System.out.println(this.message);
      }
}

class ChildOne extends Parent {
      private int b = 20;
      public void showB() {
           System.out.println("Value of b : " + b);
      }
}

class ChildTwo extends Parent {
      private int b = 20;
      public void showB() {
           System.out.println("Value of b : " + b);
      }
}
class Test {
      public static void main(String arg[]) {
          ChildOne one = new ChildOne();
          ChildTwo two = new ChildTwo();

          one.getMessage();
          two.getMessage();
      }
}

Java doesn’t support multiple inheritance

Java doesn’t support multiple inheritance, there is a reason behind this. Let’s see the example below first to understand it better.

class Parent1 {
      public void whoIsBest() {
         System.out.println("Parent 1 is best.");
      }
}

class Parent2 {
      public void whoIsBest() {
         System.out.println("Parent 2 is best.");
      }
}

class Child extends Parent1, Parent2 {

      public static void main(String arg[]) {
          Child c = new Child();
          c.whoIsBest(); // And the war begins. Who is best, how compiler will decide.
      }
}
That’s the reason, java doesn’t support multiple inheritance. When two parent class have the same method in each other and they are different in nature. Then Compiler will not be able to decide which one to call.

So to avoid conflict, complexity java doesn’t support multiple inheritance.

Related Post

One thought on “Java Inheritance”

Leave a Reply

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