How C# Supports Object-Oriented Concepts
Okay, how does C# implement object-oriented programming? In a sense, this is the wrong question. C# is an object-oriented language; however, it doesn’t implement object-oriented programming — the programmer does. You can certainly write a non-object-oriented program in C# or any other language (by, for instance, writing all of Microsoft Word in
Main()). Something like “you can lead a horse to water” comes to mind. But you can easily write an object-oriented program in C#. These C# features are necessary for writing object-oriented programs:
- Controlled access: C# controls the way in which class members can be accessed. C# keywords enable you to declare some members wide open to the
protectedfrom view and some secrets are kept
private. Notice the little hints.
- Specialization: C# supports specialization through a mechanism known as class inheritance. One class inherits the members of another class. For example, you can create a
Carclass as a particular type of
- Polymorphism: This feature enables an object to perform an operation the way it wants to. The
Vehiclemay implement the
Startoperation much differently from the way the
Vehicledoes. But all
Startoperation, and you can rely on that.
- Indirection. Objects frequently use the services of other objects — by calling their public methods. But classes can “know too much” about the classes they use. The two classes are then said to be “too tightly coupled,” which makes the using class too dependent on the used class. The design is too brittle — liable to break if you make changes. But change is inevitable in software, so you should find more indirect ways to connect the two classes. That’s where the C#
interfaceconstruct comes in.