Explanation of Inheritance in Java

Explanation of Inheritance in Java

Hello everyone, in this article we are going to talk about Inheritence feature of Object Oriented Programming in Java and we will make some examples for better understanding.

Let's get started.

Inheritence is one of the key concepts of object oriented programming. It allows us to create new classes using an existing class. We can derive new classes and these child classes can use properties of parent class. We can directly invoke the functions within parent class throgh child class.

Inheritance is increasing the code reusability. Same codes are being using in different classes with only one time declaring.

There are five types of inheritance:
  • Single Inheritance: A class inherits a super class.
    Class A Class B
  • Multilevel Inheritance: A class inherits a super class, and this this super class inherits ona other super class.
    Class A Class B Class C
  • Hierarchical Inheritance: We have a super class and there are other child classes of this super class.
    Class A Class B
    Class A Class C
    Class A Class D
  • Multiple Inheritance: A class inherits more than one super classes.
    Class B Class A
    Class C Class A

    Note: Java does not support multiple inheritance. Multiple inheritance can be provided by interface implementing.

  • Hybrid Inheritance: It ıs combination of more than one inheritance types.

We use extends keyword for inheritng an existing class.

Below you can see an example of inheritance

class Vehicle{
  public void start(){ ... }
}

class Car extends Vehicle {
    public static void main() { start(); }
}

class Motocycle extends Vehicle {
    public static void main() { start(); }
}

In above code block we created a Vehicle class. This class is our base class. We created Car and Motocycle classes and we derived them from Vehicle base class. We used start function which declared in base class without redeclaring in child classes.

Working with inheritance we need to pay attention access modifiers in base class.

In base class:
  • Private access modifier prevents the accessing property from outside even if it is a child class.
  • Protected access modifier allows child classes to access but prevents other classes.
  • Public access modifier allows everyone can access to the property.
  • Default access modifier is assigning when we dont specify any modifier to property. It allows to access from all around the same package.
Now lets make some real world examples.

import java.util.*;

public class Main {
    public static void main(String[] args) {
      Airbus a = new Airbus();
      Boeing b = new Boeing();
      a.initialize();
      b.initialize();
  }
}

public class Aircraft{
  public void start(){
    System.out.println("Started");
  }
}

public class Airbus extends Aircraft{
  public void initialize(){  
    System.out.println("Airbus initializing");
    start(); 
  }
}

public class Boeing extends Aircraft{
  public void initialize(){  
    System.out.println("Boeing initializing");
    start(); 
  }
}
Program output will be like below:

Airbus initializing
Started
Boeing initializing
Started

In above program we created a Airvraft class. This class is our base class and we derived two child classes from this base class. We called the start function which declared in base class without redeclaring them. This is the basic usage of inheritance feature of object oriented programming.

Sometimes we may need to access to base class directly. In these cases we need to use super keyword. With super keyword are able to access the inherited class. Below example you will see the how to use super keyword and accessing to the super class.


import java.util.*;

public class Main {
    public static void main(String[] args) {
      Boeing b = new Boeing();
      b.initialize();
  }
}

public class Aircraft{
  public void start(){
    System.out.println("Started");
  }
  public void greetAircraft(){
    System.out.println("Aircrafts are made for flying. This method called from base class");
  }
}

public class Boeing extends Aircraft{
  public void initialize(){  
    System.out.println("Boeing initializing");
    start(); 
    System.out.println("Super class invoking");
    super.greetAircraft();
  }
}
Program output will be like below

Boeing initializing
Started
Super class invoking
Aircrafts are made for flying. This method called from base class

That is all in this article.

Have a good class extending.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment. Check your inputs!

Comments

  • There is no comment. Be the owner of first comment...