Single inheritance

Syntax

class A {
    // code to be executed
};

class B extends A {
    // code to be executed
};

Single inheritance in Java is a type of inheritance where a subclass extends a single superclass. In Java, a class can only have one direct superclass, but the superclass can have any number of subclasses.

When a subclass extends a superclass, it inherits all of the non-private fields and methods of the superclass. This allows the subclass to reuse the code of the superclass, which can reduce redundancy and improve code organization.

Here is an example of single inheritance in Java:

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

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

class Main {
  public static void main(String[] args) {
    Dog myDog = new Dog();
    myDog.eat();  // inherited from Animal
    myDog.bark(); // specific to Dog
  }
}

In this example, the Animal class has a method called eat, and the Dog class extends the Animal class and adds a method called bark. In the main method, an instance of the Dog class is created, and the eat and bark methods are called on it.

The eat method is inherited from the Animal class, and the bark method is specific to the Dog class. This demonstrates how the Dog class can reuse code from the Animal class, while also adding its own specific functionality.

Code Example 1 – constructors

This code defines three classes: Animal, Dog, and Cube.

Animal and Dog are classes that are related by inheritance, with Dog being a subclass of Animal.

The Animal class has a constructor that prints the message “This is an Animal”. The Dog class has a constructor that prints the message “This is a Dog”. When a new Dog object is created in the Cube class by invoking the Dog constructor, the messages from both constructors will be printed to the console.

class Animal {
    Animal() {
        System.out.println("This is an Animal");
    }
};

class Dog extends Animal {
    Dog() {
        System.out.println("This is a Dog");
    }
};

class Cube {
	public static void main(String[] args) {
		Dog obj = new Dog();
	}
}
Output
This is an Animal
This is a Dog

Code Example 2 – methods

This code defines three classes: Animal, Dog, and SingleInheritance.

Animal and Dog are classes that are related by inheritance, with Dog being a subclass of Animal.

The Animal class has a method called printAnimal() that prints the message “This is an Animal”. The Dog class has a method called printDog() that prints the message “This is a Dog”. Since Dog is a subclass of Animal, it inherits the printAnimal() method from Animal.

The SingleInheritance class has a main method that creates a new Dog object called obj. It then calls the printAnimal() method on obj, which prints “This is an Animal”, and calls the printDog() method on obj, which prints “This is a Dog”.

class Animal {
    public void printAnimal() {
        System.out.println("This is an Animal");
    }
};

class Dog extends Animal {
    public void printDog() {
        System.out.println("This is a Dog");
    }
};

class SingleInheritance {
    public static void main(String[] args) {
        Dog obj = new Dog();
        obj.printAnimal();
        obj.printDog();
    }
}
Output
This is an Animal
This is a Dog