Skip to content

Instantly share code, notes, and snippets.

@teramuza
Last active September 19, 2020 17:52
Show Gist options
  • Save teramuza/5f802d64be9b202fe4dc7e37d904b197 to your computer and use it in GitHub Desktop.
Save teramuza/5f802d64be9b202fe4dc7e37d904b197 to your computer and use it in GitHub Desktop.
Contoh penggunaan Constructor, Inheritance, dan Encapsulation
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;
}
}
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());
}
}
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