At the very heart and soul of object-oriented programming (OOP) are five concepts: abstraction, encapsulation, inheritance, polymorphism, and containership.
The idea of abstraction is to reduce an object to its core essence so that only the most essential elements are represented. An example of this would be a simple phone number (803)222-5555. This number is made up of three parts: the area code, the prefix, and the number. It is much easier to deal with one object as the phone number, while knowing that it is actually made up of these three parts.
The term encapsulation is used to describe the process of controlling the contents of an object directly, as well as making the implementation of an object unknown to its consumer. Let's say that we create a class called PhoneNumber. The idea of encapsulation allows us to not really care about what the three parts of a phone number are, or for that matter, how they are generated. The consumer just wants to be able to create an instance of our class and get a valid number in return. We control access to our objects by using the access modifiers: public, protected, internal, and private.
Inheritance allows for the reuse of a class. The traditional example is that of creating a simple base class of type Animal. This class contains the basic functionality that is consistent across all animals, such as breathing, eating, mating, and so forth. We can then generate new objects by deriving a new class from our existing class Animal, without needing to provide the basic functionality of an Animal. This allows us to focus on the Dog class that we are trying to create. But this doesn't mean that we do not have the option of overriding the behavior that we inherit from Animal, because we can use polymorphism, as described in the next section.
C# allows for only single inheritance of classes and multiple inheritance of interfaces. It also provides a means to prevent inheritance by using the sealed keyword.
Polymorphism allows for you to define base classes that include common functionality via methods on groups of related objects, without regard to the type of the particular object. To implement this functionality, use the virtual keyword in the base class, which allows you to call the correct method on the basis of object type, rather than the reference type. For example, suppose that we create an instance of a Dog class that inherits from an Animal base class. The Animal base class provides for basic functionality such as eating. If we wish to ensure that the Dogs.Eat method is always called, regardless of the type that holds a reference to the Dog object, we will need to mark the Animals::Eat method as virtual and the Dog:Eat methods as override.
You may extend a class by using the concept of containership in addition to inheritance. The idea is that, instead of deriving a new class from an existing base class, you just include the class as a member of the new class.
The following example demonstrates OOP concepts.
using System; namespace Client.Chapter_5___Building_Your_Own_Classes { class Inheritance { static void Main(string[] args) { //Creates an instance of D objects B MyB = new D(); D MyD = new D(); //Both result in D's instance of Display being called MyB.Display(); MyD.Display(); } } public class B { public virtual void Display() { Console.WriteLine("Class B's Display Method"); } } public class C: B { public override void Display() { Console.WriteLine("Class C's Display Method"); } } public class ContainedClass { int MyInt = 0; } public class D: C { public ContainedClass MyClass = new ContainedClass(); public override void Display() { Console.WriteLine("Class D's Display Method"); } } }