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

2.1. Inheritance

The document explains the concept of inheritance in object-oriented programming, detailing how subclasses inherit properties and methods from superclasses. It covers various types of inheritance including single, hierarchical, multilevel, hybrid, and mentions the limitations of multiple inheritance in Java. Additionally, it provides example programs to illustrate each type of inheritance and the use of the 'super' keyword to access parent class methods and constructors.

Uploaded by

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

2.1. Inheritance

The document explains the concept of inheritance in object-oriented programming, detailing how subclasses inherit properties and methods from superclasses. It covers various types of inheritance including single, hierarchical, multilevel, hybrid, and mentions the limitations of multiple inheritance in Java. Additionally, it provides example programs to illustrate each type of inheritance and the use of the 'super' keyword to access parent class methods and constructors.

Uploaded by

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

Module-2:

Inheritance:
A new class (called a subclass or child class) inherit the properties (fields) and
behaviors (methods) of an existing class (called a superclass or parent class) is called
inheritance.
Parent class: The class whose properties and behaviors inherited.
Child class: The class inherite the properties and behaviors from base class.

Use of Inheritance:
 Reuse code from the parent class.
 Add its own specific fields and methods.
 Override methods from the parent class to provide its own implementation.
Syntax:
class parentclass{
//methods and fields;
}
Class childclass extends parentclass{
//methods and fields;
}

Types of inheritance:
1. Single Inheritance
2. Hierarchal Inheritance
3. Multilevel Inheritance
4. Hybrid Inheritance
5. Multiple Inheritance
1. Single Inheritance: One class inherits from a single base class.

Example: Class A inherits from class B.

Example program for single inheritance:


// Base Class: Vehicle
class Vehicle {
String brand;
String colour;
String fuelType;
int speed;

void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Colour: " + colour);
System.out.println("Fuel Type: " + fuelType);
System.out.println("Speed: " + speed + " km/h");
}
}

// Child Class: Car


class Car extends Vehicle {
int wheels;

void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
System.out.println("This car can carry 6 passengers.");
}
}

public class SingleInheritanceExample {


public static void main(String[] args) {
Car car = new Car();
car.brand = "Toyota";
car.colour = "Red";
car.fuelType = "Petrol";
car.speed = 120;
car.wheels = 4;

// Display car information


car.displayCarInfo();
}
}
Output:
Brand: Toyota
Colour: Red
Fuel Type: Petrol
Speed: 120 km/h
Wheels: 4
This car can carry 6 passengers.

2. Hierarchical Inheritance: Multiple classes inherit from a single base class.


Example: Class B, C and class D are inherit from class A.

Example Program for hierarchal Inheritance:


class Vehicle {
String brand;
String colour;
String fuelType;
int speed;

void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Colour: " + colour);
System.out.println("Fuel Type: " + fuelType);
System.out.println("Speed: " + speed + " km/h");
}
}

// Child Class 1: Car


class Car extends Vehicle {
int wheels;

void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
System.out.println("This car can carry 6 passengers.");
}
}

// Child Class 2: Bike


class Bike extends Vehicle {
boolean hasHelmet;

void displayBikeInfo() {
displayInfo();
System.out.println("Has Helmet: " + hasHelmet);
System.out.println("This bike can carry 2 passengers.");
}
}

public class HierarchicalInheritanceExample {


public static void main(String[] args) {
// Creating an object of Car
Car car = new Car();
car.brand = "Honda";
car.colour = "Red";
car.fuelType = "Petrol";
car.speed = 150;
car.wheels = 4;
car.displayCarInfo();

// Creating an object of Bike


Bike bike = new Bike();
bike.brand = "Yamaha";
bike.colour = "Blue";
bike.fuelType = "Electric";
bike.speed = 100;
bike.hasHelmet = true;
bike.displayBikeInfo();
}
}
Output: Brand: Honda
Colour: Red
Fuel Type: Petrol
Speed: 150 km/h
Wheels: 4
This car can carry 6 passengers.
Brand: Yamaha
Colour: Blue
Fuel Type: Electric
Speed: 100 km/h
Has Helmet: true
This bike can carry 2 passengers.

3. Multilevel Inheritance: A class inherits from a derived class, which in turn inherits from
a base class.

Example: Class C inherits from class B, and class B inherits from class A.

Example program for multilevel inheritance.


// Base Class: Vehicle
class Vehicle {
String brand;
String colour;
String fuelType;
int speed;

void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Colour: " + colour);
System.out.println("Fuel Type: " + fuelType);
System.out.println("Speed: " + speed + " km/h");
}
}

// Intermediate Class: Car (inherits from Vehicle)


class Car extends Vehicle {
int wheels;

void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
System.out.println("This car can carry 6 passengers.");
}
}

// Child Class: ElectricCar (inherits from Car)


class ElectricCar extends Car {
int batteryCapacity;

void displayElectricCarInfo() {
displayCarInfo();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
System.out.println("This electric car can carry 4 passengers.");
}
}

public class MultilevelInheritanceExample {


public static void main(String[] args) {
ElectricCar eCar = new ElectricCar();
eCar.brand = "Tesla";
eCar.colour = "Black";
eCar.fuelType = "Electric";
eCar.speed = 200;
eCar.wheels = 4;
eCar.batteryCapacity = 75;

// Display ElectricCar information


eCar.displayElectricCarInfo();
}
}
Output:
Brand: Tesla
Colour: Black
Fuel Type: Electric
Speed: 200 km/h
Wheels: 4
This car can carry 6 passengers.
Battery Capacity: 75 kWh
This electric car can carry 4 passengers.

4. Hybrid Inheritance: A combination of different types of inheritance (such as multilevel


and hierarchical inheritance combined).

Example program for hybrid inheritance.


// Base Class: Vehicle
class Vehicle {
String brand;
String colour;
String fuelType;
int speed;

void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Colour: " + colour);
System.out.println("Fuel Type: " + fuelType);
System.out.println("Speed: " + speed + " km/h");
}
}

// Helper Class 1: PassengerCapacity


class PassengerCapacity {
void displayPassengerCapacity(String vehicleType) {
if (vehicleType.equals("Car")) {
System.out.println("This car can carry 6 passengers.");
} else if (vehicleType.equals("Bike")) {
System.out.println("This bike can carry 2 passengers.");
} else if (vehicleType.equals("Bus")) {
System.out.println("This bus can carry 30 passengers.");
}
}
}

// Child Class 1: Car (inherits from Vehicle and uses PassengerCapacity)


class Car extends Vehicle {
int wheels;

void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
PassengerCapacity passengerCapacity = new PassengerCapacity();
passengerCapacity.displayPassengerCapacity("Car");
}
}

// Child Class 2: ElectricCar (inherits from Car)


class ElectricCar extends Car {
int batteryCapacity; // specific to electric cars

void displayElectricCarInfo() {
displayCarInfo();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
System.out.println("This electric car can carry 6 passengers.");
}
}

// Child Class 3: Bike (inherits from Vehicle and uses PassengerCapacity)


class Bike extends Vehicle {
boolean hasHelmet;

void displayBikeInfo() {
displayInfo();
System.out.println("Has Helmet: " + hasHelmet);
PassengerCapacity passengerCapacity = new PassengerCapacity();
passengerCapacity.displayPassengerCapacity("Bike");
}
}

// Child Class 4: Bus (inherits from Vehicle and uses PassengerCapacity)


class Bus extends Vehicle {
int seatingCapacity;

void displayBusInfo() {
displayInfo();
System.out.println("Seating Capacity: " + seatingCapacity);
PassengerCapacity passengerCapacity = new PassengerCapacity();
passengerCapacity.displayPassengerCapacity("Bus");
}
}

public class HybridInheritanceExample {


public static void main(String[] args) {
// Creating an object of Car
Car car = new Car();
car.brand = "Toyota";
car.colour = "Red";
car.fuelType = "Petrol";
car.speed = 120;
car.wheels = 4;
car.displayCarInfo();

// Creating an object of ElectricCar


ElectricCar electricCar = new ElectricCar();
electricCar.brand = "Tesla";
electricCar.colour = "White";
electricCar.fuelType = "Electric";
electricCar.speed = 150;
electricCar.wheels = 4;
electricCar.batteryCapacity = 75;
electricCar.displayElectricCarInfo();

// Creating an object of Bike


Bike bike = new Bike();
bike.brand = "Yamaha";
bike.colour = "Blue";
bike.fuelType = "Electric";
bike.speed = 80;
bike.hasHelmet = true;
bike.displayBikeInfo();

// Creating an object of Bus


Bus bus = new Bus();
bus.brand = "Mercedes";
bus.colour = "Yellow";
bus.fuelType = "Diesel";
bus.speed = 90;
bus.seatingCapacity = 30;
bus.displayBusInfo();
}
}
5. Multiple Inheritance (Java does not support multiple inheritance through classes, but it
can be achieved using interfaces):
 A class inherits from more than one class (Java does not allow multiple inheritance
through classes to avoid ambiguity, but it allows multiple inheritance through
interfaces).

Super keyword:

Key Points About super:

1. Access Parent Constructor: super() calls the constructor of the parent class.
2. Access Parent Methods: super.methodName() calls a method from the parent class
(even if overridden).
3. Access Parent Fields: super.fieldName refers to a field in the parent class.

Example program:
// Parent class (superclass)
class Animal {
String name;

// Constructor of parent class


public Animal(String name) {
this.name = name;
}

// Method to display animal name


public void display() {
System.out.println("Animal Name: " + name);
}
}

// Child class (subclass)


class Dog extends Animal {

// Constructor of child class


public Dog(String name) {
// Using 'super' to call the constructor of the parent class (Animal)
super(name);
}

// Overriding display method to call parent method using 'super'


public void display() {
super.display(); // Calls the display method of the parent class
System.out.println(name + " is a Dog.");
}
}

// Main class to run the program


public class SuperKeywordExample {
public static void main(String[] args) {
Dog dog = new Dog("Buddy"); // Create a Dog object
dog.display(); // Calls the display method of the Dog class
}
}

You might also like