Polymorphism and Encapsulation in .NET

Polymorphism and Encapsulation in .NET


Polymorphism and encapsulation are two big words in OO development, and are also a fundamental concept of software development. This article will demystify these concepts by showing you some real world examples.


Polymorphism is a simple concept that you understand right now, as it is prevalent through life. You are a person, which person is a base type and you are a more specific representation of that type. There are many people that inherit that type, of which you are a concrete implementation of that type: you have your own beliefs, attributes, and qualities that go beyond just a general person. For instance, we all have a blood type, shoe size, arm length, and many other properties. But we each add our own implementation to the person interface.
You drive an automobile, which this type has properties like wheel size, engine size, gas tank size, and other properties. The automobile you drive is a concrete implementation of the automobile interface. It has additional features, like sliding doors, logo type, cd changer slot count, moon roof lever location, or other various properties that are specific to the make/model of the car. In addition, automobile may have certain behaviors like open/close door, open trunk, turn wheel, and other behaviors that would be specific to an automobile.
In OO programming, using the automobile example, Automobile would be the base class, and each automobile manufacturer would have its own implementation. For instance, Honda has V-Tec technology, which is in its own implementation. Volvo uses diesel engines, which is the TDI technology. More importantly, you may add an added level of detail between automobile and the make/model implementation, such as Car, Truck, or Suv supertypes, to provide more relevant information.
Let's take a look at the automobile scenario. Here is an example automobile class:

01.public abstract class Automobile
03. //Properties
04. public int DoorCount { .. }
05. public string EngineType { .. }
06. public float Height { .. }
07. public float GasTankSize { .. }
08. public float WheelBase { .. }
09. public float WheelSize { .. }
11. //Abstract Properties
12. public abstract string BodyType { get; }
14. //Methods
15. public float CalculateFuelEfficiency(int lastMileage);

The Automobile class is the interface, in that it defines an interface that classes will derive from. You can't directly instantiate it through new Automobile() because it's not an implementation; however, you can cast derived types to the Automobile type. In this sense, it defines a common interface across many derived implementations.
It's important to note the BodyType property. This property is declared abstract, meaning that any derived implementation must override it and return some relevant string (we will see more of this with the next class definitions). This is similar to the Template Method pattern in that it exposes properties/methods that derived classes must override, but which Automobile doesn't need to know anything about. Automobile wants the derived class to return something relevant to it, but doesn't care what.
Now, from the example above, let's say we want a higher level of abstraction, in that there are varying types of cars and we want to target that. It is possible for classes to inherit from Automobile, but to also be an interface in that it can't be directly instantiated, but any class can inherit additional properties/methods from it. Below are two such class definitions, for Car and Suv.

01.public abstract class Car : Automobile
03. //Properties
04. public bool HasSpoiler { .. }
05. public bool IsHatchback { .. }
07. //Overridden Properties (abstract)
08. public override string BodyType { get { return "Car"; } }
10. //Abstract properties
11. public abstract bool IsFast { get; }
14.public abstract class Suv : Automobile
16. public bool CanOffroad { .. }
17. public int TowingCapacity { .. }
19. public override string BodyType { get { return "SUV"; } }

Notice the BodyType method is overridden to return a string that represents the type of body that's appropriate to the supertype. Also notice that Car and Suv also expose their own properties/methods, even abstractly in terms of the Car.IsFast property. Lastly, let's create an implementation of the Car class:

1.public class SubaruWrx : Car
3. public override bool IsFast
4. {


No responses found. Be the first to comment...