LECTURE 15: Inheritance
Mode: Online | Tool: VS Code | Type: Theory + Practical
PART 1: WHY INHERITANCE EXISTS (MOST IMPORTANT)
First Understand the Problem
Suppose we create a class for Student.
class Student {
String name;
int age;
void display() {
[Link](name);
[Link](age);
}
}
Now we create another class Teacher.
class Teacher {
String name;
int age;
void display() {
[Link](name);
[Link](age);
}
}
Observe Carefully
Both classes contain:
• name
• age
• display()
Same code repeated again.
This is called:
Code Duplication
Why Repetition is Bad?
If same code repeated many times:
• Program becomes longer
• Difficult to maintain
• If one change needed → change everywhere
• More chances of errors
• Wastes memory and time
Example:
If we add:
String address;
Then we must add it in:
• Student
• Teacher
• Employee
• Manager
• Clerk
Again and again.
This is bad programming design.
PART 2: SOLUTION → INHERITANCE
Instead of writing same code many times:
• Create one common class
• Other classes reuse it
This concept is called:
Inheritance
PART 3: WHAT IS INHERITANCE?
Definition
Inheritance is the process where one class acquires properties and methods of another
class.
Simple language:
Child class uses parent class features.
PART 4: REAL LIFE EXAMPLE (VERY IMPORTANT)
Inheritance exists everywhere in real life.
Parent Child gets
Father, surname, properties
Animal, Dog gets animal features
Vehicle, Car gets vehicle features
Example:
• Every car is a vehicle
• Every dog is an animal
This creates:
IS-A Relationship
Examples:
Dog IS-A Animal
Car IS-A Vehicle
Student IS-A Person
PART 5: IMPORTANT TERMS
Term Meaning
Parent Class Class whose properties inherited
Child Class Class which inherits
Super Class Parent class
Sub Class Child class
extends Keyword used for inheritance
PART 6: BASIC STRUCTURE OF INHERITANCE
class Parent {
class Child extends Parent {
}
Meaning:
Child class automatically gets:
• Variables
• Methods
from Parent class.
PART 7: FIRST INHERITANCE PROGRAM
class Animal {
void sound() {
[Link]("Animal makes sound");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog barks");
}
public static void main(String[] args) {
Dog d1 = new Dog();
[Link]();
[Link]();
}
}
PART 8: WHAT IS HAPPENING INTERNALLY? (VERY IMPORTANT)
Step-by-step:
Step 1
Animal class created:
sound()
Step 2
Dog class created:
bark()
But:
class Dog extends Animal
means:
Dog inherits Animal.
So Dog gets:
sound()
bark()
Step 3
Object created:
Dog d1 = new Dog();
Dog object can access:
• Dog methods
• Animal methods
Output:
Animal makes sound
Dog barks
PART 9: WHY "extends" KEYWORD USED?
class Dog extends Animal
Meaning:
Dog class is extending features of Animal class.
So:
• Dog does not rewrite sound()
• Dog reuses sound()
This is:
Code Reusability
PART 10: MEMORY THINKING (IMPORTANT THEORY)
Without inheritance:
Student:
name
age
display()
Teacher:
name
age
display()
Duplicate memory and duplicate code.
With inheritance:
Person:
name
age
display()
Student inherits Person
Teacher inherits Person
Common code written only once.
Better memory usage.
Better structure.
PART 11: FULL PRACTICAL PROGRAM – PERSON & STUDENT
Public class Person {
String name;
int age;
void showPerson() {
[Link]("Name: " + name);
[Link]("Age: " + age);
}
}
class Student extends Person {
int marks;
void showStudent() {
[Link]("Marks: " + marks);
}
public static void main(String[] args) {
Student s1 = new Student();
[Link] = "Mohan";
[Link] = 20;
[Link] = 95;
[Link]();
[Link]();
}
}
PART 12: MENTAL FLOW OF PROGRAM
Step 1
Person class contains:
name
age
showPerson()
Step 2
Student class extends Person.
So Student automatically gets:
name
age
showPerson()
plus its own:
marks
showStudent()
Step 3
Object created:
Student s1 = new Student();
Object can use:
• Parent members
• Child members
PART 13: ADVANTAGES OF INHERITANCE
1. Code Reusability
Write code once, use many times.
2. Reduces Code Duplication
No need to rewrite same variables and methods.
3. Easy Maintenance
Changes done only once in parent class.
4. Better Program Structure
Program becomes organized.
5. Supports Real-World Modeling
Represents real-life relationships.
PART 14: TYPES OF INHERITANCE
Type Meaning
Single One parent → one child
Multilevel Grandparent → Parent → Child
Hierarchical One parent → many children
PART 15: SINGLE INHERITANCE
class Animal {
void sound() {
[Link]("Animal Sound");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog Bark");
}
public static void main(String[] args) {
Dog d1 = new Dog();
[Link]();
[Link]();
}
}
PART 16: MULTILEVEL INHERITANCE
class Animal {
void sound() {
[Link]("Animal Sound");
}
}
class Dog extends Animal {
void bark() {
[Link]("Dog Bark");
}
}
class Puppy extends Dog {
public static void main(String[] args) {
Puppy p1 = new Puppy();
[Link]();
[Link]();
}
}
Flow:
Animal → Dog → Puppy
PART 17: HIERARCHICAL INHERITANCE
class Person {
void display() {
[Link]("Person Class");
}
}
class Student extends Person {
}
class Teacher extends Person {
}
class principal extends Person{
}
Flow:
Person
/ \
Student Teacher
PART 18: VERY IMPORTANT RULES
1. Use extends keyword
2. Child class gets parent members
3. Private members not directly accessible
4. Java does not support multiple inheritance with classes
5. Constructor not inherited directly
6. Object of child can access parent members
PART 19: COMMON CONFUSIONS
Question Answer
Why inheritance used? Code reuse
Which keyword used? extends
Can child access parent methods? Yes
Can parent access child methods? No
What relationship formed? IS-A
Can Java support multiple inheritance? No
PART 20: VERY IMPORTANT VIVA QUESTIONS
1. What is inheritance?
2. Why inheritance used?
3. What is parent class?
4. What is child class?
5. Which keyword used in inheritance?
6. What is IS-A relationship?
7. Types of inheritance?
8. Advantages of inheritance?
9. Can child class access parent methods?
10. Does Java support multiple inheritance?
ONE LINE TO REMEMBER
Inheritance allows one class to reuse properties and methods of another class.