44 lines
1.1 KiB
Java
44 lines
1.1 KiB
Java
|
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."
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
}
|