Java Subclass Declaration: Inheritance Keyword & Syntax

Benjamin Jul 11, 2026

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 in Java
Inheritance in Java

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.

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

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:

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

```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

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

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 ```

Java Class Inheritance
Java Class Inheritance

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"); } } ```

Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan
Java Class and Objects - Easy Learning with Real-life Examples! - TechVidvan

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.

Inheritance in Java Explained | OOP Concepts for Beginners
Inheritance in Java Explained | OOP Concepts for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
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
a diagram showing the different classes in college
a diagram showing the different classes in college
Java Inheritance - Learn its Importance and Types with Coding Examples.
Java Inheritance - Learn its Importance and Types with Coding Examples.
Abstract Class Vs Interface in Java
Abstract Class Vs Interface in Java
Types of Inheritance in Java | OOP Concepts Explained
Types of Inheritance in Java | OOP Concepts Explained
the key words in this language are used to describe something
the key words in this language are used to describe something
10 Examples of Inheritance in Java πŸ”—
10 Examples of Inheritance in Java πŸ”—
three types of variable type in an organization
three types of variable type in an organization
Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually
JAVA MAIN METHOD
JAVA MAIN METHOD
Learn Java in 30 Days Only
Learn Java in 30 Days Only
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
Master JavaScript Variables: A Beginner’s Guide πŸš€
Master JavaScript Variables: A Beginner’s Guide πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€
Static Keyword In Java : All Scenarios
Static Keyword In Java : All Scenarios
Exception Handling in Java | Try Catch Finally Explained with Example
Exception Handling in Java | Try Catch Finally Explained with Example
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
Interface in Java
Interface in Java

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!