The extends keyword in Java is used to indicate that a class is inheriting from another class. It establishes an inheritance relationship where a subclass (child class) inherits fields and methods from a superclass (parent class). This mechanism allows for code reuse, method overriding, and polymorphism.
Table of Contents
- Introduction
extendsKeyword Syntax- Understanding
extends - Examples
- Basic Inheritance
- Method Overriding
- Using
superKeyword
- Real-World Use Case
- Conclusion
Introduction
Inheritance is one of the core concepts of Object-Oriented Programming (OOP). It allows a class to inherit properties and behaviors from another class, promoting code reuse and the creation of hierarchical relationships between classes. The extends keyword in Java is used to define such inheritance relationships.
extends Keyword Syntax
The syntax for using the extends keyword is as follows:
class SubClassName extends SuperClassName {
// class body
}
Example:
class Animal {
// fields and methods
}
class Dog extends Animal {
// fields and methods specific to Dog
}
Understanding extends
Key Points:
- Inheritance: The subclass inherits fields and methods from the superclass.
- Code Reuse: Inherited methods and fields can be reused, reducing code duplication.
- Method Overriding: Subclasses can override methods from the superclass to provide specific implementations.
- Polymorphism: Allows a subclass to be treated as an instance of its superclass.
Examples
Basic Inheritance
A simple example demonstrating basic inheritance.
Example
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited method
dog.bark(); // Subclass-specific method
}
}
Output:
This animal eats food.
The dog barks.
Method Overriding
Overriding a method from the superclass in the subclass.
Example
class Animal {
void makeSound() {
System.out.println("This animal makes a sound.");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("The dog barks.");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
myDog.makeSound(); // Calls the overridden method
}
}
Output:
The dog barks.
Using super Keyword
Using the super keyword to access superclass methods and constructors.
Example
class Animal {
String name;
Animal(String name) {
this.name = name;
}
void eat() {
System.out.println(name + " eats food.");
}
}
class Dog extends Animal {
Dog(String name) {
super(name); // Calling superclass constructor
}
void eat() {
super.eat(); // Calling superclass method
System.out.println(name + " also eats dog food.");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy");
dog.eat();
}
}
Output:
Buddy eats food.
Buddy also eats dog food.
Real-World Use Case
GUI Frameworks
In real-world applications, inheritance is commonly used in graphical user interface (GUI) frameworks where custom components extend base classes.
Example
import javax.swing.JButton;
class CustomButton extends JButton {
CustomButton(String label) {
super(label);
// Customizing the button
setFocusPainted(false);
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the custom button
CustomButton button = new CustomButton("Click Me");
System.out.println(button.getText());
}
}
Output:
Click Me
Conclusion
The extends keyword in Java is a fundamental part of inheritance, allowing classes to derive properties and behaviors from other classes. This promotes code reuse, enables method overriding, and supports polymorphism. Understanding and using the extends keyword effectively is crucial for developing robust and maintainable object-oriented Java applications.