0% found this document useful (0 votes)
10 views10 pages

S6 - Core OOP Principles - Inheritance Concept Introduction

Uploaded by

Poovizhi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

S6 - Core OOP Principles - Inheritance Concept Introduction

Uploaded by

Poovizhi S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Session 5 - Inheritance

Agenda
1. Types: Single, Multilevel, Hierarchical
2. Using extends and super
3. Constructor chaining
4. Method Overriding (@Override)"

�� Java Inheritance in Depth


�� What is Inheritance?

Inheritance is when one class inherits properties (fields) and behaviors (methods) of another
class. It helps in code reusability and supports polymorphism.

In Java, inheritance is achieved using the extends keyword.

Key Features of Inheritance


1. Reusability: Code from the parent class can be reused in the child class. 2. Method
Overriding: A subclass can modify or "override" the behavior of a method defined in the
parent class.
3. Access to Parent Class Members: A subclass can access the public and protected
members of the parent class, but not private members.

1️⃣Types of Inheritance
✅ a) Single Inheritance
Java supports single inheritance, meaning a subclass can inherit from only one superclass.
However, it can still implement multiple interfaces.
1

Java does not support multiple inheritance with classes (i.e., one class cannot directly inherit
from multiple classes), though it supports multiple inheritance through interfaces.

Example: Vehicle → Car

class Vehicle {
String fuel = "Diesel";

void start() {
System.out.println("Vehicle starts with " + fuel);
}
}

class Car extends Vehicle {


void display() {
System.out.println("Car is a type of Vehicle.");
}
}

public class SingleInheritanceDemo {


public static void main(String[] args) {
Car c = new Car();
c.start(); // inherited from Vehicle
c.display(); // defined in Car
}
}

�� Real-time analogy: A Car is a specialized form of a Vehicle.

✅ b) Multilevel Inheritance
A subclass can also serve as a superclass for another subclass.

○ A class can inherit from another subclass, which itself inherits from a superclass.
Example: Vehicle → Car → ElectricCar

class Vehicle {

void fuelType() {

System.out.println("General vehicle fuel.");


}
}

class Car extends Vehicle {


void wheels() {
System.out.println("Car has 4 wheels.");
}
}

class ElectricCar extends Car {


void battery() {
System.out.println("Electric car uses battery.");
}
}

public class MultilevelInheritanceDemo {


public static void main(String[] args) {
ElectricCar tesla = new ElectricCar();
tesla.fuelType(); // from Vehicle
tesla.wheels(); // from Car
tesla.battery(); // from ElectricCar
}
}

�� Real-time analogy: Tesla (ElectricCar) is a type of Car, and a Car is a type of Vehicle.

✅ c) Hierarchical Inheritance
Multiple classes can inherit from a single superclass.
Example: Vehicle → Car & Bike
class Vehicle {
void start() {
System.out.println("Vehicle is starting...");
}
}

3
Vehicle { void type() {

class Car extends

System.out.println("This is a Car.");
}
}

class Bike extends Vehicle {


void type() {
System.out.println("This is a Bike.");
}
}

public class HierarchicalInheritanceDemo {


public static void main(String[] args) {
Car car = new Car();
Bike bike = new Bike();

car.start(); // from Vehicle


car.type();

bike.start(); // from Vehicle


bike.type();
}
}

�� Real-time analogy: Both Car and Bike are different types of Vehicle.
2️⃣Using extends and super
● extends → used to inherit a class.

● super → used to call parent class methods/constructors. 4

Example:

class Vehicle {

String brand = "Generic Vehicle";


Vehicle() {
System.out.println("Vehicle Constructor Called");
}

void displayBrand() {
System.out.println("Brand: " + brand);
}
}

class Car extends Vehicle {


String brand = "Car Brand";

Car() {
super(); // calls Vehicle constructor
System.out.println("Car Constructor Called");
}

void displayBrand() {
super.displayBrand(); // call parent method
System.out.println("Brand in Car: " + brand);
}
}
public class SuperKeywordDemo {
public static void main(String[] args) {
Car car = new Car();
car.displayBrand();
}
}

�� Output:

Vehicle Constructor Called


Car Constructor Called
Brand: Generic Vehicle

5
Brand

Brand in Car: Car

3️⃣Constructor Chaining
When an object of a subclass is created, constructors are called in the inheritance chain
(top-down).

Example:

class Vehicle {
Vehicle() {
System.out.println("Vehicle constructor");
}
}

class Car extends Vehicle {


Car() {
System.out.println("Car constructor");
}
}

class ElectricCar extends Car {


ElectricCar() {
System.out.println("ElectricCar constructor");
}
}

public class ConstructorChainingDemo {


public static void main(String[] args) {
ElectricCar tesla = new ElectricCar();
}
}

�� Output:

Vehicle constructor
Car constructor

6
constructor

ElectricCar

4️⃣Method Overriding (@Override)


When a subclass provides its own implementation of a parent class method.

Example:

class Vehicle {
void start() {
System.out.println("Vehicle starts...");
}
}

class Car extends Vehicle {


@Override
void start() {
System.out.println("Car starts with a key ignition...");
}
}
class ElectricCar extends Car {
@Override
void start() {
System.out.println("Electric Car starts silently with a
button...");
}
}

public class MethodOverridingDemo {


public static void main(String[] args) {
Vehicle v1 = new Vehicle();
Vehicle v2 = new Car();
Vehicle v3 = new ElectricCar();

v1.start(); // Vehicle version


v2.start(); // Car version
v3.start(); // ElectricCar version

}
}

�� Real-time analogy: Start method works differently for a general Vehicle, a Car, and
an ElectricCar.

Polymorphism and Inheritance

Polymorphism in Java is closely tied to inheritance. When a subclass object is referred to by a


superclass reference, the method invoked is based on the actual object type (i.e., dynamic
method dispatch).

Example of polymorphism with inheritance:

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


@Override
void sound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myDog = new Dog();

myAnimal.sound(); // Output: Animal makes a sound


myDog.sound(); // Output: Dog barks
}
}

Access Modifiers and Inheritance

Access modifiers determine the visibility of members (fields and methods) across classes:

● Public: Accessible from anywhere.


● Protected: Accessible within the same package or by subclasses.
● Default (no modifier): Accessible within the same package.
● Private: Not accessible outside the class.

Example:

class Animal {
public String name; // Can be accessed from anywhere
protected int age; // Can be accessed by subclasses or within the
same package
private String breed; // Only accessible within the Animal class
}

class Dog extends Animal {


void printDetails() {
System.out.println("Name: " + name); // Public field, accessible
System.out.println("Age: " + age); // Protected field, accessible
// System.out.println("Breed: " + breed); // Error: breed is
private
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();
dog.printDetails();
}
}

You might also like