Consider the following classes: public abstract class Cat { private int age; private double weight; private double top_speed; public void run(double newSpeed) { ... } private void eat(double portionSize) { ... } // ... -> getXXX() and setXXX() methods here } } public class Cheetah extends Cat { private String type; public Cheetah(int age, double wt, double sp, String type) { this.type = type; setAge(age); setWeight(wt); setTopSpeed(sp); } private void run(double newSpeed) { ... } public void camouflage() { ... } // ... -> // getXXX() // and setXXX() methods here } } Which design principles are violated by these classes? Explain it and then correct it
1. You have violated Encapsulation design principle without using
getter and setter methods.
2. You don't need to write a new method "run" in the class Cheetah.
In such way you have violated Inheritance principle.
Code:
public abstract class Cat {
private int age;
private double weight;
private double top_speed;
public void run(double newSpeed) {
this.top_speed = newSpeed;
}
private void eat(double portionSize) {
this.weight += portionSize;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getTopSpeed() {
return top_speed;
}
public void setTopSpeed(double top_speed) {
this.top_speed = top_speed;
}
}
public class Cheetah extends Cat {
private String type;
public Cheetah(int age, double wt, double sp, String type) {
setAge(age);
setWeight(wt);
setTopSpeed(sp);
this.type = type;
}
public void camouFlage() {
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
Comments
Leave a comment