Java W3schools Inheritance Tutorial Master The Basics

Robert Jul 13, 2026

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.

Types of Inheritance in Java: A Comprehensive Guide
Types of Inheritance in Java: A Comprehensive Guide

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.

What is Inheritance in Java – 5 Types, Use and Advantages
What is Inheritance in Java – 5 Types, Use and Advantages

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.

Java Class Inheritance
Java Class Inheritance

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

What is Inheritance in Java with example - Object Oriented Programming Tutorial
What is Inheritance in Java with example - Object Oriented Programming Tutorial

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

Inheritance In Java
Inheritance In Java

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

the different types of children's names and their meanings are shown in this diagram
the different types of children's names and their meanings are shown in this diagram

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:

Inheritance In JAVA
Inheritance In JAVA
Types of Inheritance in Java | OOP Concepts Explained
Types of Inheritance in Java | OOP Concepts Explained
10 Examples of Inheritance in Java 🔗
10 Examples of Inheritance in Java 🔗
Java Inheritance - Learn its Importance and Types with Coding Examples.
Java Inheritance - Learn its Importance and Types with Coding Examples.
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
Inheritance in java
Inheritance in java
Types of Inheritance in Java - Scientech Easy
Types of Inheritance in Java - Scientech Easy
types of inheritance 💪
types of inheritance 💪
5 Benefits of Single Inheritance in Java
5 Benefits of Single Inheritance in Java
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
a diagram with two people connected to one another and the caption that says,'herarchical interface in this type of interaction, more than one sub class is interrupted a single base class
a diagram with two people connected to one another and the caption that says,'herarchical interface in this type of interaction, more than one sub class is interrupted a single base class
Inheritance in Java with Examples - DataFlair
Inheritance in Java with Examples - DataFlair
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Learn Java Collections Fast 🚀 | ArrayList HashMap Set Queue Explained
Top 21 Java Inheritance Interview Questions and Answers
Top 21 Java Inheritance Interview Questions and Answers
Inheritance in Java with Example - Broadly Explained
Inheritance in Java with Example - Broadly Explained
🔥 5 Examples of Inheritance in Java 🔗
🔥 5 Examples of Inheritance in Java 🔗
this is how they regularly enter rooms
this is how they regularly enter rooms
Inheritance in Java OOPs
Inheritance in Java OOPs
a girl with red hair is standing on the sidewalk
a girl with red hair is standing on the sidewalk

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!