0% found this document useful (0 votes)
7 views18 pages

OOP Slide-7 Exercise (Lec 14)

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

OOP Slide-7 Exercise (Lec 14)

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

Content

❑ Java Inheritance Exercises


Types of inheritance :
Why use inheritance?

• The most important use is the reusability


of code. The code that is present in the
parent class doesn’t need to be written
again in the child class.

• To achieve runtime polymorphism


through method overriding.
Analytical Problem 1

• Suppose you are to design a Green


University Management System where a
class named Faculty from which 3 classes:
Chairperson, and Program_Coordinator
will inherit some attributes such as
Experience and Degree and a method
teach(). You are allowed to assign the
value of the parent class only from its
child class
Analytical Problem 1

• Now write a Java program as follows:


▫ Create all the classes mentioned with a
constructor to assign the default values of
Experience and Degree.
▫ Create 4 instances from each class. Also,
create a Faculty type reference that refers to
the object of each child class accordingly.
▫ Call teach() method each time by a reference
type of Faculty refers to Chairperson type
object. Explain the output.
Analytical Problem 1
• Now write a Java program as follows:
▫ Create all the classes mentioned with a
constructor to assign the default values of
Experience and Degree.
▫ Create 4 instances from each class. Also,
create a Faculty type reference that refers to
the object of each child class accordingly.
▫ Call teach() method each time by a reference
type of Faculty refers to Chairperson type
object. Explain the output.
Analytical Problem 1(Solution)
class Faculty {
protected int Experience;
protected String Degree;

public Faculty() {
Experience = 0;
Degree = "Unknown";
}

public void teach() {


System.out.println("This is the teaching method of a generic Faculty.");
}
}

class Chairperson extends Faculty {


public Chairperson() {
Experience = 20; // Default experience for a Chairperson
Degree = "Ph.D."; // Default degree for a Chairperson
}

@Override
public void teach() {
System.out.println("Teaching method of a Chairperson.");

}
}
Analytical Problem 1(Solution)
class Program_Coordinator extends Faculty {
public Program_Coordinator() {
Experience = 10; // Default experience for a Program Coordinator
Degree = "Masters"; // Default degree for a Program Coordinator
}

@Override
public void teach() {
System.out.println("Teaching method of a Program Coordinator.");
}
}
Analytical Problem 1(Solution)
public class UniversityManagementSystem {
public static void main(String[] args) {
Faculty faculty1 = new Chairperson();
Faculty faculty2 = new Program_Coordinator();

Chairperson chairperson = new Chairperson();

// Call teach() method using Faculty reference


faculty1.teach(); // Output: Teaching method of a Chairperson.
faculty2.teach(); // Output: Teaching method of a Program Coordinator.
chairperson.teach(); // Output: Teaching method of a Chairperson.
}
}
Analytical Problem 2
Suppose you are to design a Vehicle
Management System for your varsity where
a class named Vehicle from which 3
classes: Bus, Coaster_Bus and HiAce_Car
will inherit some attributes such as Mileage
and Capacity and a method
Realtime_speed(). You are allowed to assign
the value of the parent class only from its
child class. Now write a Java program as
follows:
Analytical Problem 2
A.Create all the classes mentioned with a
constructor to assign the default values of
Mileage and Capacity.
B.Create 4 instances from each class. Also,
create a Vehicle type reference that refers
to the object of each child class
accordingly.
C.Call Realtime_speed() method each time
by a reference type of Vehicle refers to
Coaster_Bus type object. Explain the
output.
Analytical Problem 2(Solution)
class Vehicle {
protected double Mileage;
protected int Capacity;

public Vehicle() {
Mileage = 0.0;
Capacity = 0;
}

public void Realtime_speed() {


System.out.println("This is the Realtime Speed of a generic Vehicle.");
}
}
Analytical Problem 2(Solution)
• class Bus extends Vehicle {
• public Bus() {
• Mileage = 6.5;
• Capacity = 50;
• }

@Override
• public void Realtime_speed() {
• System.out.println("Realtime speed of a Bus: 60 km/h");
• }
• }
Analytical Problem 2(Solution)
class Vehicle {
protected double Mileage;
protected int Capacity;

public Vehicle() {
Mileage = 0.0;
Capacity = 0;
}

public void Realtime_speed() {


System.out.println("This is the Realtime Speed of a generic Vehicle.");
}
}
Analytical Problem 2(Solution)
class Coaster_Bus extends Bus {
public Coaster_Bus() {
Mileage = 8.0;
Capacity = 20;
}
@Override
public void Realtime_speed() {
System.out.println("Realtime speed of a Coaster Bus: 80 km/h");
}
}
class HiAce_Car extends Vehicle {
public HiAce_Car() {
Mileage = 12.0;
Capacity = 8;
}
Analytical Problem 2(Solution)
public class VehicleManagementSystem {
public static void main(String[] args) {
Vehicle vehicle1 = new Bus();
Vehicle vehicle2 = new Coaster_Bus();
Vehicle vehicle3 = new HiAce_Car();

// Call Realtime_speed() method using Vehicle reference


vehicle1.Realtime_speed(); // Output: Realtime speed of a Bus: 60 km/h
vehicle2.Realtime_speed(); // Output: Realtime speed of a Coaster Bus: 80 km/h
vehicle3.Realtime_speed(); // Output: Realtime speed of a HiAce Car: 100 km/h
coasterBus.Realtime_speed(); // Output: Realtime speed of a Coaster Bus: 80 km/h
}
}
Analytical Problem 3
Explain the concept of method overriding in Java
inheritance. Provide an example and discuss its
significance in object-oriented programming
Thanks

You might also like