Java Java Tutorial Java Methods Java Classes Java File Handling Java Reference



Java Inheritance

Java Inheritance is one of the fundamental concepts of object-oriented programming. It allows a class to inherit properties and methods from another class. Inheritance is a way to reuse code and create a hierarchy of classes. In Java, inheritance is implemented using the "extends" keyword.

When a class inherits from another class, it is called the subclass or derived class, and the class it inherits from is called the superclass or base class. The subclass inherits all the properties and methods of the superclass, and it can also add its own properties and methods.

Let's take a look at an example:


class Animal {
  public void eat() {
    System.out.println("The animal is eating");
  }
}

class Dog extends Animal {
  public void bark() {
    System.out.println("The dog is barking");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog myDog = new Dog();
    myDog.eat();
    myDog.bark();
  }
}

In this example, we have a superclass called "Animal" that has a method called "eat". We also have a subclass called "Dog" that extends the "Animal" class and has its own method called "bark".

In the main method, we create an instance of the "Dog" class and call the "eat" and "bark" methods. Since the "Dog" class extends the "Animal" class, it inherits the "eat" method from the superclass.

Another important concept in inheritance is the "super" keyword. The "super" keyword is used to call a method or constructor of the superclass. Let's take a look at an example:


class Animal {
  public Animal() {
    System.out.println("The animal is created");
  }
}

class Dog extends Animal {
  public Dog() {
    super();
    System.out.println("The dog is created");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog myDog = new Dog();
  }
}

In this example, we have a superclass called "Animal" that has a constructor that prints "The animal is created". We also have a subclass called "Dog" that extends the "Animal" class and has its own constructor that calls the superclass constructor using the "super" keyword and prints "The dog is created".

In the main method, we create an instance of the "Dog" class. When we create an instance of the subclass, the constructor of the superclass is automatically called first, followed by the constructor of the subclass.

Java also supports multiple inheritance through interfaces. An interface is a collection of abstract methods that a class can implement. Let's take a look at an example:


interface Animal {
  public void eat();
}

interface Mammal {
  public void run();
}

class Dog implements Animal, Mammal {
  public void eat() {
    System.out.println("The dog is eating");
  }
  public void run() {
    System.out.println("The dog is running");
  }
}

public class Main {
  public static void main(String[] args) {
    Dog myDog = new Dog();
    myDog.eat();
    myDog.run();
  }
}

In this example, we have two interfaces called "Animal" and "Mammal" that have one method each. We also have a class called "Dog" that implements both interfaces and has its own implementations of the "eat" and "run" methods.

In the main method, we create an instance of the "Dog" class and call the "eat" and "run" methods. Since the "Dog" class implements both interfaces, it must provide implementations for both methods.

Java inheritance is a powerful feature that allows us to reuse code and create a hierarchy of classes. It is important to understand the concepts of superclass, subclass, and the "super" keyword. Java also supports multiple inheritance through interfaces.

References

Activity