2.1. Inheritance
2.1. Inheritance
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.
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");
}
}
void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
System.out.println("This car can carry 6 passengers.");
}
}
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");
}
}
void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
System.out.println("This car can carry 6 passengers.");
}
}
void displayBikeInfo() {
displayInfo();
System.out.println("Has Helmet: " + hasHelmet);
System.out.println("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.
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");
}
}
void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
System.out.println("This car can carry 6 passengers.");
}
}
void displayElectricCarInfo() {
displayCarInfo();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
System.out.println("This electric car can carry 4 passengers.");
}
}
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");
}
}
void displayCarInfo() {
displayInfo();
System.out.println("Wheels: " + wheels);
PassengerCapacity passengerCapacity = new PassengerCapacity();
passengerCapacity.displayPassengerCapacity("Car");
}
}
void displayElectricCarInfo() {
displayCarInfo();
System.out.println("Battery Capacity: " + batteryCapacity + " kWh");
System.out.println("This electric car can carry 6 passengers.");
}
}
void displayBikeInfo() {
displayInfo();
System.out.println("Has Helmet: " + hasHelmet);
PassengerCapacity passengerCapacity = new PassengerCapacity();
passengerCapacity.displayPassengerCapacity("Bike");
}
}
void displayBusInfo() {
displayInfo();
System.out.println("Seating Capacity: " + seatingCapacity);
PassengerCapacity passengerCapacity = new PassengerCapacity();
passengerCapacity.displayPassengerCapacity("Bus");
}
}
Super keyword:
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;