Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

Java
  • Install Java 25 (JDK) and IntelliJ IDEA, Then Run Your First Program
  • Use Claude Agent, Copilot or Junie Inside IntelliJ IDEA
  • Java Hello World with Maven: What the JDK Does and What Maven Adds
  • Java Variables, Keywords and Naming Rules
  • Maven in IntelliJ: pom.xml, Dependencies and the Build Lifecycle
  • Java Primitive Types and Memory: What int x = 5 Really Does
  • Java Strings and Text Blocks: Quotes, Concatenation and parseInt
  • Java Operators: Arithmetic, a++ vs ++a, Precedence, && || and the Ternary
  • Java Type Casting: Widening, Narrowing and Converting Text to Numbers
  • Java Practice Lab: A Cafe Bill with Variables, Operators and Casting
  • Java if, else and switch: Braces, Else-If Ladders and the Modern Switch
  • Java Loops: for, while, do-while and for-each
  • Java break, continue and Nested Loops
  • What's New in Java 21 to 25: Compact Source Files and Sequenced Collections
  • Java Arrays: Indexes, Default Values, Loops and 2D Grids
  • Java Methods and Overloading: Parameters, return and Pass-by-Value
  • Java Scope, Code Blocks and the Call Stack
  • Read User Input in Java with Scanner (and the Leftover Enter Trap)
  • Java Practice Lab: A Gym Weekly Tracker with Loops, Arrays and Methods
  • Debugging Java in IntelliJ: Stack Traces, Breakpoints and a Better AI Prompt
  • Java var: Let Java Infer the Type
  • Push and Pull in IntelliJ: Two Developers, One GitHub Repository, a Rejected Push and a Conflict
  • Java Classes and Objects: One Blueprint, Many Cars
  • Java Constructors and the this Keyword
  • Java Instance Methods, Getters, Setters and toString
  • Java static: Fields, Methods and Blocks Shared by the Whole Class
  • Java Packages and Imports: Every Class Has an Address
  • Java Access Modifiers and Encapsulation: Private Data, Public Methods
  • Java final and Immutable Objects: Assign Once, Share Safely
  • Java Strings as Objects: The String Pool, == vs equals, StringBuilder and printf
  • Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
  • Java Inheritance: extends, super and the Order Objects Are Built
  • Java Method Overriding, @Override and the Object Class
  • Java Polymorphism: Upcasting, Dynamic Dispatch and Downcasting
  • Java Composition: Has-A vs Is-A
  • Java Abstract Classes: A Parent That Leaves One Blank
  • Java Interfaces: Contracts and Multiple Inheritance
  • Java Nested, Inner and Anonymous Classes
  • Java Enums with Fields, Methods and switch
  • Java Practice Lab: Payment Processing with an Interface, an Abstract Class and an Enum
  • Java Records: A Whole Data Class in One Line
  • Java Sealed Classes and Interfaces
  • Java Pattern Matching with instanceof and switch
  • Java Record Patterns: Unpacking a Record
  • Java equals and hashCode: Why a HashSet Needs Both
  • From Arrays to Collections: The Java Collections Framework Map
  • Java Wrapper Classes and Autoboxing
  • Java ArrayList: The Stretchy Array
  • Java LinkedList vs ArrayList: Nodes or an Array
  • Java Sets: HashSet, LinkedHashSet and TreeSet
  • Java Sorting with Comparable and Comparator
  • Java Maps: HashMap, LinkedHashMap and TreeMap
  • Java Stacks, Queues and Deques: ArrayDeque and PriorityQueue
  • Choosing a Java Collection: Immutable and Sequenced Collections
  • Java Exception Handling: try, catch and finally
  • Java throw, throws, Checked vs Unchecked and Custom Exceptions
  • Java try-with-resources and Clean Error Handling
  • Java Generics: Type-Safe Containers
  • Java Generic Methods, Bounded Types and Wildcards
  • Java Lambdas and Functional Interfaces
  • Java Built-in Functional Interfaces: Predicate, Function, Consumer, Supplier
  • Java Optional: Saying Maybe Without null
  • Java Streams I: filter, map and reduce
  • Java Streams II: Collectors, groupingBy and flatMap
  • Why Concurrency: Processes, Threads and the Main Thread in Java
  • Creating Threads in Java: Thread, Runnable and Lambdas
  • Java Thread Lifecycle: sleep, join and interrupt
  • Java Race Conditions and synchronized
  • Java volatile and Atomic Variables
  • Java ExecutorService and Thread Pools
  • Java Callable, Future and CompletableFuture
  • Java Locks, Deadlock and tryLock
  • Java Concurrent Collections and Virtual Threads
  • Why Slow Work Belongs off the Request Thread: 202 Accepted and a Job Id in Spring Boot
  • Virtual Threads in Spring Boot: One Property, and the Connection Pool Trap
  • @Async, Named TaskExecutors and Structured Concurrency in Spring Boot
  • Java Files and Directories with NIO.2: Path and Files
  • Reading and Writing Text Files in Java
  • Java Structured Data: Reading and Writing JSON and CSV
  1. Blog
  2. /
  3. Java
  4. /
  5. Object-Oriented Programming

Java Inheritance: extends, super and the Order Objects Are Built

Managers and developers are both employees, so name, salary and work() live once in Employee. extends hands them down, super(name, salary) builds the parent part first, and a Director prints three build lines in order.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026Object-Oriented Programming

What you will learn

  • Put shared fields and methods in a parent class and extend it
  • Call the parent's constructor with super(...) as the first statement
  • Explain why parent parts are built first, level by level
  • Name single, multilevel and hierarchical inheritance and know a class has one parent

Before you start

  • Java constructors and the this keyword
  • Java access modifiers and encapsulation

A manager and a developer are both employees. They each have a name, a salary and some work they do. Write those in both classes and you copy the same lines twice. Inheritance lets you write the shared part once, in a parent class, and every child class gets it for free. Each child keeps only what's new: a manager has a team size, a developer has a programming language.

Write the parent once

package org.studyeasy.s03l10;

// The parent: what every employee has, written once
public class Employee {
    protected String name;      // protected: child classes can use it directly
    protected double salary;

    public Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
        System.out.println("-> Employee part built");
    }

    public String work() {
        return name + " is working";
    }
}

protected lets child classes use the fields directly (see the access table in encapsulation). The constructor prints a line so you can watch when it runs.

Add only what is new

package org.studyeasy.s03l10;

// A Manager is an Employee, plus a team
public class Manager extends Employee {
    protected int teamSize;

    public Manager(String name, double salary, int teamSize) {
        super(name, salary);        // let the parent build its part
        this.teamSize = teamSize;   // then add what is new
        System.out.println("-> Manager part built");
    }

    public String holdMeeting() {
        return name + " meets a team of " + teamSize;
    }
}

extends Employee makes Employee the parent. A Manager is an Employee, so it gets every field and method from it, and adds teamSize. super(name, salary) calls the parent's constructor so Employee builds its part first. It must be the first statement; move it below another line and javac says "call to super must be first statement in constructor". Delete it and you get "constructor Employee cannot be applied to given types", because Employee has no empty constructor.

package org.studyeasy.s03l10;

public class Ex01_InheritedMembers {
    public static void main(String[] args) {
        Manager emma = new Manager("Emma", 95000, 6);
        // Manager never declared name or salary: it inherited them
        System.out.println("Name:   " + emma.name);
        System.out.println("Salary: " + emma.salary);
        System.out.println(emma.work());          // Employee's method
        System.out.println(emma.holdMeeting());   // Manager's own
    }
}
-> Employee part built
-> Manager part built
Name:   Emma
Salary: 95000.0
Emma is working
Emma meets a team of 6
The variable emma points to one object with two parts: an Employee part with name, salary and work(), built first, and a Manager part with teamSize and holdMeeting(), built second. Calling work() looks in the Manager part first, does not find it, and uses the Employee part's method. emma Employee part (1st) name, salary, work() Manager part (2nd) teamSize, holdMeeting() emma.work(): not in Manager, found in Employee
One new, one object, two parts. The parent part is built first.

Notice the two build lines: the Employee part first, then the Manager part. name and salary were never declared in Manager, yet they work. When you call work(), Java looks in Manager, doesn't find it, and goes up to the parent.

A child can have a child

package org.studyeasy.s03l10;

// Multilevel: Director extends Manager extends Employee
public class Director extends Manager {
    private String department;

    public Director(String name, double salary, int teamSize, String department) {
        super(name, salary, teamSize);   // Manager builds first
        this.department = department;
        System.out.println("-> Director part built");
    }

    public String approveBudget() {
        return name + " approves the " + department + " budget";
    }
}
package org.studyeasy.s03l10;

public class Ex02_ConstructorOrder {
    public static void main(String[] args) {
        System.out.println("Hiring a director...");
        Director priya = new Director("Priya", 150000, 40, "Sales");
        // three levels, one object: parent parts are built first
        System.out.println(priya.work());            // from Employee
        System.out.println(priya.holdMeeting());     // from Manager
        System.out.println(priya.approveBudget());   // its own
    }
}
Hiring a director...
-> Employee part built
-> Manager part built
-> Director part built
Priya is working
Priya meets a team of 40
Priya approves the Sales budget

super goes one step up: Director's constructor calls Manager's, which calls Employee's. So the build lines come out top-down. Priya is a director, a manager and an employee, and uses a method from every level.

Two children, one parent

package org.studyeasy.s03l10;

// Hierarchical: Developer and Manager share one parent
public class Developer extends Employee {
    private String language;

    public Developer(String name, double salary, String language) {
        super(name, salary);
        this.language = language;
        System.out.println("-> Developer part built");
    }

    public String writeCode() {
        return name + " writes " + language;
    }
}
Developer lucas = new Developer("Lucas", 80000, "Java");
Manager emma = new Manager("Emma", 95000, 6);
System.out.println(lucas.work());
System.out.println(emma.work());
System.out.println(lucas.writeCode());
System.out.println(emma.holdMeeting());
-> Employee part built
-> Developer part built
-> Employee part built
-> Manager part built
Lucas is working
Emma is working
Lucas writes Java
Emma meets a team of 6

Both share work(), written once in Employee. Each has its own method too, and siblings don't share those: lucas.holdMeeting() is "cannot find symbol". Make name private in Employee and writeCode() breaks: "name has private access in Employee".

Kinds of inheritance

ShapeExample
SingleManager extends Employee
MultilevelDirector extends Manager extends Employee
HierarchicalManager and Developer both extend Employee

In every case a class extends only one parent. (Multiple contracts come with interfaces.)

Recap

  • extends makes a child class; use it for an is-a relationship.
  • super(...) calls the parent's constructor, and it comes first.
  • protected fields are usable in child classes; private ones are not.
  • One new builds one object: parent part first, then the child part.

A child can also replace a parent's method with its own version. That's method overriding, next.

Keep going in Java · 32 of 79

Previous
Java Practice Lab: A Library Book Catalogue with final Fields and StringBuilder
Next post
Java Method Overriding, @Override and the Object Class
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Related posts

Java Classes and Objects: One Blueprint, Many Cars

Six loose variables for two cars doesn't scale. A Car class is the blueprint; new builds each car with its own brand, speed and fuel. Plus field defaults, and why two variables can point at one car.

Java Constructors and the this Keyword

A constructor decides how an object is born: new Car("Toyota", 42.5) arrives filled in. this.brand vs brand, a second constructor that calls this(brand, 50.0), and why new Car() stops compiling once you write your own.

Java Instance Methods, Getters, Setters and toString

Give a Car class behaviour: accelerate() changes only this car's speed, private fields are reached through getSpeed and setSpeed, and toString turns Car@1b6d3586 into 'Toyota doing 60 km/h'.

Java static: Fields, Methods and Blocks Shared by the Whole Class

A static field lives once on the class: every Car ticks the same carsBuilt counter while keeping its own brand. Static methods need no object, a static block runs before main, and main is static so the JVM can start with no object.

On this page

  • Write the parent once
  • Add only what is new
  • A child can have a child
  • Two children, one parent
  • Kinds of inheritance
  • Recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

Related course on Udemy

FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE

Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.