The Two Interfaces in OOP
Damien Cassou, Stéphane Ducasse and Luc Fabresse
http://stephane.ducasse.free.fr
Outline
- Some questions
- Two different clients
What are the Consequences of
A
{ private x ;
void foo(){ ... x ...}
}
Consequences
- cannot access x from
- cannot replace x
- cannot extend!
Cannot even copy and paste foo(){ ...} in subclasses!
Clients?
What are the clients of a class?
- Its users (e.g., Person is client of Address)
- but also its subclasse
What do you think about this
Some tutorials mention: Fields should be private
The correct idiom is...
- Fields should be private AND class should provide protected accessors
- OR
- Fields should be protected
Benefits
- Your clients cannot access your fields
- And your subclasses are empowered
- A subclass can extend/refine the behavior of the superclass
.
Conclusion
A class has always two clients:
/