Learning new concepts in a new language can be overwhelming as it definitely for me when learning Object-Oriented programming. in this blog, I will try to explain the difference between different abstraction implementations and why you should use them.
What is abstraction?
in object-oriented programming, abstraction is a way to hide information and showing only essentials. it's a way to generalize features but also define the nature of certain classes.
In C#, the abstract keyword is one of the ways to achieve abstraction in classes or method
Creating Abstract classes
Before creating abstract classes, it is important to know it is impossible to create an instance of an abstract class, which means abstract should be used with inheritance.
Creating an abstract class has the syntax of access modifier, the abstract keyword, the class's name.
the abstract class contains fields and very often, abstract methods (which we will talk about later). As we said earlier, abstract classes are usually inherited, for example in this case, a HummingBird class can derive from the abstract class and it will contain all attributes the Bird classes have.
However, we need to be careful when to inherit from an abstract class and be careful that it does not change or be very different from the abstract class. For example, if a RobiticBird inherits from the Bird class, but it changes the implementation of methods and fields from the Bird class, a new abstract class should be used and inherit from it
Interfaces
Another way to achieve abstraction is the use of Interface. interfaces are like a supercharged abstract class, they do not have fields but only contains methods which it forces classes that inherit from it to implement.
Interfaces forces implementation of methods and all methods are abstract by default, which leads smoothly to our next point
Abstract methods
Abstract methods are like abstract classes, the difference is that they achieve abstraction through methods and functions. There are two keywords for defining an abstract method, abstract and virtual methods.
Abstract method
Although using the same syntax, Abstract methods are different from Abstract classes when implementing them. When defining abstract methods, think of them like defining Interfaces because an abstract method does not have a body, but forces the inherited class from the method's class to implement it
The abstract method only determines the return type, an error will show if the method has a body.
Virtual method
Unlike abstract methods, Virtual methods can have a body in them and do not needs to be implemented. however, if the derived class does not implement it, it will still be able to use it and it will use it however the abstract class defines it
The class Eagle was forced to override the method fly but does not need to for Makesound function, if an Eagle object calls the Makesound method, it will just use the one defined by the Bird class
Quick summary
■ Interfaces only contain abstract methods and forces child-class to implement it
■ Abstract classes can have fields and Methods
■ Cannot create an instance of Interfaces and Abstract classes
■ Abstract methods do not have a body and forces child-class to implement it
■ Virtual methods can be used by child-class without re-implementing it
Conclusion
This was my take on explaining Abstraction and implementing it as a beginner, I hope that this blog has helped you to understand abstraction in object-oriented programming. The real challenge is not learning syntax and how to write them but to think about when abstraction is appropriate to help you build better programs, and I hope you will start thinking about those soon when you start building more complex programs.
Top comments (0)