130 likes | 248 Vues
This interlude explores the concepts of abstract classes and interfaces in programming. It defines their behavior, implementation constraints, and their roles in the object-oriented paradigm. Abstract classes cannot be instantiated and can inherit from one single abstract class, while interfaces allow multiple implementations. Key decisions on when to use each are discussed, emphasizing code reuse and encapsulation. The guide also touches on the importance of managing change in software development and provides solutions to avoid common pitfalls in design.
E N D
HFOOAD Chapter 5 Interlude OO Catastrophe!
Two possible answers Interface Abstract class Defines behavior Can have implementation code Cannot be instantiated A class can inherit from a single abstract class Unless the language supports multiple inheritance • Defines behavior • Contract • Cannot be instantiated • A class can implement multiple interfaces • In languages that support interfaces
How do I know when to use an interface and when to use an abstract class? If (almost) all classes implementing the behavior would have the same code, then you can use an abstract class to implement it. Avoid repeating code with abstract classes Program to interfaces
ENCAPSULATION Isn’t encapsulation just about hiding implementation?
Prefer delegation over inheritance
Manage change Manage change Manage change … Change Change Change Change