The Three Pillars of Object-Oriented Programming
Object-oriented programming is built on three pillars: encapsulation, specialization, and polymorphism.
Each class should be fully encapsulated; that is, it should fully define the state and responsibilities of that type. Specialization allows you to establish hierarchical relationships among your classes. Polymorphism allows you to treat a group of hierarchically related objects in a similar way and have the objects sort out how to implement the programming instructions.
Encapsulation
The first pillar of object-oriented programming is encapsulation. The idea behind encapsulation is that you want to keep each type or class discrete and self-contained so that you can change the implementation of one class without affecting any other class.
A class that provides a method that other classes can use is called a server. A class that uses that method is called a client. Encapsulation allows you to change the details of how a server does its work without breaking anything in the implementation of the client.
This is accomplished by drawing a bright and shining line between the public interface of a class and its private implementation. The public interface is a contract issued by your class that consists of two parts. The first part says, “I promise to be able to do this work.” Specifically, you’ll see that a public interface says, “Call this method, with these parameters, and I’ll do this work, and return this value.” The second part says, “You ...
Get Learning C# 3.0 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.