Inheritance is a fundamental concept in object-oriented programming, allowing classes to acquire properties and behaviors 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 'extends' keyword and its role in class inheritance.

Before we dive into the 'extends' keyword, let's briefly understand what inheritance is. Inheritance enables a new class to acquire the properties and methods of an existing class. The class whose properties or methods are inherited is known as the superclass, and the class that inherits those properties or methods is known as the subclass.

Understanding the 'extends' Keyword
The 'extends' keyword in Java is used to indicate that a class is a subclass of another class. It allows the subclass to inherit the fields and methods of the superclass, and can also override or extend the behavior of the superclass methods.

Here's a simple example illustrating the use of the 'extends' keyword:
```java public class Animal { public void eat() { System.out.println("Eating"); } } public class Dog extends Animal { public void bark() { System.out.println("Barking"); } } ```
Inheriting Fields and Methods

In the above example, the 'Dog' class is a subclass of the 'Animal' class. This means that the 'Dog' class inherits the 'eat' method from the 'Animal' class. So, an instance of the 'Dog' class can call the 'eat' method.
Here's how you can create an instance of the 'Dog' class and call its inherited method:
```java Dog myDog = new Dog(); myDog.eat(); // Outputs: Eating ```
Overriding Methods

Subclasses can override the behavior of inherited methods using the '@Override' annotation. This allows the subclass to provide its own implementation of the method while keeping the same method signature.
Here's an example of overriding the 'eat' method in the 'Dog' class:
```java public class Dog extends Animal { @Override public void eat() { System.out.println("Dog is eating"); } public void bark() { System.out.println("Barking"); } } ```
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.
Here's an example illustrating constructor chaining:




















```java public class Animal { public Animal() { System.out.println("Animal created"); } } public class Dog extends Animal { public Dog() { System.out.println("Dog created"); } } ```
Calling Superclass Constructors
Subclasses can explicitly call the superclass constructor using the 'super' keyword. This can be useful when you want to perform some initialization in the superclass constructor.
Here's an example of calling the superclass constructor from the subclass:
```java public class Animal { public Animal(String name) { System.out.println("Animal " + name + " created"); } } public class Dog extends Animal { public Dog(String name) { super(name); System.out.println("Dog " + name + " created"); } } ```
In conclusion, the 'extends' keyword in Java is a powerful tool for achieving code reuse and establishing 'is-a' relationships between classes. It enables subclasses to inherit fields and methods from superclasses, override their behavior, and call superclass constructors. Understanding and mastering the 'extends' keyword is crucial for any Java developer looking to write maintainable and extensible code.