In the realm of object-oriented programming, inheritance is a powerful mechanism that allows classes to acquire properties and methods from other classes. In Java, this is achieved using the 'extends' keyword. Let's delve into the world of inheritance in Java, focusing on the keyword used to inherit a class.

Before we dive into the 'extends' keyword, let's briefly understand what inheritance is. Inheritance in Java enables a new class to acquire the properties (attributes) and methods of an existing class. It represents the 'is-a' relationship, meaning a subclass is a type of superclass.

Understanding the 'extends' Keyword
The 'extends' keyword in Java is used to create a subclass from a superclass. It allows the subclass to inherit the properties and methods of the superclass. Here's a basic syntax:

class SubClassName extends SuperClassName { ... }
Inheriting Properties and Methods

When a subclass inherits from a superclass, it automatically gains access to all the properties (variables) and methods of the superclass, unless they are marked as private or final. Here's an example:
class Animal {
String name;
void eat() {
System.out.println("Eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
}
In this example, the 'Dog' class inherits the 'name' variable and the 'eat' method from the 'Animal' class.
Constructors and Inheritance

When a subclass is instantiated, the superclass constructor is called first, followed by the subclass constructor. This is known as constructor chaining. To call the superclass constructor explicitly, use the 'super' keyword. Here's an example:
class Animal {
Animal() {
System.out.println("Animal created");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the superclass constructor
System.out.println("Dog created");
}
}
Overriding Methods
Inheritance also allows for method overriding, where a subclass provides a different implementation of a method that already exists in the superclass. The '@Override' annotation is used to ensure that the method is indeed overriding a superclass method. Here's an example:

class Animal {
void makeSound() {
System.out.println("The animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
System.out.println("The dog barks");
}
}
Method Hiding vs Method Overriding
It's important to note the difference between method hiding and method overriding. Method hiding occurs when a subclass has a method with the same name and parameter list as a method in the superclass, but the superclass method is not overridden. Instead, the subclass method hides the superclass method. Method overriding, on the other hand, provides a new implementation of an existing method in the superclass.




















In conclusion, the 'extends' keyword in Java is a powerful tool for creating inheritance hierarchies. It allows classes to inherit properties and methods from other classes, promoting code reuse and creating a clear, hierarchical structure. Understanding how to use 'extends' and the related concepts of constructor chaining, method overriding, and method hiding is crucial for effective object-oriented programming in Java.