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

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.

Types of Inheritance in Java
- Single inheritance
- Multilevel inheritance
- 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.

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
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.
Please try to compile the above programs they are working fine. Thankyou
What are you trying to say, that the main method should be included in a different class?
You have missed public in your class definition:
Class Swift extends Car –> pubic class Swift extends Car{}
Since we are writing everything in the same file, the class where the main method is defined should be public.