Add OOP/Java/Lab/Week6/Bike/Biker.java
This commit is contained in:
parent
f8f3f0cb69
commit
6982f92ddb
43
OOP/Java/Lab/Week6/Bike/Biker.java
Normal file
43
OOP/Java/Lab/Week6/Bike/Biker.java
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
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."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user