in , ,

Java Inheritance

Java Inheritance
Java Inheritance

Java Inheritance

In this tutorial, we will find out about inheritance in Java with the help of examples.

Inheritance is one of the key highlights of OOP (Object-oriented Programming) that allows us to characterize another class from a current class. For instance,

class Animal
{
    // eat() method
    // sleep() method
}
class Dog extends Animal
{
    // bark() method
}

In Java, we use the extends keyword to inherit from a class. Here, we have inherited the Dog class from the Animal class.

The Animal is the superclass (parent class or base class), and the Dog is a subclass (child class or derived class). The subclass inherits the fields and methods of the superclass.


is-a relationship

Inheritance is an is-a relationship. We use inheritance just if an is-a relationship is available between the two classes.

Here are some examples:

  • A car is a vehicle.
  • Orange is a fruit.
  • A surgeon is a doctor.
  • A dog is an animal.

Example 1: Java Inheritance

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

I can eat
I can sleep
I can bark

Here, we have inherited a subclass Dog from superclass Animal. The Dog class inherits the methods eat() and sleep() from the Animal class.

Hence, objects of the Dog class can access the members of both the Dog class and the Animal class.


protected Keyword

We learned about private and public access modifiers in previous tutorials.

  • private members can be accessed only within the class
  • public members can be accessed from anywhere

You can likewise assign methods and fields protected. Protected members are accessible

  • from within the class
  • within its subclasses
  • within the same package

Here’s a summary from where access modifiers can be accessed.

 ClassPackagesubclassWorld
publicYesYesYesYes
privateYesNoNoNo
protectedYesYesYesNo

Example 2: protected Keyword

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

I can eat
I can sleep
I can bark
I am a mammal
My color is black

Here, the type field inside the Animal class is protected. We have accessed this field from the Main class using

dog1.type = "mammal";

It is possible because both the Animal and Main classes are in the same package (same file).


Java Method overriding

From the above examples, we realize that objects of a subclass can likewise get to strategies for its superclass.

What occurs if a similar method is characterized in both the superclass and subclass?

Well, in that case, the method in the subclass supersedes the mthod in the superclass. For instance,

Example 3: Method overriding 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

I eat dog food
I can sleep
I can bark

Here, eat() is available in both the superclass Animal and subclass Dog. We made an object dog1 of the subclass Dog.

At the point when we call eat() using the dog1 object, the strategy inside the Dog is called, and a similar technique for the superclass isn’t called. This is called method overriding.

In the above program, we have used the @Override annotation to tell the compiler that we are overriding a method. Be that as it may, it’s not compulsory. We will find out about the method overriding in detail in the next tutorial.

If we have to call the eat() method for Animal from its subclasses, we use the super watchword.

Example 4: 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 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

I am an Animal
I am a dog
I can eat
I eat food
I can bark

Here, we have used the super keyword to call the constructor using super(). Also, we have called the eat() method of Animal superclass using super.eat().

Note the difference in the use of super while calling constructor and method


Types of inheritance

There are five types of inheritance.

  • Single inheritance – Class B extends from class A only.
  • Multilevel inheritance – Class B extends from class A; then class C extends from class B.
  • Hierarchical inheritance – Class A acts as the superclass for classes B, C, and D.
  • Multiple inheritances – Class C extends from interfaces A and B.
  • Hybrid inheritance – Mix of two or more types of inheritance.

Java doesn’t support different and hybrid inheritance through classes. Be that as it may, we can accomplish various inheritances in Java through interfaces. We will find out about interfaces in later chapters.


Why use inheritance?

The most significant use is the reusability of code. The code that is available in the parent class shouldn’t be written again in the child class.

To accomplish runtime polymorphism through method overriding. We will learn more about polymorphism in later chapters.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java instanceof

Java instanceof

Java Method Overriding

Java Method Overriding