This commit is contained in:
2024-09-27 10:23:15 +02:00
parent 4908d26105
commit c556d9d901
10 changed files with 81 additions and 1 deletions

View File

@ -0,0 +1,7 @@
package com.oop;
public class ElVehicle extends Vehicle {
public void resource(){
System.out.println("I use electricity");
}
}

View File

@ -0,0 +1,7 @@
package com.oop;
public class HybridVehicle extends Vehicle {
public void resource(){
System.out.println("I use both petrol and electricity");
}
}

View File

@ -21,5 +21,45 @@ public class Main {
pro1.write(); pro1.write();
pro1.useEffects(); pro1.useEffects();
pro1.changeResolution(); pro1.changeResolution();
//Polymorphism
// Vehicles can be classified according to what energy source powers them. The program you are given has 3 vehicle classes: Vehicle, ElectricVehicle and HybridVehicle.
// ElectricVehicle and HybridVehicle classes are inherited from Vehicle class.
// Complete the program by reimplementing method resource() in inherited classes, so that the given calls work correctly.
Vehicle vehicle = new Vehicle();
Vehicle electric = new ElVehicle();
Vehicle hybrid = new HybridVehicle();
//calls
vehicle.start();
vehicle.resource();
electric.start();
electric.resource();
hybrid.start();
hybrid.resource();
//overriding funcions! For more visit Program.js
System.out.println(Program.max(8, 17));
System.out.println(Program.max(3.14, 7.68));
//Overloading or overriding
a = 5;
double b = 10.2;
System.out.println(doubleTheValue(a));
System.out.println(doubleTheValue(b));
} }
//complete the method for integer value type
public static int doubleTheValue(int a) {
return a*2;
}
//overload the method for double value type
public static double doubleTheValue(double a) {
return a*2;
}
} }

View File

@ -0,0 +1,20 @@
package com.oop;
public class Program {
static double max(double a, double b) {
if(a > b) {
return a;
}
else {
return b;
}
}
static int max(int a, int b) {
if(a > b) {
return a;
}
else {
return b;
}
}
}

View File

@ -27,5 +27,11 @@ public class Vehicle {
public void setColor(String c) { public void setColor(String c) {
this.color = c; this.color = c;
} }
public void start(){
System.out.println("Starting");
}
public void resource(){
System.out.println("I use petrol");
}
} }

Binary file not shown.

Binary file not shown.