Java Intermediate

0% completed

Previous
Next
Single Level Inheritance

Single Level Inheritance is the simplest form of inheritance in Java, where a subclass inherits from one and only one superclass. This establishes a clear and straightforward relationship between two classes, promoting simplicity and avoiding the complexities associated with multiple inheritance.

Syntax of Single Inheritance

The basic syntax for implementing single inheritance in Java involves using the extends keyword. Here's the general structure:

Image
class Superclass { // Superclass members (variables and methods) } class Subclass extends Superclass { // Subclass members (additional variables and methods) }
  • Superclass: The class being inherited from.
  • Subclass: The class that inherits from the superclass.

The is-a Relationship

Inheritance establishes an is-a relationship between classes. This means that the subclass is a type of the superclass. For example:

  • A Car is a Vehicle.
  • A Dog is an Animal.

This relationship signifies that the subclass inherits the properties and behaviors of the superclass while also introducing its own specific features.

Example 1: Car Inheriting from Vehicle

In this example, the Car class inherits from the Vehicle class. The Vehicle class contains general attributes and methods common to all vehicles, while the Car class introduces attributes and methods specific to cars.

Java
Java

. . . .

Explanation:

  • Vehicle Class:

    • Attributes: brand, model, year
      These represent general characteristics common to all vehicles.
    • Constructor:
      Initializes the brand, model, and year of the vehicle.
    • Method: displayInfo()
      Displays the vehicle's brand, model, and manufacturing year.
  • Car Class:

    • Inheritance: extends Vehicle
      The Car class inherits from the Vehicle class, acquiring its attributes and methods.
    • Additional Attribute: carType
      Represents the type of the car (e.g., Sedan, SUV).
    • Constructor:
      Initializes the brand, model, year, and carType of the car by calling the superclass constructor using super().
    • Additional Method: displayCarType()
      Displays the car's type.
  • Solution Class:

    • main Method:
      • Creates an instance of Car with specific values.
      • Calls the inherited displayInfo() method to display general vehicle information.
      • Calls the subclass-specific displayCarType() method to display the type of the car.

Example 2: Dog Inheriting from Animal

In this example, the Dog class inherits from the Animal class. The Animal class includes general attributes and methods applicable to all animals, while the Dog class adds attributes and methods specific to dogs.

Java
Java

. . . .

Explanation:

  • Animal Class:

    • Attributes: name, age
      Represent general characteristics of animals.
    • Constructor:
      Initializes the name and age of the animal.
    • Method: displayInfo()
      Displays the animal's name and age.
  • Dog Class:

    • Inheritance: extends Animal
      The Dog class inherits from the Animal class, gaining access to its attributes and methods.
    • Additional Attribute: breed
      Specifies the breed of the dog.
    • Constructor:
      Initializes the name, age, and breed of the dog by calling the superclass constructor using super().
    • Additional Method: displayBreed()
      Displays the breed of the dog.
  • Solution Class:

    • main Method:
      • Creates an instance of Dog with specific values.
      • Calls the inherited displayInfo() method to display general animal information.
      • Calls the subclass-specific displayBreed() method to display the breed of the dog.

Single Inheritance in Java establishes a clear and straightforward relationship between a subclass and a single superclass. By leveraging the extends keyword, subclasses can inherit and utilize the properties and methods of their superclasses, promoting code reusability and logical class hierarchies. Through simple examples like the Car inheriting from Vehicle and Dog inheriting from Animal, we've demonstrated how single inheritance works in practice. This foundational understanding prepares you for more advanced inheritance concepts and their applications in Java programming.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next