User Artem Divertitto
Artem Divertitto
Senior Android Developer at United Tech

Java extends Keyword with Examples

Published in the Java Developer group

What is Java extends Keyword?

The extends in Java is a keyword that indicates inheritance between a child and parent class.
Extends In Java is a keyword that is written with the child class during class declaration followed by the name of the parent class. When a child class extends a class it acquires or inherits all the properties of the parent class. The syntax for using it is quite simple. While defining the child class, the extends keyword follows the child class name, followed by the parent class name. It is given as follows.

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

My name is Billy My sound is Bark My no. of legs is 4 My color is Brown My breed is Labrador ------------------ My name is Grace My sound is Bark My no. of legs is 4 My color is Black My breed is Husky ------------------ My name is Hugo My sound is Bark My no. of legs is 4 My color is Gray My breed is Poodle

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.

Conclusion

By the end of this post, we hope you have got yourself familiarized with the extends keyword in Java in detail. You have learned how to use extends in Java with examples. You have also been explained the concepts of the Object-Oriented nature of Java and Inheritance. You can now use inheritance between classes. You can try other real-world examples by creating classes and see how this concept works to understand it in more depth. Keep practicing for a deeper command of the concept. Till then, keep growing and keep shining! Java extends Keyword with Examples - 1
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION