In the realm of object-oriented programming, Java provides a robust mechanism for creating new classes that can inherit and extend the behavior of existing ones. This process is known as inheritance, and it's a fundamental aspect of Java's design. But before we dive into the specifics, let's understand the basics.

Inheritance allows us to create a new class, called a subclass or derived class, which can reuse, extend, and modify the behavior defined in another class, known as the superclass or base class. This not only promotes code reuse but also establishes a 'is-a' relationship between the two classes, making the code more organized and maintainable.

Declaring a Subclass in Java
To declare a subclass in Java, you need to use the 'extends' keyword. This keyword indicates that the new class is a subclass of the specified superclass. Here's a simple example:

```java public class Animal { void eat() { System.out.println("Eating"); } } public class Dog extends Animal { void bark() { System.out.println("Barking"); } } ```
Using the 'extends' Keyword

The 'extends' keyword is used after the class declaration and before the class name. In the example above, 'Dog' is a subclass of 'Animal', and it inherits the 'eat' method from the 'Animal' class. Additionally, it has its own 'bark' method.
Here's how you can create an object of the 'Dog' class and call its methods:
```java Dog myDog = new Dog(); myDog.eat(); // Outputs: Eating myDog.bark(); // Outputs: Barking ```

Inheriting Constructors and Fields
In addition to methods, Java also allows subclasses to inherit constructors and fields from their superclass. However, it's important to note that constructors are not inherited, but they can be invoked using the 'super' keyword. Here's an example:
```java public class Animal { String name; Animal(String name) { this.name = name; } void eat() { System.out.println(name + " is eating"); } } public class Dog extends Animal { Dog(String name) { super(name); // Invoking the superclass constructor } void bark() { System.out.println(name + " is barking"); } } ```

Keyword for Inheritance in Java
As we've seen, the keyword used for inheritance in Java is 'extends'. This keyword is used to establish the 'is-a' relationship between a subclass and its superclass. It's important to note that Java supports single inheritance, meaning a class can only extend one superclass.




















However, Java also supports multiple inheritance through interfaces. Interfaces allow a class to implement multiple 'is-a' relationships, providing a way to achieve multiple inheritance in a controlled manner.
Using the 'implements' Keyword
To implement an interface in Java, you use the 'implements' keyword. This keyword is used after the class declaration and before the class name. Here's an example:
```java interface CanFly { void fly(); } interface CanSwim { void swim(); } public class Duck implements CanFly, CanSwim { public void fly() { System.out.println("Flying"); } public void swim() { System.out.println("Swimming"); } } ```
In this example, the 'Duck' class implements both the 'CanFly' and 'CanSwim' interfaces, inheriting their methods. This allows 'Duck' to have an 'is-a' relationship with both 'CanFly' and 'CanSwim'.
In conclusion, understanding and effectively using inheritance in Java is crucial for writing efficient, maintainable, and organized code. The 'extends' keyword is used to declare a subclass and establish an 'is-a' relationship with its superclass, while the 'implements' keyword is used to implement interfaces and achieve multiple inheritance. By mastering these concepts, you'll be well on your way to becoming a proficient Java developer. So, keep practicing and exploring the vast world of Java!