Master Java Extends w3schools Tutorial Inheritance Object Oriented Programming

Robert Jul 13, 2026

When learning Java, you'll often come across the term "extend" in relation to classes. This keyword is fundamental to object-oriented programming (OOP) and is used to create new classes from existing ones. Knowing how to use "extend" is essential for creating robust, reusable, and maintainable code.

Java Exception Handling Made Simple
Java Exception Handling Made Simple

"Extend" is used to inherit properties and methods from an existing class, called the superclass, into a new class, known as the subclass. This concept is a pillar of Java's OOP paradigm and helps in code reuse, making your programs more efficient. Let's dive into the details of "extend" in Java and how you can apply it in your projects.

β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€

Creating a Subclass in Java

To create a subclass using the "extend" keyword, you must first define your superclass. The superclass is the parent class that has attributes and methods you want to use in your new class. Here's a simple example:

a woman with long black hair sitting in front of a computer
a woman with long black hair sitting in front of a computer

First, let's create a superclass called Animal:

```java public class Animal { void eat() { System.out.println("eating"); } } ```

Now, we'll create a subclass called Dog that extends the Animal class:

Must-Have IntelliJ IDEA Plugins For Java Developers
Must-Have IntelliJ IDEA Plugins For Java Developers

```java public class Dog extends Animal { void bark() { System.out.println("barking"); } } ```

Inheriting Attributes and Methods

In the Dog class, we're inheriting the eat method from the Animal class. This means we can use the eat method in the Dog class without rewriting its code. Here's how you can use it:

```java Dog myDog = new Dog(); myDog.eat(); // prints: eating myDog.bark(); // prints: barking ```

Overriding Superclass Methods

Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide

Subclasses can override superclass methods to provide their own implementation. This allows you to customize the behavior of the inherited method for the subclass. Here's an example:

```java public class Dog extends Animal { @Override void eat() { System.out.println("Dog is eating dog food"); } void bark() { System.out.println("barking"); } } ```

Now, when we call the eat method on an instance of the Dog class, it will print "Dog is eating dog food" instead of the original "eating" message.

Constructors and the Superclass

Top Features of Java Every Beginner Should Know β˜•πŸš€
Top Features of Java Every Beginner Should Know β˜•πŸš€

When creating an instance of a subclass, the superclass constructor is called first. You can invoke the superclass constructor explicitly using the super keyword. Let's see an example:

Invoking the Superclass Constructor

a red poster with the words sintaxis basica de java
a red poster with the words sintaxis basica de java
Java Roadmap for Beginners 2026
Java Roadmap for Beginners 2026
5 Java Concepts Every Developer Must Know πŸ’»β˜•
5 Java Concepts Every Developer Must Know πŸ’»β˜•
an info sheet with different types of web pages
an info sheet with different types of web pages
Best Java IDEs in 2020
Best Java IDEs in 2020
Java Patterns Notes 1
Java Patterns Notes 1
How To Become A Java Full-Stack Developer
How To Become A Java Full-Stack Developer
a computer screen with multiple lines of code on it's side and another line of code on the other side
a computer screen with multiple lines of code on it's side and another line of code on the other side
Java Project Ideas For Beginners
Java Project Ideas For Beginners
Java OOP concepts cheat sheet
Java OOP concepts cheat sheet
60 Java projects ideasπŸ’‘
60 Java projects ideasπŸ’‘
Java Full Stack Developer Course
Java Full Stack Developer Course
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java πŸš€ Intermediate Cheat Sheet You’ll Actually Use
Java Basics Cheat Sheet πŸ“˜βš‘ | Learn Coding Faster & Smarter
Java Basics Cheat Sheet πŸ“˜βš‘ | Learn Coding Faster & Smarter
core java classes in Pune
core java classes in Pune
Java Variables: The Building Blocks of Programming
Java Variables: The Building Blocks of Programming
Java Application Development Company - Codemech Solutions
Java Application Development Company - Codemech Solutions
🌟 β˜•Java User Input Example | Learn Java Programming Easily
🌟 β˜•Java User Input Example | Learn Java Programming Easily
β˜• Java Operators Cheat Sheet | Master Java Operators Easily πŸš€
β˜• Java Operators Cheat Sheet | Master Java Operators Easily πŸš€

In the following example, the Animal class constructor is called before the Dog class constructor:

```java public class Animal { Animal() { System.out.println("Animal constructor called"); } } public class Dog extends Animal { Dog() { System.out.println("Dog constructor called"); } } ```

When creating a Dog object, you'll see the following output:

``` Animal constructor called Dog constructor called ```

Parameterized Constructors and the Superclass

You can also pass arguments to the superclass constructor using the super keyword. Here's an example:

```java public class Animal { String name; Animal(String name) { this.name = name; System.out.println("Animal constructor called: " + name); } } public class Dog extends Animal { Dog(String name) { super(name); System.out.println("Dog constructor called: " + name); } } ```

Now, when creating a Dog object with a name, you'll see the following output:

``` Animal constructor called: Fido Dog constructor called: Fido ```

Understanding how to use "extend" in Java is crucial for building well-structured, maintainable, and efficient code. It allows you to create reusable classes and customize their behavior as needed. Don't hesitate to experiment with "extend" and apply it in your projects to master this powerful concept in Java.