Defining the Pillars of OOP
All object oriented languages must contend with these 3 core principles, often called the pillars of OOP:
- Encapsulation
- Inheritance
- Polymorphism
Understanding the Role of Encapsulation
This trait boils down to the language's ability to hide unnecessary implementation details from the object user.
DbReader r = new DbReader();
r.Open(@"C:\test.mdf");
r.Close();
The fictitious DbReader class encapsulates the inner details of locating,loading, manipulating and closing a data file.
Understanding the Role of Inheritance
Inheritance boils down the language's ability to allow you to build new class definitions based on existing class definitions. In essence, inheritance allows you to extend the behavior if a base (or parent) class by inherting core functionality into the derived sublcass (also called a child class).
"is-a relationship"
There is another form of code reuse in the world of OOP: the containment/delegation model also known as the "has-a" relationship at aggreegation. This form of reuse is not used to establish parent-child relationships. Rather the "has-a" relationship allows one class to define a member variable of another class and expose its functionality (if required) to the object user indirectly.
For ex :
class Radio
{
public void Power(bool turnOn)
{
Console.WriteLine("Ok");
}
}
class Car
{
// has-a
public Radio radio = new Radio();
public void TurnOnRadio(bool onOff)
{
radio.Power(onOff);
}
}
Understanding the Role of Polymorphism
This tenant of oop language allows a base class to define a set of members that are available to all descendants. A class's polymorphic interface is constructed using any number of virtual or abstract members.
Shape s = new Shape[2];
s[0] = new Circle();
s[0] = new Hexagon();
foreach(Shape shape in s)
{
shape.Draw();
}