When learning Java, you'll often come across the term "extend" in relation to classes. This keyword is fundamental to object-oriented programming (OOP) and is used to create new classes from existing ones. Knowing how to use "extend" is essential for creating robust, reusable, and maintainable code.

"Extend" is used to inherit properties and methods from an existing class, called the superclass, into a new class, known as the subclass. This concept is a pillar of Java's OOP paradigm and helps in code reuse, making your programs more efficient. Let's dive into the details of "extend" in Java and how you can apply it in your projects.

Creating a Subclass in Java
To create a subclass using the "extend" keyword, you must first define your superclass. The superclass is the parent class that has attributes and methods you want to use in your new class. Here's a simple example:

First, let's create a superclass called Animal:
```java public class Animal { void eat() { System.out.println("eating"); } } ```
Now, we'll create a subclass called Dog that extends the Animal class:

```java public class Dog extends Animal { void bark() { System.out.println("barking"); } } ```
Inheriting Attributes and Methods
In the Dog class, we're inheriting the eat method from the Animal class. This means we can use the eat method in the Dog class without rewriting its code. Here's how you can use it:
```java Dog myDog = new Dog(); myDog.eat(); // prints: eating myDog.bark(); // prints: barking ```
Overriding Superclass Methods

Subclasses can override superclass methods to provide their own implementation. This allows you to customize the behavior of the inherited method for the subclass. Here's an example:
```java public class Dog extends Animal { @Override void eat() { System.out.println("Dog is eating dog food"); } void bark() { System.out.println("barking"); } } ```
Now, when we call the eat method on an instance of the Dog class, it will print "Dog is eating dog food" instead of the original "eating" message.
Constructors and the Superclass

When creating an instance of a subclass, the superclass constructor is called first. You can invoke the superclass constructor explicitly using the super keyword. Let's see an example:
Invoking the Superclass Constructor



















In the following example, the Animal class constructor is called before the Dog class constructor:
```java public class Animal { Animal() { System.out.println("Animal constructor called"); } } public class Dog extends Animal { Dog() { System.out.println("Dog constructor called"); } } ```
When creating a Dog object, you'll see the following output:
``` Animal constructor called Dog constructor called ```
Parameterized Constructors and the Superclass
You can also pass arguments to the superclass constructor using the super keyword. Here's an example:
```java public class Animal { String name; Animal(String name) { this.name = name; System.out.println("Animal constructor called: " + name); } } public class Dog extends Animal { Dog(String name) { super(name); System.out.println("Dog constructor called: " + name); } } ```
Now, when creating a Dog object with a name, you'll see the following output:
``` Animal constructor called: Fido Dog constructor called: Fido ```
Understanding how to use "extend" in Java is crucial for building well-structured, maintainable, and efficient code. It allows you to create reusable classes and customize their behavior as needed. Don't hesitate to experiment with "extend" and apply it in your projects to master this powerful concept in Java.