Solid Design Principle # Open - Closed
1 min readDec 25, 2019
Open = Open for extend existing behavior
Closed = Closed for change existing code
package solid.openclosed;
public abstract class Animal {
protected String name; //closed to modification
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
abstract public void move(); //open for extension
public void callMe() {
System.out.println("My name is " + getName());
}
}package solid.openclosed;
public class Dog extends Animal {
@Override
public void move() {
System.out.println("I can Run.");
}
}import solid.openclosed.Animal;
import solid.openclosed.Dog;
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.setName("MooMoo");
dog.move();
dog.callMe();
}
}
Run แล้วจะได้
I can Run.
My name is MooMoo