Discover the power of Java inheritance, a fundamental concept in object-oriented programming, with comprehensive, step-by-step guidance at W3Schools. Dive in to understand how inheritance enables code reuse, promotes polymorphism, and enhances your coding efficiency.

Inheritance in Java allows one class to acquire the properties and methods of another. By mastering this concept, you'll expand your Java skillset and improve your ability to build scalable, maintainable code. Now, let's explore inheritance in detail, backed by clear examples and practical tips from W3Schools.

Understanding Inheritance in Java
Inheritance in Java is a way to achieve code reusability by allowing a new class to use the attributes and methods of an existing class. The new class is known as the derived class, while the existing class is the base class.

This mechanism promotes the concept of "is-a" relationship where the derived class is a specialized type of the base class. For instance, a 'Dog' is an 'Animal' – a clear example of inheritance in action.
Creating Inheritance Relationships

To establish an inheritance relationship in Java, you simply declare the base class name within the derived class definition, followed by the 'extends' keyword. 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"); } } ```
Accessing Base Class Properties

Once the inheritance relationship is established, the derived class can access the properties and methods of the base class. Here's how you can access the 'eat' method from the 'Dog' class:
```java Animal myAnimal = new Animal(); Animal myDog = new Dog(); myAnimal.eat(); // Output: eating myDog.eat(); // Output: eating (from base class Animal) myDog.bark(); // Output: barking (accessing inherited method) ```
Inheritance and Polymorphism

Inheritance in Java also enables polymorphism, which allows objects to exhibit different behaviors based on their class. With inheritance, the base class reference can hold an object of the derived class, leading to dynamic method dispatch.
W3Schools provides a fantastic real-life example using the shapes' area calculation. Let's explore this to understand polymorphism:



















Base Class and Derived Class Example
In this example, 'Shape' is the base class, while 'Circle' and 'Rectangle' are derived classes.
```java public class Shape { void area() { } } public class Circle extends Shape { double radius; Circle(double r) { radius = r; } void area() { System.out.println("Area of Circle is " + Math.PI * radius * radius); } } public class Rectangle extends Shape { double width, height; Rectangle(double w, double h) { width = w; height = h; } void area() { System.out.println("Area of Rectangle is " + width * height); } } ```
Polymorphism in Action
Now let's see polymorphism in action, using the base class reference to call the derived classes' methods:
```java Shape s1 = new Circle(5.0); Shape s2 = new Rectangle(4.0, 5.0); s1.area(); // Output: Area of Circle is 78.53981633974483 s2.area(); // Output: Area of Rectangle is 20.0 ```
With this understanding of inheritance and polymorphism, you're well on your way to becoming a proficient Java programmer. Now, start practicing and applying these concepts in your projects. Happy coding!