Many of you are still in confusion that when I should use Interface and when Abstract Class while developing applications or for preparing for an interviews. As these are part of core concepts of Object oriented programming languages, all of us should know exact use case.
As you all know bookish definitions of both concepts, we will jump to simple example directly to explain the actual use cases of both.
Imagine you are working on a system which is representing list of Colleges which will have Engineering Colleges, Medical Colleges and regular degree colleges. Now there are few things which are must when it comes to any college like Black(green) Board, Classrooms which we can have in our Interface, it will be implemented later based on types of colleges i.e. Classes which will be implementing that Interface.
But if all the colleges are having this then what does Abstract Class does ?? Big question right..
Here is the answer of that, Abstract class is for some common behavior that can be inherited by it's sub-classes, and not necessary to implement that entire class. We will go to Abstract class later, before that we should know what kind of groups are there in our use case which share common behavior like we can have groups like Engineering Colleges, Medical colleges etc. for which we can have abstract class. These abstract classes further will be implemented by sub classes like (set of classes representing) Medical colleges AND/OR Engg colleges. Lets take an example of Medical colleges classes, all Medical colleges have operation theaters but there is a difference between Op theater's architecture (structure) of accident's special hospital and Vets' special but since Op theater is must in each n every medical college, we can make it abstract.
Hope you have understood the difference between Method of an Interface and abstract method. If not yet then just remember one thing that Interface will be used by similar or different functionality classes with common contract but abstract class will be used by subclasses with partially similar functionalities.
Our Abstract classes will be Medical colleges specific, Engineering colleges specific which will take care of required abstraction which will be specific as well as common for that particular group.
So in short we will have interface for the things which we need to force (mandatory) in contract (architecture) and Abstract class for shared behavior. Hope you have understood this and enjoyed. Will improve this with example soon. Thanks for reading.
As you all know bookish definitions of both concepts, we will jump to simple example directly to explain the actual use cases of both.
Imagine you are working on a system which is representing list of Colleges which will have Engineering Colleges, Medical Colleges and regular degree colleges. Now there are few things which are must when it comes to any college like Black(green) Board, Classrooms which we can have in our Interface, it will be implemented later based on types of colleges i.e. Classes which will be implementing that Interface.
But if all the colleges are having this then what does Abstract Class does ?? Big question right..
Here is the answer of that, Abstract class is for some common behavior that can be inherited by it's sub-classes, and not necessary to implement that entire class. We will go to Abstract class later, before that we should know what kind of groups are there in our use case which share common behavior like we can have groups like Engineering Colleges, Medical colleges etc. for which we can have abstract class. These abstract classes further will be implemented by sub classes like (set of classes representing) Medical colleges AND/OR Engg colleges. Lets take an example of Medical colleges classes, all Medical colleges have operation theaters but there is a difference between Op theater's architecture (structure) of accident's special hospital and Vets' special but since Op theater is must in each n every medical college, we can make it abstract.
Hope you have understood the difference between Method of an Interface and abstract method. If not yet then just remember one thing that Interface will be used by similar or different functionality classes with common contract but abstract class will be used by subclasses with partially similar functionalities.
interface IColleges { void subBoard(); void subClassRoom(); } public abstract class clsAbsMedical { public abstract void subLab(); } public class clsMedical : clsAbsMedical, IColleges { public void subBoard() { // Board as per req... } public void subClassRoom() { // Class Room as per req... } public override void subLab() { // Lab as per req... } } public abstract class clsAbsEngine { public abstract void subWorkShop(); } public class clsEngine : clsAbsEngine , IColleges { public void subBoard() { // Board as per req... } public void subClassRoom() { // Class Room as per req... } public override void subWorkShop() { // Work Shop as per req... } }
Our Abstract classes will be Medical colleges specific, Engineering colleges specific which will take care of required abstraction which will be specific as well as common for that particular group.
So in short we will have interface for the things which we need to force (mandatory) in contract (architecture) and Abstract class for shared behavior. Hope you have understood this and enjoyed. Will improve this with example soon. Thanks for reading.