OOPS
-
date_range 21/12/2017 00:00 infosortIntroductionlabelObjectsandClasses
Please visit the link above before proceeding any further.
Encapsulation deals with hiding complexity and building models which logically group together an functionality. Inhertiance and Polymorphism deals with reusing of code and as well as data. In C# we can’t inherit from single to many classes even though the chain of inheritance can be unlimited. Polymorphism means many shapes. One variable can point to different objects as long as they are inherited and will behave differently based on their type. Functions from base class which will be inheriting and use polymorphism must have the virtual
keyword. And the function which is over writing the base class function must have the override
keyword. It is not very adivisble to use Inhertiance and Polymorphism in project with lot of dependencies and very frequent changes.
Abstract Classes
The amount of rigidity between inheritance and polymorphism is very hard to break and this becomes problem when we need to change the existing code /design. This can be overcome by the use of abstract classes or interfaces.
Abstract Classes can’t be instainted but can be inherited by other classes and the inherited classes can have their defintions of the classes. Methods in the abstract class are just defintions of the functions which will be implemented by the inhertited classes.
public abstract class Window
{
public virtual string Title {get; set;}
public virtual void Draw(){}
public abstract void Open(){}
}
Interfaces
Interface in type in .NET which never contains any details about implementation but serve as contract between class and its implementation. Its like an API for an object and it will have all functionality of the API if implemented by the object. It is ideally the signature of events, methods and properties. Unlinke inheritance Interfaces can be implemented by multiple classes.
public interface IWindow
{
string Title {get; set;}
void Draw();
void Open();
}
** More on this will be explained in the upcoming posts