Java Class Inheritance Keyword: Extends vs Implements

Benjamin Jul 11, 2026

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?

10 Example of this keyword in Java
10 Example of this keyword in Java

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.

The new keyword in Java programming language - Codeforcoding
The new keyword in Java programming language - Codeforcoding

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.

the key words in this language are used to describe something
the key words in this language are used to describe something

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

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

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 Explained | OOP Concepts for Beginners
Inheritance in Java Explained | OOP Concepts for Beginners

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

Java List of Keywords (All Reserved Words) | Complete Java Reference Guide
Java List of Keywords (All Reserved Words) | Complete Java Reference Guide

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.

Java Programming
Java Programming
Super Keyword in Java: Syntax, Uses and Examples - Scientech Easy
Super Keyword in Java: Syntax, Uses and Examples - Scientech Easy
Comprehensive Guide to Java Keywords
Comprehensive Guide to Java Keywords
10 Examples of Inheritance in Java 🔗
10 Examples of Inheritance in Java 🔗
What is Inheritance in Java – 5 Types, Use and Advantages
What is Inheritance in Java – 5 Types, Use and Advantages
✅ Java Inheritance Example Explained Simply
✅ Java Inheritance Example Explained Simply
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
http://chatgpt.com/g/g-lvtjtj09i-darkhorse-dns
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
30 Days of Java – Day 14 | Java Inheritance Explained for Beginners
the language used in web development
the language used in web development
Keywords in Java - Java Reserved Words - TechVidvan
Keywords in Java - Java Reserved Words - TechVidvan
30 Days of Java – Day 12 | Java Classes and Objects Explained for Beginners
30 Days of Java – Day 12 | Java Classes and Objects Explained for Beginners
Sorting ArrayList in Java using Collections.sort() Method
Sorting ArrayList in Java using Collections.sort() Method
Abstract Class Vs Interface in Java
Abstract Class Vs Interface in Java
Java OOPs Concepts - Object Oriented Programming in Java - TechVidvan
Java OOPs Concepts - Object Oriented Programming in Java - TechVidvan
Java Keywords
Java Keywords
Top Features of Java Every Beginner Should Know ☕🚀
Top Features of Java Every Beginner Should Know ☕🚀
Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners
a poster with the words in red and black, including an image of a man's face
a poster with the words in red and black, including an image of a man's face
📘 Must-Know Java Keywords Explained Simply 🔑
📘 Must-Know Java Keywords Explained Simply 🔑
Java Inheritance - Learn its Importance and Types with Coding Examples.
Java Inheritance - Learn its Importance and Types with Coding Examples.

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.