CBSE 2026 results are out, Mukul scored a perfect 100/100 in Computer ScienceSee all toppers →

KwickAcademy Java · 8 min · free

Inheritance in Java: extends, super and Method Overriding

8 min5 KwickClipsFull text belowFree
Next lesson →Kajal Ma'am (MCA), teaching since 2004Remembered in this browser

How extends reuses parent code, what a child really inherits, both jobs of super, protected access, overriding, and the types Java allows.

Follows the syllabus of: CBSE Class 10 Computer Applications (165), ICSE Class 9 Computer Applications, ISC Class 12 Computer Science (868), GSEB Std 12 Computer Studies

On screen in this lesson

What is inheritance?

One class gets the fields and methods of another
Parent class: also called superclass or base class
Child class: also called subclass or derived class
Test: a child IS A kind of parent (a Dog is an Animal)

What a child gets

Public and protected members are always inherited
Default members: only inside the same package
Private members are not accessible in the child
Constructors are never inherited
Every class finally extends the Object class

Pause and predict

What if Student() does not call super("Riya")?
Java silently adds super() with no arguments
Person has no constructor without arguments
Result: a compile-time error

Access modifiers

ModifierWho can use itIn a child class?
privatesame class onlyno
default (none)same packagesame package only
protectedpackage + subclassyes, always
publiceveryoneyes

Method overriding

The child writes a method with the same name and parameters
The child's version replaces the parent's for child objects
Write @Override above it so the compiler checks it
Access cannot be made weaker than the parent's
private, static and final methods cannot be overridden

Overloading vs overriding

PointOverloadingOverriding
Wheresame classparent and child
Parametersmust differmust be the same
Decidedat compile timeat run time

Quick answers

Why does removing super("Riya") break the program?

Java adds super() instead, and the parent has no no-argument constructor, so it will not compile.

Can a Java class have two parent classes?

No. Multiple inheritance is possible only through interfaces.

KwickClips from this lesson

Short clips, one idea each. Good for revision the night before.

The full lesson, in text

Hello students, welcome to Kwickprep. You got your surname from your parents without asking for it. Can a Java class also get code from a parent class, without writing it again? Yes. Today we learn extends, super, protected, method overriding, and the types of inheritance Java supports.

Inheritance means one class receives the fields and methods of another class. A field is a variable inside a class, and a method is a function inside a class. The class that gives is the parent, also called the superclass or base class. The class that receives is the child, also called the subclass or derived class. Use inheritance only when the child is a kind of the parent. A dog is an animal, so that fits.

The keyword extends creates a child class. Student extends Person, and the Student class body is completely empty. Still, in main we make a Student object and call hi on it. It works, because Student inherited the name field and the hi method from Person. So it prints Riya. Main sits inside Person here only to keep the program short.

Let us be exact about what a child gets. Public and protected members of the parent are always inherited. Default members, with no modifier, are inherited only inside the same package. Private members stay hidden, so the child cannot use them directly. Constructors are never inherited, because a constructor belongs to its own class. And if you write no extends at all, the class still extends Java's built-in Object class.

A constructor is a special method that runs when an object is created. When we create a Student, the Student constructor runs. Its first line, super with Riya, calls the Person constructor and passes Riya to it. So the parent part of the object is set up first, and it prints Name: Riya. Remember, the call to super must be the very first statement in the constructor.

Pause and predict. What happens if we delete super Riya from the Student constructor? Java quietly adds a call to super with empty brackets. But Person has no constructor that takes no arguments. So the program does not compile at all. That is why the parent constructor call matters.

The word super has a second use. Super dot a method name calls the parent's version of that method. Car has an info method that returns four wheels. The class E v, short for electric vehicle, extends Car and writes its own info method. Inside it, super dot info gets four wheels from Car, and then adds battery. So the child adds to the parent's work, instead of copying it.

Access modifiers decide who can use a field or method. Private means only the same class can use it, so a child cannot. With no modifier, called default access, any class in the same package can use it. Protected allows the same package, and also child classes in any package. Public allows every class everywhere.

Here Bank has a protected field called bal, short for balance. Acc, short for account, extends Bank. The show method in Acc uses bal directly, and it prints five hundred. Protected is useful when data should be hidden from the outside world, but shared with child classes. If bal were private, this program would give a compile-time error.

Method overriding means the child gives its own version of a parent method. The method must have the same name and the same parameters as in the parent. For a child object, the child's version runs instead of the parent's. Writing at Override above the method asks the compiler to check that it really overrides. The child cannot make the method less accessible, for example public to private. Private, static and final methods cannot be overridden.

Bird has a cry method that returns Chirp. Crow extends Bird and overrides cry to return Caw. Pause and predict. The variable B has the type Bird, but the object is a Crow. What prints? It prints Caw, because Java looks at the actual object when the program runs. This is called runtime polymorphism.

Exams often ask you to tell overloading and overriding apart. Overloading happens in the same class, while overriding needs a parent and a child. Overloaded methods must have different parameters, but an overriding method keeps the same parameters. Java picks an overloaded method when compiling, but picks an overridden method while the program runs.

Now the types of inheritance. In single inheritance, one child extends one parent. In multilevel inheritance, C extends B, and B extends A, like grandchild, parent and grandparent. In hierarchical inheritance, many children, B and C, extend the same parent A. Multiple inheritance means one class with two parents, and Java does not allow it with classes. Hybrid inheritance mixes types, and it is possible only with interfaces.

Here is the rule in code. C extends A comma B gives a compile-time error, because a class can have only one parent. If both parents had a method with the same name, Java would not know which one to use. An interface is a list of methods that a class promises to write. A class can implement many interfaces, so D implements both P and Q.

Let us revise. The keyword extends makes a child class that reuses the parent's code. Super with brackets calls the parent constructor, and it must be the first line. Protected members can be used in the same package and in every child class. Overriding keeps the same name and parameters, and the child's version runs. And with classes, Java supports single, multilevel and hierarchical inheritance, but not multiple.

Courses that teach this

CourseUnit
CBSE Class 10 Computer Applications (165)HTML
ICSE Class 9 Computer ApplicationsIntroduction to Object Oriented Programming Concepts
ISC Class 12 Computer Science (868)Inheritance, Interfaces and Polymorphism
GSEB Std 12 Computer StudiesObject-Oriented Concepts and Java Basics
Programming All levels JavaInheritance and Polymorphism

Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.

Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

Want a plan that actually fits your board dates?

Ask Kajal Ma'am directly, 20+ years teaching computer science. Free demo class first, no payment.

Talk to Kajal Ma'am on WhatsApp

Or see the Class 12 Computer Science course →

Studying outside India?

We coach CBSE, IGCSE & international students across the globe, one-to-one, in your local time zone.

Visit International →