Last active
April 8, 2019 19:31
-
-
Save bnonni/7094199d7c7fe3b97247213f03655759 to your computer and use it in GitHub Desktop.
Java Composition vs. Java Inheritance
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
public class Car { | |
//private variables | |
private String color; | |
private int maxSpeed; | |
public void carInfo() { | |
System.out.println("Car Color= " + color + " Max Speed= " + maxSpeed); | |
} | |
public void setColor(String color) { | |
this.color = color; | |
} | |
public void setMaxSpeed(int maxSpeed) { | |
this.maxSpeed = maxSpeed; | |
} | |
} |
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
public class Racecar extends Car { | |
public void startRacecar() { | |
Engine NASCAR_engine = new Engine(); | |
NASCAR_engine.start(); | |
} | |
} |
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
public class Engine { | |
public void start() { | |
System.out.println("Engine Started:"); | |
} | |
public void stop() { | |
System.out.println("Engine Stopped:"); | |
} | |
} |
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
public class Programmer { | |
private String name; | |
private int age; | |
private char gender; | |
private Computer computer; | |
public Programmer(String name, int age, char gender) { | |
this.name = name; | |
this.age = age; | |
this.gender = gender; | |
this.computer = new Computer(); | |
computer.setType("Mac"); | |
computer.setRAM(32); | |
computer.setProcessorSpeed(4.3); | |
computer.setStorageCapacity(512); | |
} | |
public String getType() { | |
return computer.getType(); | |
} | |
public int getRAM() { | |
return computer.getRAM(); | |
} | |
public int getStorage() { | |
return computer.getStorageCapacity(); | |
} | |
public double getProcessor() { | |
return computer.getProcessorSpeed(); | |
} | |
public void getComputer() { | |
computer.getComputerInfo(); | |
} | |
public void start() { | |
computer.startUp(); | |
} | |
public void reboot() { | |
computer.sudoReboot(); | |
} | |
public void shutdown() { | |
computer.sudoShutdown(); | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public void setGender(char gender) { | |
this.gender = gender; | |
} | |
public void getProgrammerInfo() { | |
System.out.println("Programmer: " + "\n[Name]= " + name + "\n[Age]= " + age + " years old" + "\n[Gender]= " + gender); | |
} | |
} |
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
public class Computer { | |
private String type; | |
private int RAM; | |
private int storage_capacity; | |
private double processor_speed; | |
public void getComputerInfo() { | |
System.out.println("Computer: " + "\n[Type]= " + type + "\n[RAM]= " + RAM + " GB" + "\n[Storage Capacity]= " | |
+ storage_capacity + "\n[Processor Speed]= " + processor_speed + " GHz"); | |
} | |
public void setType(String type) { | |
this.type = type; | |
} | |
public String getType() { | |
return type; | |
} | |
public void setRAM(int RAM) { | |
this.RAM = RAM; | |
} | |
public int getRAM() { | |
return RAM; | |
} | |
public void setStorageCapacity(int storage_capacity) { | |
this.storage_capacity = storage_capacity; | |
} | |
public int getStorageCapacity() { | |
return storage_capacity; | |
} | |
public void setProcessorSpeed(double processor_speed) { | |
this.processor_speed = processor_speed; | |
} | |
public double getProcessorSpeed() { | |
return processor_speed; | |
} | |
public void startUp() { | |
System.out.println("Mac Starting Up... Welcome!"); | |
} | |
public void sudoReboot() { | |
System.out.println("Mac Rebooting... See you soon."); | |
} | |
public void sudoShutdown() { | |
System.out.println("Mac Shutting Down... Goodbye."); | |
} | |
} |
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
public class Coding { | |
public static void main(String[] args) { | |
Programmer employee_001 = new Programmer("Bryan", 28, 'M'); | |
Computer c_001 = new Computer(); | |
System.out.println(); | |
employee_001.getProgrammerInfo(); | |
System.out.println(); | |
c_001.getComputerInfo(); | |
System.out.println(); | |
c_001.startUp(); | |
System.out.println(); | |
System.out.println("Upgrading processor speed...\n"); | |
c_001.setProcessorSpeed(5.5); | |
System.out.println("New processor speed= " + c_001.getProcessorSpeed()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment