MIT-Curricular/OOP/Java/Lab/Week6/Bike/Biker.java

44 lines
1.1 KiB
Java
Raw Normal View History

2024-09-14 01:52:19 +05:30
class Bike {
int speedlimit = 80;
void run() {
System.out.println("Bike speed limit: " + speedlimit + " km/h");
}
}
class Splendar extends Bike {
int speedlimit = 60;
void run() {
System.out.println("Splendar speed limit: " + speedlimit + " km/h");
}
}
class Biker {
public static void main(String[] args) {
// Create objects for runtime polymorphism
Bike bike = new Bike();
Bike splendar = new Splendar();
// Demonstrate runtime polymorphism through method calls
bike.run();
splendar.run();
// Check whether runtime polymorphism can be achieved through data members
System.out.println("Bike speedlimit: " + bike.speedlimit);
System.out.println("Splendar speedlimit: " + splendar.speedlimit);
if (bike.speedlimit == splendar.speedlimit) {
System.out.println(
"Runtime polymorphism is not achieved through data members."
);
} else {
System.out.println(
"Runtime polymorphism is achieved through data members."
);
}
}
}