English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, we will learn about inheritance in Java through examples.
Inheritance is one of the important features of OOP (Object-Oriented Programming), which allows us to define a new class from an existing class. For example,
class Animal class Dog extends Animal // eat() method // sleep() method } class Dog extends Animal class Dog extends Animal // { }
bark() method
Animal is the superclass (parent class or base class), and Dog is the subclass (subclass or derived class). The subclass inherits the fields and methods of the superclass. In Java, we use the extends keyword to inherit from a class. Here, we inherit the Dog class from the Animal class.
Inheritance isis-arelationship, only when there is an is-When there is a relationship, we use inheritance.
Here are some examples:
Cars are vehicles.
Oranges are fruits.
Surgeons are doctors.
Dogs are animals.
class Animal { public void eat() { System.out.println("I can eat"); } public void sleep() { System.out.println("I can sleep"); } } class Dog extends Animal { public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.sleep(); dog1.bark(); } }
Output result
I can eat I can sleep I can bark
Here, we inherit the subclass Dog from the superclass Animal. The Dog class inherits the eat() and sleep() methods from the Animal class.
Therefore, the objects of the Dog class can access the members of the Dog class and the Animal class.
We learned about the private and public access modifiers in the previous tutorial.
Private members can only be accessed within the class
Public members can be accessed from anywhere
You can also set methods and fields to protected, and protected members can be accessed
Within the class
In its subclass
Within the same package
This is an overview of access modifiers that can be accessed.
Modifier | Class | Package | Subclass | Global |
---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
private | Yes | No | No | No |
protected | Yes | Yes | Yes | No |
class Animal { protected String type; private String color; public void eat() { System.out.println("I can eat"); } public void sleep() { System.out.println("I can sleep"); } public String getColor(){ return color; } public void setColor(String col){ color = col; } } class Dog extends Animal { public void displayInfo(String c){ System.out.println("I am a " + type); System.out.println("My color is " + c); } public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.sleep(); dog1.bark(); dog1.type = "mammal"; dog1.setColor("black"); dog1.displayInfo(dog1.getColor()); } }
Output result
I can eat I can sleep I can bark I am a mammal My color is black
In this case, the type field in the Animal class is protected. We have accessed this field from the Main class
dog1.type = "mammal";
This is feasible because both the Animal and Main classes are in the same package (the same file).
From the above example, we know that an object of the subclass can also access its superclass methods.
What happens if the same method is defined in both the superclass and the subclass?
Alright, in this case, the method in the subclass will override the method in the superclass. For example,
class Animal { protected String type = "animal"; public void eat() { System.out.println("I can eat"); } public void sleep() { System.out.println("I can sleep"); } } class Dog extends Animal { @Override public void eat() { System.out.println("I eat dog food"); } public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.sleep(); dog1.bark(); } }
Output result
I eat dog food I can sleep I can bark
In this case, eat() appears in both the superclass Animal and the subclass Dog. We create an object of the subclass Dog as dog1Java super keyword
When we use dog1When an object calls eat(), it will call the method within Dog instead of the same method in the superclass. This is called method overriding.
In the above program, we used the @Override annotation to tell the compiler that we are overriding a method. However, this is not mandatory. In the next tutorial, we will learn more aboutMethod overridingJava super keyword
If you need to call the eat() method from the Animal subclass, use the super keyword.
class Animal { public Animal() { System.out.println("I am an Animal"); } public void eat() { System.out.println("I can eat"); } } class Dog extends Animal { public Dog(){ super(); System.out.println("I am a dog"); } @Override public void eat() { super.eat(); System.out.println("I eat dog food"); } public void bark() { System.out.println("I can bark"); } } class Main { public static void main(String[] args) { Dog dog1 = new Dog(); dog1.eat(); dog1.bark(); } }
Output result
I am an Animal I am a dog I can eat I eat dog food I can bark
I can bark
Here, we use the super keyword to call the constructor through super(). Additionally, we use super.eat() to call the eat() method of the Animal superclass.Note: The difference in calling constructors and super methods. For more information, please visitJava super keyword
Inheritance Types
There are five types of inheritance. - Single Inheritance
Class B inherits only from class A. - Multi-level Inheritance
Class B inherits from class A, and then class C inherits from class B. - Hierarchical Inheritance
Class A is the superclass of B, C, and D. -Multiple Inheritance
Class C extends interface A and B. -Mixed InheritanceTwo or moreInherited
Mixed.
Why use inheritance?
The most important use is code reusability. Code that exists in the superclass does not need to be rewritten in the subclass. In the following chapters, we will learn more about polymorphism.