Inheritance is a key feature of object-oriented programming, which allows a class to inherit properties and behaviors from another class. In Java, inheritance is implemented using the extends
keyword, which allows a subclass to inherit properties and methods from a superclass. This helps in reducing code duplication and makes it easier to create and maintain software applications.
In this article, we will discuss inheritance in Java in detail, including how to define a superclass and a subclass, how to use constructors and methods in inheritance, and how to override methods in a subclass.
Defining a Superclass and a Subclass
To create a subclass, you first need to define a superclass. A superclass is a class that contains the properties and behaviors that you want to share with the subclass. Here’s an example of a superclass:
public class Animal {
private String name;
private int age;
private String breed;
public Animal(String name, int age, String breed) {
this.name = name;
this.age = age;
this.breed = breed;
}
public void makeSound() {
System.out.println("Animal is making a sound!");
}
}
In this example, we have defined a class called Animal
that has three private fields, a constructor, and a method called makeSound
. The constructor takes three parameters, which are used to initialize the fields of the class.
To create a subclass, you can use the extends
keyword followed by the name of the superclass. Here’s an example of a subclass:
public class Dog extends Animal {
public Dog(String name, int age, String breed) {
super(name, age, breed);
}
public void makeSound() {
System.out.println("Woof!");
}
}
In this example, we have defined a subclass called Dog
that extends the Animal
class. The subclass has a constructor that calls the constructor of the superclass using the super
keyword. The subclass also overrides the makeSound
method of the superclass to make a different sound for dogs.
Using Constructors and Methods in Inheritance
When a subclass is created, it automatically inherits all the fields and methods of its superclass. This means that you can use the fields and methods of the superclass in the subclass without having to redefine them. For example, you can create an instance of the Dog
class and call the makeSound
method, which was defined in the Animal
class:
Dog myDog = new Dog("Buddy", 3, "Golden Retriever");
myDog.makeSound();
When you run this code, the output will be “Woof!”, which is the sound made by dogs.
You can also use the fields of the superclass in the subclass by using the super
keyword to access them. For example, you can add a method to the Dog
class that returns the age of the dog as well as the age of the dog in dog years:
public int getDogAge() {
int humanYears = super.age;
int dogYears = humanYears * 7;
return dogYears;
}
In this example, we have used the super
keyword to access the age
field of the superclass and then calculated the age of the dog in dog years by multiplying it by 7. You can then call this method on an instance of the Dog
class to get the dog’s age in dog years.
Overriding Methods in a Subclass
When a subclass inherits a method from its superclass, it can override the method to provide a different implementation. This is called method overriding. To override a method in a subclass, you
need to define a method with the same signature as the method in the superclass. For example, you can override the makeSound
method in the Dog
class to make a different sound for puppies:
public class Puppy extends Dog {
public Puppy(String name, int age, String breed) {
super(name, age, breed);
}
public void makeSound() {
System.out.println("Yip!");
}
}
In this example, we have defined a subclass called Puppy
that extends the Dog
class. The subclass overrides the makeSound
method of the Dog
class to make a different sound for puppies.
When you call the makeSound
method on an instance of the Puppy
class, it will output “Yip!”, which is the sound made by puppies. This is an example of polymorphism, which is the ability of objects to take on multiple forms.
Conclusion
Inheritance is a powerful feature of object-oriented programming that allows you to create subclasses that inherit properties and behaviors from a superclass. In Java, you can use the extends
keyword to create a subclass, which can then use the fields and methods of the superclass. You can also override methods in a subclass to provide a different implementation. Understanding inheritance is essential for creating complex software applications that are easy to maintain and extend.