1. Overview
In this tutorial, we'll learn how to use extends keyword in java with examples.
Extends keyword is used only in the inheritance chain in java and it cannot be used in any other places.
In Java, Interfaces and classes can use extends keyword.
2. What is extends keyword?
Extends keyword is one of the reserved words in java and this can not be used as an identifier.
extends is used to define the inheritance relationship between the classes. Inheritance is one of the Object-oriented programming concepts which is used to define the relationship among the classes to acquire the properties of the other class.
Java extends keyword indicates that child class gets all properties and methods of the parent class. So, the child class has the access to all methods and instance variables from the parent class.
Sometimes parent class can be called superclass or base class.
Java extends keyword is used to define the IS-A relationship in java.
3. Java Extends on Class With Examples
Always class name must be provided after extends keyword when extends is used on the class.
Below example, Car is a superclass and HondaCar is the subclass. This child-parent relation is established with the help of extends keyword.
Example
package com.javaprogramto.keywords.extend; public class ClasesExtendsExamples { public static void main(String[] args) { HondaCar hondaCar = new HondaCar(); int noOfWheels = hondaCar.getNoOfWheels(); System.out.println("Honda car wheels : " + noOfWheels); } } class Car { int noOfWheels = 4; } class HondaCar extends Car { public int getNoOfWheels() { return this.noOfWheels; } }
Output
Honda car wheels : 4
From the above example, noOfWheels is declared and initialized in the parent class and could access the same variable from the child class getNoOfWheels() method.
So, extends keyword could create the connection between Car and HondaCar classes.
A class cannot extend more than one class in java that indicates java does not support multiple inheritances.
Hence, we can not provide multiple class names after extends keyword. If we use it so, it will give compile time error.
class HybridCar extends Car , HondaCar{ }
The above code does not work.
But, Java provides support for the hierarchical inheritance. So that multiple classes can extends the same class.
class Car { int noOfWheels = 4; } class HondaCar extends Car { } class SkodaCar extends Car { }
4. Java Extends on Interfaces With Examples
Interfaces also can use the extends keyword.
Java extends keyword on interfaces provides the extension to the parent interface.
This concept works as similar to the class extends another class.
One interface can use the extends keyword to inherit another interface.
But one class can not extend the interface by using extends keyword.
interface Payment { boolean processPayment(); } interface CreditCardPayment extends Payment{ void validateCardDetails(); } interface UPIPayment extends Payment{ void validateUPIAddress(); }
5. Extending Final Classes In Java
As we have seen in the above sections, always we have used extends keyword on non-final classes.
But we can use the final non-access modifier on the classes.
If a class is declared as final then we can not extend or inherit it using extends keyword.
If we attempt to create the child classes to the final class then it will give the compile time error.
We can declare the class as final when we do not want other classes to inherit its properties.
Example
final class Calculator { public int sum(int a, int b) { return a + b; } public int substract(int a, int b) { return a - b; } public int multiply(int a, int b) { return a * b; } public int divide(int a, int b) { return a / b; } public int reminder(int a, int b) { return a % b; } } class MyCalculator extends Calendar{ }
Output
The type MyCalculator must implement the inherited abstract method Calendar.add(int, int)
6. Conclusion
In this article, we've seen how to use extends keyword with classes and interfaces in java along with examples.
No comments:
Post a Comment
Please do not add any spam links in the comments section.