Skip to content

Instantly share code, notes, and snippets.

@leonardorifeli
Last active August 20, 2016 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonardorifeli/d03e68ef59a0667a806952583c1ce978 to your computer and use it in GitHub Desktop.
Save leonardorifeli/d03e68ef59a0667a806952583c1ce978 to your computer and use it in GitHub Desktop.
Inheritance or composition article
package com.leonardorifeli.article.inheritance.model;
public class Automobile {
private String color;
private Integer quantityPort;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public Integer getQuantityPort() {
return quantityPort;
}
public void setQuantityPort(Integer quantityPort) {
this.quantityPort = quantityPort;
}
}
package com.leonardorifeli.article.inheritance.model;
public class Car extends Automobile {
public Car(final String color, final Integer quantityPort) {
this.setColor(color);
this.setQuantityPort(quantityPort);
}
public String getColor() {
return "perfect "+ this.color;
}
public String myColor() {
return "Color is: "+ this.getColor();
}
public String myQuantityPort() {
return "Quantity port is: "+ this.getQuantityPort();
}
}
package com.leonardorifeli.article.composition.model;
public class Job {
private String name;
private String service;
private boolean started = false;
public Job(final String name, final String service, final boolean started) {
this.name = name;
this.service = service;
this.started = started;
}
public String checkAndStartJob() {
if(this.started == true) {
return "Job has already started.";
}
if(this.started == false) {
this.startJob();
return "Job is started";
}
return "Job stoped";
}
private void startJob() {
this.started = true;
}
private void stopJob() {
this.started = false;
}
}
package com.leonardorifeli.article.composition.model;
public class People {
private String name;
private boolean hasJob;
public People(final String name, final boolean hasJob) {
this.name = name;
this.hasJob = hasJob;
if(this.hasJob == true) {
Job job = new Job(this.name, "Developer", true);
job.checkAndStartJob();
}
}
}
package com.leonardorifeli.article.inheritance.model;
public class String {
@Override
public String toString() {
return "Is a new string";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment