When it comes to leveraging the power of classes in Java, developers often find themselves in need of reusing code or adding functionality to existing classes. This is where inheritance comes into play. But when it comes to implementing or including a class in Java, the question arises: which keyword is used for inheritance?

In Java, the keyword used to inherit a class is 'extends'. This keyword allows a new class to be based on an existing class, inheriting its properties and methods. However, understanding the use of 'extends' is just the tip of the iceberg when it comes to Java inheritance.

Understanding Java Inheritance
Java inheritance is a powerful feature that allows one class to acquire the properties (attributes) and methods of another. It promotes code reusability and establishes a 'is-a' relationship between classes.

Inheritance in Java follows the concept of 'is-a' hierarchy. For example, a 'Dog' is an 'Animal'. Therefore, the 'Dog' class can inherit from the 'Animal' class, acquiring its properties and methods.
Inheritance Syntax

The syntax for inheritance in Java is straightforward. The 'extends' keyword is used to indicate that a class is inheriting from another. Here's a simple example:
public class Animal { // Parent class
public void eat() {
System.out.println("Eating");
}
}
public class Dog extends Animal { // Child class
public void bark() {
System.out.println("Barking");
}
}
Inheritance and Access Modifiers

Inheritance in Java is also influenced by access modifiers. A subclass can only access the public and protected methods and variables of its superclass. Here's an example:
public class Animal { // Parent class
private void eat() {
System.out.println("Eating");
}
}
public class Dog extends Animal { // Child class
public void bark() {
System.out.println("Barking");
}
public void test() {
eat(); // This will give a compilation error
}
}
Inheritance vs Composition

While inheritance is a powerful tool, it's not always the best solution. Composition, where a class contains an instance of another class, can often be a more flexible and maintainable approach.
Composition allows for more flexible relationships between classes. For example, a 'House' can have a 'Door', but it's not accurate to say that a 'House' is a 'Door'. This is where composition comes in handy.




















Composition Syntax
The syntax for composition in Java is simple. An instance of one class is created within another class. Here's an example:
public class Door {
public void open() {
System.out.println("Opening door");
}
}
public class House {
private Door door;
public House() {
this.door = new Door();
}
public void openDoor() {
door.open();
}
}
Composition vs Inheritance
Composition allows for more flexible relationships between classes. It's often a better choice when you want to model 'has-a' relationships, rather than 'is-a' relationships. It also promotes encapsulation and can make your code more maintainable.
In conclusion, understanding when to use inheritance and when to use composition is a crucial aspect of effective Java programming. Both have their uses and understanding their strengths and weaknesses is key to writing maintainable, extensible code.