You know that it is not a good practice to use destructors to clean up your objects. You are not supposed to disrupt the garbage collection process either. The “System.Object” class has the “Finalize()” method. But in C#, you cannot override this method. You need to use the destructor instead.
So how do we cleanup used objects or resources?
No doubt that you have heard of “Interface-based Programming”. In OOP, polymorphism is one of the key features but inheritance-based programming has its limits.
Interfaces help you define the prototype of the functionality, implement the concrete logic in appropriate classes, and invoke the right implementation in a polymorphic way.
Polymorphism is what makes OOP so unique and powerful.
In C#, polymorphism works with “virtual / abstract” methods and “override” methods. But using these keywords can be tricky in some cases and might result in confusing results.
Before I started to use C#, I had used Java for a while. It was quite a lot of work to provide getters and setters for each model class. C#’s Property syntax is a nice addition. The syntax is cleaner (Not everybody agrees, though) and sometimes it saves a lot of coding efforts.
The “System.Object” class is the ultimate (master) base class of all classes in the .NET Framework, which means:
- All methods in the “System.Object” class are available in all .NET objects
- All classes can be casted into the “System.Object” type
When you create a custom class, you do not need to override any methods in the “Object” class. But sometimes the default implementation of the methods by the “Object” class might not be appropriate for your need. Let’s find out what the “Object” class offers to us. Continue reading
This is the third article of the Three Pillars of OOP. This time, let’s look at the third pillar – Polymorphism.
This is the second article of the Three Pillars of OOP. This time, let’s look at the second pillar – Inheritance.
Encapsulation, Inheritance, and Polymorphism; they are called three pillars of OOP.
Let’s look at the first pillar among three – Encapsulation. Subsequent posts will deal with other 2 pillars.