What is Java extends Keyword?
class ParentClass{ ...}
class ChildClass extends ParentClass { ... }
What Inheritance in Java?
To understand the usage of extends keyword in Java, it is first essential to understand the concept of inheritance. Java is an Object-Oriented Programming (OOP) language. OOP is a method to design a program using classes and objects. When dealing with classes and objects, there can be certain relationships between different classes which need to be represented. Inheritance is one such relationship between classes. Inheritance denotes Is-A-Relationship among objects. Inheritance can be defined as the mechanism where one class acquires the properties of another class. The class that inherits is called the child class or the subclass whereas the class which is inherited is called the parent class or the superclass. Extends in Java is the keyword that is used to perform inheritance between classes.Example
Example of Java extends keyword is as follows:
class Animal {
// fields of the parent class
String name;
String sound;
int noOfLegs;
// default constructor of the parent class
public Animal (){}
// parameterized constructor of the parent class
public Animal (String name, String sound, int legs){
this.name = name;
this.sound = sound;
this.noOfLegs = legs;
}
// method of the parent class
public void display() {
System.out.println("My name is " + name);
System.out.println("My sound is " + sound);
System.out.println("My no. of legs is " + noOfLegs);
}
}
// inherit from Animal
class Dog extends Animal {
String color;
String breed;
// new method in subclass
public Dog(String name, String sound ,int legs, String color, String breed){
super(name,sound,legs);
this.color = color;
this.breed = breed;
}
public void display() {
super.display();
System.out.println("My color is " + color);
System.out.println("My breed is " + breed);
}
}
public class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog dog1 = new Dog("Billy","Bark",4,"Brown","Labrador");
dog1.display();
System.out.println("------------------");
Dog dog2 = new Dog("Grace","Bark",4,"Black","Husky");
dog2.display();
System.out.println("------------------");
Dog dog3 = new Dog("Hugo","Bark",4,"Gray","Poodle");
dog3.display();
}
}
Output
Explanation
In the code snippet above, we have explained how inheritance works in Java by using extends keyword. We have two classes declared. First, we have a parent class which is the Animal class. Secondly, we have a child class which is the Dog class. The Dog class extends Animal class. By using this keyword, the Dog class acquires all the properties and methods of the Animal class. The child class can now access and use these properties and does not need to write the code again or declare these properties again. It increases the reusability of code.Method Overriding with Examples
Method overriding occurs when a subclass provides a specific implementation of a method already defined in its superclass. This allows the subclass to customize or enhance the behavior of the inherited method.
Example: Method Overriding
class Animal {
void sound() {
System.out.println("Animals make sounds");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dogs bark");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.sound(); // Outputs: Dogs bark
}
}
Tip: Always use the @Override
annotation when overriding methods. It ensures that the method in the subclass matches the superclass method signature, helping to avoid subtle errors.
The Importance of the super Keyword
The super
keyword is used to call methods or access properties of the superclass. It is particularly useful when the subclass needs to use the superclass implementation of an overridden method.
Example: Using super
in Method Overriding
class Animal {
void sound() {
System.out.println("Animals make sounds");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calls the superclass method
System.out.println("Dogs bark");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
myDog.sound();
// Outputs:
// Animals make sounds
// Dogs bark
}
}
Note: Use super
judiciously to combine superclass behavior with subclass-specific functionality.
Constructor Chaining in Inheritance
Constructor chaining allows a subclass constructor to call a superclass constructor using the super
keyword. This ensures that the superclass is properly initialized before the subclass is constructed.
Example: Constructor Chaining
class Animal {
Animal(String name) {
System.out.println("Animal constructor: " + name);
}
}
class Dog extends Animal {
Dog(String name) {
super(name); // Calls the superclass constructor
System.out.println("Dog constructor: " + name);
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog("Buddy");
// Outputs:
// Animal constructor: Buddy
// Dog constructor: Buddy
}
}
Tip: Always use super()
as the first statement in the subclass constructor to ensure proper initialization.
GO TO FULL VERSION