본문 바로가기
공부/Object-Oriented Design

3.5 – Inheritance Issues

by 혼밥맨 2021. 9. 26.
반응형

3.5 – Inheritance Issues

 

Abstraction, Encapsulation, Decomposition, Generalization. Each of these principles requires you to make a decision on how they apply to a system.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Pizza {
    private List toppings;
    private String size;
    private String crustStyle;
 
    public Pizz(String size, String crust) {
        this.toppings = new ArrayList();
        this.size = size;
        this.crustStyle = crust;
    }
 
    public void addTopping(String topping) {
        this.toppings.add(topping);
    }
 
    public void bulkAddTopping(ArrayList toppingList) {
        this.toppings.addAll(toppingList);
    }
 
    public void cook() throws InterruptedException {
        wait(10*6000);
    }
}
cs

 

The pizza class has been generalized to know what toppings it will have, its size, style of crust, and how long it will take to cook. This seems reasonable, but let us look at why this is a misuse of inheritance by examing the code for a subclass of pizza. 

 

 

Liskov Substitution Principle

This principle states that a subclass can replace a superclass, if and only if, the subclass does not change the functionality of the superclass.

반응형

'공부 > Object-Oriented Design' 카테고리의 다른 글

3.7 – UML State Diagram  (0) 2021.09.27
3.6 – UML Sequence Diagram  (0) 2021.09.26
3.4 – Conceptual Integrity  (0) 2021.09.22
3.3 – Information Hiding  (0) 2021.09.21
3.2 – Separation of Concerns  (0) 2021.09.21

댓글