In this Java tutorial, you will learn what inheritance is in object-oriented programming, how Java implements inheritance with the extends keyword, what types of inheritance Java supports with classes, and how inherited fields and methods work in a simple program.
What inheritance in Java means
Inheritance in Java is an object-oriented programming feature that lets one class derive fields and methods from another class. The class that is inherited from is called the superclass, parent class, or base class. The class that inherits is called the subclass, child class, or derived class.
Java implements class inheritance using the extends keyword. When a class extends another class, an object of the subclass can use accessible members of the superclass as if they are available through the subclass object.

Java extends syntax for inheritance
The basic syntax for inheritance in Java places the subclass name first, followed by the extends keyword and the superclass name.
class SubClass extends SuperClass {
// fields and methods of SubClass
}
For example, if SmartPhone extends MobilePhone, then SmartPhone becomes a specialized form of MobilePhone. The smartphone can have its own members and can also use the accessible members declared in the mobile phone class.
Why inheritance is used in Java class design
Inheritance is useful when a subclass is a more specific version of a superclass. It helps avoid repeated code and keeps common behavior in one place.
- Code reuse: common fields and methods can be written once in the superclass.
- Specialization: subclasses can add their own fields and methods for more specific behavior.
- Method overriding: subclasses can provide their own implementation for inherited methods when required.
- Polymorphism: a superclass reference can refer to subclass objects, which helps write flexible code.
Superclass and subclass rules in Java inheritance
The following points are important when working with Java inheritance.
- Java supports single inheritance with classes. A class can extend only one direct superclass.
- Multiple subclasses can extend the same superclass. This is called hierarchical inheritance.
- A subclass can use the accessible fields and methods of its superclass.
privatefields and methods are not directly accessible in the subclass. They can be accessed only through public, protected, or package-level methods provided by the superclass.- Constructors are not inherited, but the superclass constructor is called when a subclass object is created.
- Every class in Java ultimately inherits from
java.lang.Objectunless the inheritance chain already leads to it.
When a child class inherits a parent class, the compiler does not copy the superclass methods into the subclass source code. Instead, the Java type system allows the subclass object to resolve accessible inherited members through the class hierarchy.
Inheritance example in Java: MobilePhone and SmartPhone
Usually in Java, Parent class is also called as Super class, and Child class is also called as Sub Class.
In this example project, we shall create a Super Class, MobilePhone.java and a Sub Class SmartPhone.java.
Following is the parent class or super class.
MobilePhone.java
package com.tutorialkart.java;
/**
* @author tutorialkart
*
*/
public class MobilePhone {
public int price;
public void makeCall(){
System.out.println("Calling...");
}
public void getCharge(){
System.out.println("Charging...");
}
// private method is not visible to other classes
private void check(){
System.out.println("This is private to MobilePhone");
}
}
Following is the child class or subclass.
SmartPhone.java
package com.tutorialkart.java;
/** This class inherits MobilePhone
* @author tutorialkart
*/
public class SmartPhone extends MobilePhone {
public String name;
public static void main(String[] args) {
SmartPhone sonyXZ = new SmartPhone();
// variable of SmartPhone (Sub Class)
sonyXZ.name = "Sony XZ";
// variable of MobilePhone (Super Class)
sonyXZ.price = 40500;
// method of SmartPhone (SubClass)
sonyXZ.playVideo();
// methods of MobilePhone (Super Class)
sonyXZ.makeCall();
sonyXZ.getCharge();;
}
public void playVideo(){
System.out.println("Playing video..");
}
}
Run SmartPhone.java program, and the output to console should be as shown in the following.
Output
Playing video..
Calling...
Charging...
How the SmartPhone object uses inherited members
In the example, SmartPhone extends MobilePhone. Because of this relationship, the object sonyXZ can access the public field price and the public methods makeCall() and getCharge() from MobilePhone.
The same object can also access name and playVideo(), which are declared directly inside SmartPhone. However, the private method check() in MobilePhone cannot be called directly from SmartPhone, because private members are visible only inside the class in which they are declared.
Types of inheritance in Java and what Java supports
Many Java interview and learning resources discuss five inheritance patterns. Java supports some of them directly with classes and some through interfaces.
| Inheritance type | Supported with Java classes? | How it works in Java |
|---|---|---|
| Single inheritance | Yes | One subclass extends one superclass. |
| Multilevel inheritance | Yes | A class extends a subclass, forming a chain. |
| Hierarchical inheritance | Yes | Multiple subclasses extend the same superclass. |
| Multiple inheritance | No, not with classes | A class cannot extend more than one class. Java uses interfaces for multiple type inheritance. |
| Hybrid inheritance | Not directly with classes | It may be modeled using a mix of class inheritance and interfaces. |
This restriction keeps class inheritance easier to reason about and avoids ambiguity that can happen when the same method is inherited from multiple parent classes. For official background on subclasses, you may refer to the Oracle Java Tutorials page on subclasses.
Method overriding in Java inheritance
A subclass can override an inherited method when it needs a different implementation. The method name, return type, and parameters must match the superclass method. The @Override annotation is recommended because it lets the compiler verify that overriding is actually happening.
class Vehicle {
void start() {
System.out.println("Vehicle starts");
}
}
class Car extends Vehicle {
@Override
void start() {
System.out.println("Car starts with a key");
}
public static void main(String[] args) {
Car car = new Car();
car.start();
}
}
Car starts with a key
Here, Car inherits from Vehicle, but its own start() method replaces the inherited implementation for Car objects.
Using super in Java inheritance
The super keyword refers to the immediate superclass. It is commonly used to call a superclass constructor, access a superclass field, or call an overridden superclass method.
class Phone {
String category = "Basic phone";
void showCategory() {
System.out.println(category);
}
}
class AndroidPhone extends Phone {
String category = "Smartphone";
void showBothCategories() {
System.out.println(category);
System.out.println(super.category);
super.showCategory();
}
}
In this example, category refers to the field in AndroidPhone, while super.category refers to the field in Phone.
When inheritance is not the right choice in Java
Use inheritance only when the subclass really is a specialized form of the superclass. For example, SmartPhone is a kind of MobilePhone, so inheritance is reasonable. If one class only uses another class as a helper, composition is usually better.
- Use inheritance for an is-a relationship, such as
Caris aVehicle. - Use composition for a has-a relationship, such as
Carhas anEngine. - Avoid deep inheritance chains because they can make code harder to understand and maintain.
- Do not use inheritance only to avoid writing a few lines of code. The relationship between the classes must still be correct.
Common mistakes while learning Java inheritance
- Assuming private members are inherited in a directly usable way. Private members belong to the superclass implementation and are not directly accessible in the subclass.
- Forgetting that Java does not allow
class A extends B, C. A class can extend only one class. - Confusing method overloading with method overriding. Overriding happens across superclass and subclass with the same method signature.
- Trying to inherit constructors. Constructors are not inherited, although superclass constructors are invoked during subclass object creation.
- Using inheritance where composition gives a cleaner design.
Inheritance in Java FAQs
What is inheritance in Java?
Inheritance in Java is an object-oriented feature where one class extends another class and gains access to its accessible fields and methods. The inherited class is the superclass, and the class that extends it is the subclass.
What keyword is used for inheritance in Java?
The extends keyword is used for class inheritance in Java. For example, class SmartPhone extends MobilePhone means SmartPhone is a subclass of MobilePhone.
What are the types of inheritance supported in Java?
Java supports single, multilevel, and hierarchical inheritance with classes. It does not support multiple inheritance with classes, but a class can implement multiple interfaces.
Can a Java class inherit private methods?
Private methods are not directly accessible in a subclass. A subclass can use them only indirectly through accessible superclass methods that call those private methods.
What is the difference between inheritance and polymorphism in Java?
Inheritance defines a superclass-subclass relationship. Polymorphism lets code use a superclass reference to work with different subclass objects, often with overridden methods being selected at runtime.
Java inheritance QA checklist for this tutorial
- The tutorial explains the
extendskeyword with a superclass and subclass example. - The MobilePhone and SmartPhone program shows inherited fields, inherited methods, and subclass-specific members.
- The notes clearly state that Java allows only one direct superclass for a class.
- The private-member rule is explained without implying direct subclass access.
- The FAQ answers cover definition, syntax, supported inheritance types, private methods, and polymorphism.
Summary of inheritance in Java with extends
In this Java Tutorial, we learned that inheritance in Java lets a subclass reuse and specialize the accessible members of a superclass. Java uses the extends keyword for class inheritance, supports single class inheritance, and allows subclasses to add new behavior or override inherited methods when needed.
TutorialKart.com