Last active
September 19, 2020 17:52
-
-
Save teramuza/5f802d64be9b202fe4dc7e37d904b197 to your computer and use it in GitHub Desktop.
Contoh penggunaan Constructor, Inheritance, dan Encapsulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package per2; | |
public class Engine { | |
private String manufacturer; | |
private double engineDisplacement; | |
private String fuelType; | |
private int yearProduction; | |
public Engine ( | |
String manufacturer, | |
double engineDisplacement, | |
String fuelType, | |
int yearProduction | |
) { | |
this.manufacturer = manufacturer; | |
this.engineDisplacement = engineDisplacement; | |
this.fuelType = fuelType; | |
this.yearProduction = yearProduction; | |
} | |
public String getManufacturer() { | |
return manufacturer; | |
} | |
public double getEngineDisplacement() { | |
return engineDisplacement; | |
} | |
public String getFuelType() { | |
return fuelType; | |
} | |
public int getYearProduction() { | |
return yearProduction; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package per2; | |
public class EngineFunctions { | |
Engine engine; | |
public void setEngine(Engine engine) { | |
this.engine = engine; | |
} | |
public void printManufacturer() { | |
System.out.println("Manufactured by: " + engine.getManufacturer()); | |
} | |
public void printEngineDispacment() { | |
System.out.println(engine.getEngineDisplacement() + " cc"); | |
} | |
public void printFuelType() { | |
System.out.println("This engine using " + engine.getFuelType() + " fuel"); | |
} | |
public void printYearProduction() { | |
System.out.println("Production " + engine.getYearProduction()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package per2; | |
public class Mobil extends EngineFunctions{ | |
public Mobil (Engine engine) { | |
setEngine(engine); | |
} | |
public static void main(String[] args) { | |
Engine engine = new Engine("Honda", 2200, "Gasoline", 2019); | |
Mobil mobil = new Mobil(engine); | |
System.out.println("Here are the specifications of your car: "); | |
mobil.printManufacturer(); | |
mobil.printYearProduction(); | |
mobil.printEngineDispacment(); | |
mobil.printFuelType(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment