Inheritance in Java (Is-A relationship)

Here is the table content of the article we will cover on this topic.
1. What is Inheritance in java?
2. Important terms about Inheritance in java?
3. How to use inheritance?
4. How it relates to real life?
5. Single Inheritance?
6. Multilevel Inheritance?
7. Hierarchical Inheritance?

8. What is Multiple inheritance in java?
9. What Problem occurs in multiple inheritance in Java?
10. What is the diamond problem?

11. How to achieve multiple inheritances?
12. Why multiple inheritance is not supported in JAVA?
13. Java interface inheritance?
14. Type Casting in Inheritance?
15. How the interface uses inheritance?
16. Multiple inheritance in Java by interface inheritance?

17. Inheritance and method overriding?

What is inheritance in Java?

Inheritance is an important concept/feature of Object-Oriented Programming. Inheritance is a  mechanism by which one class can acquire/inherit the features(fields and methods) of another class. In this article, we will read the concept of Inheritance in java. It means in java one class can inherit the variables and methods of another class. It is also known as the Is-A relationship.

  • Reusability: The aim of inheritance is to reusability of code. If we want to create a new class but there is already a class that includes some of the code that we want, so that we can reuse the existing one by using the inheritance concept. We will discuss it in detail.
  • Method Overriding: We will discuss it in polymorphism.

Important terms about Inheritance in java

Base/Super/Parent class: The Base class is the class whose variables and methods are used(inherited) by another class. It is also known as the superclass or parent class.

Derived/sub/child class: The Derived class is the class that inherits the variables or methods of other classes. It is also known as the subclass or child class. The derived class can add its own variables or methods in addition to the base class variables or methods

Inheritance in java
class Derived_Class_Name extends  Base_Class_Name
{
    // Body of derived class
} 

We can inherit the features of one class to another class by the use of keyword extends. The extends keyword tells the compiler that a new class making derives from an existing class. The extends keywords increase functionality.

class ExampleOfBaseClass
{
	// Body of base class
}
class ExampleOfDerivedClass extends  ExampleOfBaseClass
{
	// Body of derived class
}

How to use inheritance?

Now I am going to showing an example of inheritance. Let’s say we have a derived class Swift that extends the base class Car.

class Car
{
	String nameOfCompany = "Suzuki";
	boolean isACAvail = true;
	int numberOfTyre = 4;
	
	public void getCarDetails()
	{
		System.out.println("Company name of Car = "+ nameOfCompany);
		System.out.println("Is AC available in car = "+ isACAvail);
		System.out.println("Number tyres in Car = "+ numberOfTyre);
	}
}

public class Swift extends Car
{
	String carName = "Swift";
	String modelName = "Vxi";
	String color = "white";
	
	public void getModelDetails()
	{
		System.out.println("Name of Car = "+ carName);
		System.out.println("Model name = "+ modelName);
		System.out.println("Color of model = "+ color);
	}
	
	public static void main(String arg[])
	{
		Swift newSwift = new Swift();
		newSwift.getCarDetails();
		newSwift.getModelDetails();
	}
}

Output: Company name of Car = Suzuki
Is AC available in car = true
Number tyres in Car = 4
Name of Car = Swift
Model name = Vxi
Color of model = white

As you see in the above example the swift class(Derived class) inherits the properties of the Car class(Base class). When we extend the car class(Base class) then indirectly its code available in the swift class (derived class). So, we can easily access the variables or methods of a base class.

How it relates to real life?

If you are the owner of the Car company and want to build a new model of car. Then you don’t want to build an engine, tire, body, etc. because these things are already discovered. Infect these are the common things to build a car. So, you can you existing things to build a car and add some new features according to your model. 

In JAVA programming: We define some common things in Car class like company name, tire, etc. These are the generic things that are used to build any model of car. So, we declared some variables and methods in class Car. Now, if I am wanting to build a new model Swift, then we can use the existing code from class Car.

Inheritance in java

Types of Inheritance in Java

  1. Single inheritance
  2. Multilevel inheritance
  3. Hierarchical inheritance

1. Single inheritance

There is only one base class, and another is a derived class. The derived class inherits all the properties of the base class. The given above example is a type of single inheritance.

2. Multilevel inheritance

In multilevel inheritance, a derived class inherits the properties of a base class, and as well as the derived class also acts as a base class for other classes.

In this example, class B inherits the properties of class A and class C inherits the properties of class B. So, class B is acting as a derived class for class A and the base class for class C.

Example: In this example, we have three classes. Car is the base class, Swiftnew is the intermediate class and SwiftNewModel is the derived class. The SwiftNewModel indirectly inherits the properties of Swiftnew and Car.

class Car
{
	String nameOfCompany = "Suzuki";
	boolean isACAvail = true;
	int numberOfTyre = 4;
	
	public void getCarDetails()
	{
		System.out.println("Company name of Car = "+ nameOfCompany);
		System.out.println("Is AC available in car = "+ isACAvail);
		System.out.println("Number tyres in Car = "+ numberOfTyre);
	}
}

class SwiftNew extends Car
{
	String carName = "Swift";
	String modelName = "Vxi";
	String color = "white";
	
	public void getModelDetails()
	{
		System.out.println("Name of Car = "+ carName);
		System.out.println("Model name = "+ modelName);
		System.out.println("Color of model = "+ color);
	}
	
}

public class SwiftNewModel extends SwiftNew
{
	boolean  autoMatic = true;
	
	public void newSwiftFeature()
	{
		System.out.println("The new model of swift is Automatic = "+  autoMatic);
	}
	
	public static void main(String arg[])
	{
		SwiftNewModel  newSwiftModel = new SwiftNewModel ();
		newSwiftModel.getCarDetails();
		newSwiftModel.getModelDetails();
		newSwiftModel.newSwiftFeature();
	}
}

Output: Company name of Car = Suzuki
Is AC available in car = true
Number tyres in Car = 4
Name of Car = Swift
Model name = Vxi
Color of model = white
The new model of swift is Automatic = true

3. Hierarchical Inheritance

In hierarchical inheritance, more than one derived class can inherit the properties of one base class. There is only one base class and the derived class is more than one.

java inheritance

Example: In this example, we have three classes. Car is the base class, Swift and Alto are derived classes. The Swift and Alto, both are indirectly inheriting the properties of Car.

class Car
{
	String nameOfCompany = "Suzuki";
	boolean isACAvail = true;
	int numberOfTyre = 4;
	
	public void getCarDetails()
	{
		System.out.println("Company name of Car = "+ nameOfCompany);
		System.out.println("Is AC available in car = "+ isACAvail);
		System.out.println("Number tyres in Car = "+ numberOfTyre);
	}
}

class Swift extends Car
{
	String carName = "Swift";
	String modelName = "Vxi";
	String color = "white";
	
	public void getModelDetails()
	{
		System.out.println("Name of Car = "+ carName);
		System.out.println("Model name = "+ modelName);
		System.out.println("Color of model = "+ color);
	}
	
}

class Alto extends Car
{
	String carName = "Alto";
	String modelName = "Zxi";
	String color = "Black";
	
	public void getModelDetails()
	{
		System.out.println("Name of Car = "+ carName);
		System.out.println("Model name = "+ modelName);
		System.out.println("Color of model = "+ color);
	}
	
}

public class AllCars
{
	public static void main(String arg[])
	{ 
		// We can get information by creating the serpate objects
		
		Swift swift = new Swift();
		swift.getCarDetails();
		swift.getModelDetails();
		
		Alto alto = new Alto();
		alto.getCarDetails();
		alto.getModelDetails();		
	}
}

Output: Company name of Car = Suzuki
Is AC available in car = true
Number tyres in Car = 4
Name of Car = Swift
Model name = Vxi
Color of model = white
Company name of Car = Suzuki
Is AC available in car = true
Number tyres in Car = 4
Name of Car = Alto
Model name = Zxi
Color of model = Black

1. Quiz, Read the below code and do answer.

Which inheritance doesn't supported by java?

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

Which definition is true for subclass in java?

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

Which one is the correct syntax to extend class A?

 

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

What is the Order of execution of constructors in inheritance?

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

Inheritance relationship in Java language is _____?

Click on anyone to know the answer.

6. Quiz, Read the below code and do answer.

Advantage of inheritance in java programming is/are

Click on anyone to know the answer.

7. Quiz, Read the below code and do answer.

Which class can't be extend?

Click on anyone to know the answer.

8. Quiz, Read the below code and do answer.

Can we inherited constructor from the base class?

Click on anyone to know the answer.

9. Quiz, Read the below code and do answer.

Which one is true statement?
class B
{
  void show(){}
}
class A
{
  void hide(){}
}

Click on anyone to know the answer.

10. Quiz, Read the below code and do answer.

interface A
{
 public void method1();
}
 
class One implements A
{
     public void method1()
     {
         System.out.println("Method of One");
     }
}
 
class Two extends One
{
     public void method1()
     {
         System.out.println("Method of Two");
     }
 }

 public class Test extends Two 
 {
     public static void main(String[] args)
     {
         A a = new Two();
         a.method1();
     }
 }

Click on anyone to know the answer.

2 thoughts on “Inheritance in Java (Is-A relationship)”

  1. Hi, the below program will show an error. It will not execute the program. You need to include the class name for that. You have not mentioned a class
    class Car
    {
    String nameOfCompany = “Suzuki”;
    boolean isACAvail = true;
    int numberOfTyre = 4;

    public void getCarDetails()
    {
    System.out.println(“Company name of Car = “+ nameOfCompany);
    System.out.println(“Is AC available in car = “+ isACAvail);
    System.out.println(“Number tyres in Car = “+ numberOfTyre);
    }
    }
    Class Swift extends Car
    {
    String carName = “Swift”;
    String modelName = “Vxi”;
    String color = “white”;

    public void getModelDetails()
    {
    System.out.println(“Name of Car = “+ carName);
    System.out.println(“Model name = “+ modelName);
    System.out.println(“Color of model = “+ color);
    }

    public static void main(String arg[])
    {
    Swift newSwift = new Swift();
    newSwift.getCarDetails();
    newSwift.getModelDetails();
    }
    }
    —————————————————————–
    Correct:
    package Inheritance;

    public class Car // Car is a Parent Class
    {
    String nameOfCompany = “Suzuki”; // Instance Variable
    boolean isACAvail = true; // Instance Variable
    int numberOfTyre = 4; // Instance Variable

    public void getCardata() // Instance method for the Parent class
    {
    System.out.println(“Company name of Car = “+ nameOfCompany);
    System.out.println(“Is AC available in car = “+ isACAvail);
    System.out.println(“Number tyres in Car = “+ numberOfTyre);
    }
    }
    class Swift extends Car // Swift is a derived class for Parent class
    {
    String carName = “Swift”; // Instance Variable
    String modelName = “Vxi”; // Instance Variable
    String color = “white”; // Instance Variable

    public void getModeldata() // Instance method for the Child class
    {
    System.out.println(“Name of Car = “+ carName);
    System.out.println(“Model name = “+ modelName);
    System.out.println(“Color of model = “+ color);
    }
    }

    package Inheritance;

    class Testinheritance{
    public static void main(String arg[])
    {
    Swift s=new Swift();
    s.getCardata();
    s.getModeldata();

    }
    }

    Please don’t upload the wrong code.

    Reply

Leave a Comment