Comprehensive Study on Inheritance and the super Keyword in Java (Basics to Advanced)

navindecr23 4 views 35 slides Oct 23, 2025
Slide 1
Slide 1 of 35
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35

About This Presentation

This presentation provides a clear and detailed explanation of Inheritance in Java, one of the most fundamental concepts in Object-Oriented Programming (OOP).

It covers everything from the basics of inheritance to advanced topics such as constructor chaining, method overriding, use of the super key...


Slide Content

INHERITANCE
IN JAVA PROGRAMMINGDone By NAVIN PRASATH R

TABLE OF
CONTENTS
What is
inheritance?
1
Why we use?
2
Types of
Inheritance
3
4
Super keyword
5
Debugging
6
FAQs

WHAT IS
INHERITANCE?
Definition:
Inheritance is a mechanism in Java where one class (child/subclass/)
acquires the properties and behaviors (fields & methods) of another class
(parent/superclass/Baseclass).
We use “extends” keyword
Child Class Parent Class
C Extends P
class Parent {
// parent class code
}
class Child extends Parent {
// child class code
}

Code Reusability: Avoid rewriting common logic.
Readability & Maintenance: Easier to update and manage.
Extensibility: Build on existing classes.
Polymorphism Support: Enables method overriding and dynamic method
dispatch.
WHY WE USE
INHERITANCE?
Inherits
Eg: Sound()
The child class cat Inherits the functions
of the parent class Animal.
Such as sound(), eat(),walk() etc..,

Single Inheritance
Multi-level Inheritance
Hierarchical inheritance
Hybrid Inheritance
Multiple inheritance
TYPES OF
INHERITANCE

It inherites only one parent’s
class properties
SINGLE
class Animal {
void eat() {
System.out.println("Eating...");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
OUTPUT:
Eating...
Barking...

A class inherits from a class
that itself is a child
MULTI-LEVEL
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking..."); }
}
class Puppy extends Dog {
void weep() { System.out.println("Weeping..."); }
}
public class Main {
public static void main(String[] args) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.weep();
}
}
OUTPUT:
Eating...
Barking...
Weeping...

Multiple classes inherit from a
single parent
HIERARCHICAL
class Animal {
void eat() { System.out.println("Eating..."); }
}
class Dog extends Animal {
void bark() { System.out.println("Barking..."); }
}
class Cat extends Animal {
void meow() { System.out.println("Meowing..."); }
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
d.eat(); d.bark();
c.eat(); c.meow();
}
}
OUTPUT:
Eating...
Barking...
Eating...
Meowing...

It is a mix of two or more of the
above types of inheritance.
In Java, we can achieve hybrid
inheritance only through
Interfaces.
HYBRID

In Multiple inheritances, one
class can have more than one
super class and inherit
features from all parent
classes and achieved only
through interfaces.
MULTIPLE

super is a reference variable in Java that refers to the immediate parent
class object.
When you create a subclass object, Java internally creates the parent part
first —and super gives you access to that parent part.
SUPER KEYWORD
Why do we use super?
To call parent class methods - When child overrides a method and you still
need parent version Eg: super.methodName();
To access parent class variables - When child hides a variable with the
same name Eg: super.variableName
To call parent class constructor - Access parent constructor from child
class Eg: super(); or super(parameters);

class Animal{
void eat(){
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void eat(){
System.out.println("Dog is eating");
}
void display() {
super.eat();
eat();
}
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.display();
}
}
class Parent {
int value = 100;
}
class Child extends Parent {
int value = 200;
void showValues() {
System.out.println("Child value = " + value);
System.out.println("Parent value = " + super.value);
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.showValues();
}
}

OUTPUT
Animal is eating
Dog is eating
class Parent {
int value = 100;
}
class Child extends Parent {
int value = 200;
void showValues() {
System.out.println("Child value = " + value);
System.out.println("Parent value = " + super.value);
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.showValues();
}
}

OUTPUT
Animal is eating
Dog is eating
OUTPUT
Child value = 200
Parent value = 100

class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("Dog constructor");
}
}
public class Main {
public static void main(String[] args) {
new Dog();
}
}
class Vehicle {
Vehicle(String type) {
System.out.println("Vehicle type: " + type);
}
}
class Car extends Vehicle {
Car() {
super("Car");
System.out.println("Car object created");
}
}
public class Main {
public static void main(String[] args) {
new Car();
}
}

class Vehicle {
Vehicle(String type) {
System.out.println("Vehicle type: " + type);
}
}
class Car extends Vehicle {
Car() {
super("Car");
System.out.println("Car object created");
}
}
public class Main {
public static void main(String[] args) {
new Car();
}
}
OUTPUT
Animal constructor
Dog constructor

OUTPUT
Animal constructor
Dog constructor
OUTPUT
Vehicle type: Car
Car object created

class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog {
void bark() { System.out.println("Barking"); }
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.sound();
}
}
DEBUGGING
class A {
private int num = 10;
}
class B extends A {
void show() { System.out.println(num); }
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.show();
}
}
class A {
final void show() { System.out.println("Final show"); }
}
class B extends A {
void show() { System.out.println("Override show"); }
}
class A {
void display() { System.out.println("A"); }
}
class B extends A { }
class C extends B { }
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.display();
}
}

Error Happens Because the
extends keyword is missing
DEBUGGING
class A {
private int num = 10;
}
class B extends A {
void show() { System.out.println(num); }
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.show();
}
}
class A {
final void show() { System.out.println("Final show"); }
}
class B extends A {
void show() { System.out.println("Override show"); }
}
class A {
void display() { System.out.println("A"); }
}
class B extends A { }
class C extends B { }
public class Main {
public static void main(String[] args) {
C obj = new C();
obj.display();
}
}

Error Happens Because the
extends keyword is missing
DEBUGGING
class A {
private int num = 10;
}
class B extends A {
void show() { System.out.println(num); }
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.show();
}
}
class A {
final void show() { System.out.println("Final show"); }
}
class B extends A {
void show() { System.out.println("Override show"); }
}
Output :
A

DEBUGGING
Error Happens Because,
num is private in class A → not
accessible in B.
class A {
final void show() { System.out.println("Final show"); }
}
class B extends A {
void show() { System.out.println("Override show"); }
}
Output :
A
Error Happens Because the
extends keyword is missing

DEBUGGING
Error Happens Because,
num is private in class A → not
accessible in B.
Error Happens Because, final
methods cannot be
overridden.
Error Happens Because the
extends keyword is missing
Output :
A

DEBUGGING
class Vehicle {
void start() {
System.out.println("Vehicle: Starting engine...");
}
}
class Car extends Vehicle {
void start() {
System.out.println("Car: Checking seatbelt...");
super.start();
System.out.println("Car: Ready to drive!");
}
}
public class Main {
public static void main(String[] args) {
Car c = new Car();
c.start();
}
}
class A {
A() {
System.out.println("A constructor");
show();
}
void show() { System.out.println("A show"); }
}
class B extends A {
int x = 10;
B() {
System.out.println("B constructor");
}
void show() {
System.out.println("B show, x = " + x);
}
}
public class Main {
public static void main(String[] args) {
new B();
}
}

DEBUGGING
OUTPUT
Car: Checking seatbelt...
Vehicle: Starting engine...
Car: Ready to drive!
class A {
A() {
System.out.println("A constructor");
show();
}
void show() { System.out.println("A show"); }
}
class B extends A {
int x = 10;
B() {
System.out.println("B constructor");
}
void show() {
System.out.println("B show, x = " + x);
}
}
public class Main {
public static void main(String[] args) {
new B();
}
}

DEBUGGING
OUTPUT
Car: Checking seatbelt...
Vehicle: Starting engine...
Car: Ready to drive!
OUTPUT
A constructor
B show, x = 0
B constructor

FAQs
Q2: Can we achieve multiple inheritance in Java?
Q1: What keyword is used for inheritance in Java?
Q4: Can a subclass inherit private members of its parent class?
Q3: What is hierarchical inheritance?

FAQs
Q2: Can we achieve multiple inheritance in Java?
Q1: What keyword is used for inheritance in Java?
The extends is used for class inheritance
Q4: Can a subclass inherit private members of its parent class?
Q3: What is hierarchical inheritance?

FAQs
Q2: Can we achieve multiple inheritance in Java?
Only through interfaces, not classes.
Q1: What keyword is used for inheritance in Java?
Q4: Can a subclass inherit private members of its parent class?
Q3: What is hierarchical inheritance?
The extends is used for class inheritance

FAQs
Q2: Can we achieve multiple inheritance in Java?
Only through interfaces, not classes.
Q1: What keyword is used for inheritance in Java?
Q4: Can a subclass inherit private members of its parent class?
Q3: What is hierarchical inheritance?
When multiple classes inherit from a single superclass.
Example: Dog and Cat both extend Animal.
The extends is used for class inheritance

FAQs
Q4: Can a subclass inherit private members of its parent class?
Q2: Can we achieve multiple inheritance in Java?
Q3: What is hierarchical inheritance?
No, private members are not inherited directly.
Only through interfaces, not classes.
When multiple classes inherit from a single superclass.
Example: Dog and Cat both extend Animal.
Q1: What keyword is used for inheritance in Java?
The extends is used for class inheritance

FAQs
Q6: What happens if a child and parent class have the same variable name?
Q5: Why is the parent constructor always called first when creating a child object?

FAQs
Q6: What happens if a child and parent class have the same variable name?
Q5: Why is the parent constructor always called first when creating a child object?
When a child object is created, it inherits the parent’s fields and methods.
Java needs to initialize the parent part first to ensure the child has a fully-formed
parent structure.
That’s why the parent constructor is automatically invoked before the child
constructor, even if you don’t explicitly call super().

FAQs
Q6: What happens if a child and parent class have the same variable name?
Q5: Why is the parent constructor always called first when creating a child object?
When a child object is created, it inherits the parent’s fields and methods.
Java needs to initialize the parent part first to ensure the child has a fully-formed
parent structure.
That’s why the parent constructor is automatically invoked before the child
constructor, even if you don’t explicitly call super().
The child’s variable hides the parent’s variable; it doesn’t override it.
Accessing the variable normally in the child accesses the child’s version.
To access the parent’s version, you must use super.variableName.

ANY QUERIES?
FEEL FREE TO ASK!

THE END
THANK YOU FOR LISTENINGDone By NAVIN PRASATH R