Object Oriented World
Introduction
Our world is full of objects and whenever I see anything I see it as an object, no matter whether it’s complex or simple but eventually I find it one object which maybe composed of smaller objects!
I am just thinking so much about Object Oriented World, and I wonder about its beauty and instrumentation, and simply I find that everything in this world is an object, think of your monitor in front of you now, you will simply find out it's an object it has input and output ports and it has some buttons, all these small parts are encapsulated tiny or big objects.
Metaphorically I will talk about the Object oriented world compared to some real world objects.
Encapsulation and Abstraction
Encapsulation is a very important concept in OOW ( I will refer to Object Oriented World using this shortcut ), it's the thing that makes you deal with any object with no need to delve into the inners of it, this concept is employed in almost all the modern industries, for example when they manufacture cars they are not interested in knowing the detailed design of the AC system, they just care about the inputs and outputs of it, so you are away from the inner details and once you enter the inputs and you get the expected behavior/output that's it !! , so what if you are asked to do one complex configuration setting before plugging the AC to the car, it may be one tedious and risky operation, so maybe the AC manufacturer releases you from this step and he makes it himself professionally since he's the owner of the object, this way we can say he's added one more abstraction layer to the AC object, actually this is what Microsoft is doing with every new release; take .NET 2.0 for example you will find more controls added to be used out of the box and these controls are developed to bypass some workarounds or as a response to customers/developers requirements, SQLCacheDependency just came to my mind, developers were implementing one workaround to just implement SQL invalidation (you can do so by just writing one text file and write date and time to it and once the data is changed at the database table one trigger is triggered and it writes the DateTime to the text file , and once the text file is changed the callback function related to this file is executed, note that ASP.NET 1.x only has file dependency and there is no built-in capability to manage SQL Cache, that also requires the SQL Server and the web server to be on the same machine to monitor the textual file written by SQL Server and this is a big obstacle since you often have one server for database and another for web server).
Inheritance
Inheritance is everywhere; we inherit our parents’ genome properties for example, and sometimes we deviate from that , sometimes we have the property but we act or do in another different way, maybe we acquire new features that never existed for our parents.
So let’s review it from the technical point of view, suppose you have a Class which is called “Transport” , and let me please show you the difference between the Class and the Object as many developers using the terms to give the same concept but actually they different; the Class is the core implementation for some functionality or entity , while the Object is one instance from that class, so if you have a class called Car , objects instantiated from this Class maybe : Toyota , BMW , Jaguar , etc.., so Transport is our base class , sometimes called Super Class, and we may have some other classes inheriting from this class which might be Car, Train, Plane, Motorcycle, etc..
So if the Transport has some specifications and actions (properties and methods), let’s say that any Transport Class will have a color, weight, speed per KM, Luxury Factor; these are the properties. And it can have some actions like Move, Stop.
Car, Train, Plane, Motorcycle all inheriting from the Transport class, so they hold all these properties and operations, but we all know that the Move operation of the Car is different than that of the Plane, so the Move method of the base class should be overrideable, or virtual ( in terms of C#, C++ ), and each and every child class or sub class should implement the Move method based on its internal implementation, and also sub class may have extra methods/properties as the car may have a property called GearType while we cannot have this property to the Plane class.
Inheritance is very simple and it’s already easy to be viewed everywhere, sometimes you will need to make your super class as an empty one holding no implementation as explained above we should have the Transport class as an abstract class as you can never one real instance called transport but you already have Car , Motorcycle, etc…, so you will need to add any implementation for the Move method while you will need for the subclasses, if you need to provide some common functionality for your sub classes you should add it to your super class which will be called a Concrete class.
OOP Facts:
- For C# if you have one abstract method/property for your class this will make your class abstract one, despite the fact that in C#, abstract class can have concrete and abstract methods/properties.
- Whenever you call sub class constructor the super class constructor is called behind the scenes before executing the subclass constructor code, this just like when you instantiate any class the constructor is called as well automatically.
- For the destructors it’s executed in the reverse order, it means first the destructor of the subclass is executed then the destructor of the base class is implicitly executed.
- You may have chained constructors/destructors; this actually happens whenever you have n-level inheritance hierarchy, so all the constructors/destructors chain is executed till you go to the Object’s constructor/destructor, remember that Object is the parent for each and every class in C# ( and in .NET as a whole ).
- Also you should note that whenever GC (Garbage Collector) start to remove a class it calls that class destructor.
- If you have a class called Car, destructor should be ~Car, and you can force GC to start collecting by calling this : GC.Collect();
Polymorphism
Polymorphism is a very important and interesting concept in Object oriented world; let’s see the official dictionary definition for polymorphism:
- (chemistry) the existence of different kinds of crystal of the same chemical compound
- (biology) the existence of two or more forms of individuals within the same animal species (independent of sex differences)
Let me show it right from another different angle; we can say that “Dog is an Animal”, and also we can say that “Lion is an Animal”; on the other side we cannot say that “Animal is a Dog”, it makes sense huh! Okay for development purposes suppose that you have our class which is Transport and just inside your code you need to have a variety of classes that are inherited from the Transport Class, and your objective is to loop for all the objects in your application and then call one specific method that exists in the Transport Class, you can just call every class by itself, while at the same time you can just create on array and decide the type of the array to be Transport then you can fill this array with any kind of classes that are inheriting from the Transport class, that’s why simply as we’ve said the Car is Transport, Train is Transport, Plane is Transport and so on, and during runtime the compiler will be responsible to check the current data type of the array item and it will call the specific correct method of the child class not the base one, Amazing right ?!, this gives us, developers, ability to never care about the specific and right methods to called because it’s a duty of the compiler.
Polymorphism is very useful to make your system extensible, because once you need to add one more new type; you can do that easily by inheriting form the base class.
Case Study:
I remember that once I was developing a Meta search engine that has various kinds of searches, like web, image, audio, and video, I was searching other search engines and getting a collection of search results, I created a base class called SearchResult, and then I created WebSearchResult, ImageSearchResult, AudioSearchResult, and VideoSearchResult, it helped so much since I had a class called SearchEngine that has a property called SearchType this property may have any of the 4 kinds ; web, image, audio, and video. So based on the value of this property I was searching for whatever kind of search required, but the beauty was that I was getting results as a collection of the base class SearchResult regardless of the type of search I am executing, then I was looping in this collection of results and calling one common method which is Show(), nothing that I never cared about the method to be called as during design time the SearchResult type already has a method called Show(), but during execution time the compiler was calling the correct required method which could be WebSearchResult.Show(),ImageSearchResult.Show(), AudioSearchResult, and VideoSearchResult. And note that Show method had different implementation for each different class, for example for the Image search Show , shows thumbnail, and size, and physical storage size in KB, while for Video results I was adding the length of the video file, that was one of the cases I used Polymorphism professionally and it helped so much!
Interface, Interface Inheritance, and Interface Polymorphism
What Interface is? It’s simply a description or layout for a class, sometimes they call it software contract, suppose that you have some classes that are not relevant to each other at all but you want to impose some properties/methods to make sure they are included and implemented during class implementation, if you’re in this situation so you gotta know that you are in need to use Interfaces!
Assume you have a class called Computer, another called Car, another called Horse, do you notice something common! Absolutely not, so what if you need to impose some properties/methods to be implemented maybe it’s Speed as a property and Run as a method , and of course the Speed unit is completely different for each type of these classes, and the Run operation as well, so we can define one Interface called IFunctionality, which has Speed property and Run method, and you should also tell the compiler that all of these classes will implement this interface, afterwards the classes will be forced to provide some implementation for the properties and methods mentioned in this interface, remember the relationship between sub class and base class is “inherits” but between a class and an interface it’s “implements”. You can also use abstract empty classes and interfaces interchangeably.
Conclusion
In this post I covered most of the concepts of Object oriented design/programming, I didn’t intend to give code examples as you will find it distributed everywhere but I just cared about the concepts, hoping I added you something !
<< Home