Interfaces and Abstract class
Interfaces
An Interface contains definitions for a group of related functionalities that a class can implement. Interfaces are contracts that your class should abide by.
Requirement
Suppose you are creating a Web Automation tool, that targets IE and FF browser and supports the functionality of Opening and Closing the browser.
Here you can notice that the functionalities will remain the same for both the browsers and also other new browsers which you can support in the future.
We can have individual classes implementing these functionalities for respective browsers however we want all the classes to implement the functionalities(Opening and Closing Browser) which we defined earlier.
So how can we force the class to implement the functionalities or contracts which we defined?
Well that's where interfaces help
We can define an interface or contract with all our requirements and let other browser classes provide their implementations for those functionalities
Here IE class implements WebAutoTool interface, you can do this for FF class.
Point to note here is we are creating an instance of IE class and storing in WebAutoTool interface variable
This satifies one of the design principles which states as "Program to interfaces rather than concrete class"
Once you depend on interfaces than you are decoupled from the implementation and it can vary which should not bother you.
In our above code, we are free to pass a webAutoTool object across the solution and we don't care whether that's an IE, FF or any other browser instance.
Also if we are going to decide our browser at runtime than programming to interfaces helps
All in all, an interface acts as insulation from the implementation details 👱
Abstract Class
We will stick to our earlier example of Web Automation tool, here we assume say opening, closing and some more functionalities of the browsers share the same code.
So if we are using interfaces here then we will be writing the same code for those functionalities.
Wouldnt it be great if I can add some implementation inside interface and that could be shared across the browser classes?
Well, interfaces cannot contain implementation code. So what next ?
Abstract classes can achieve above requirement, they may contain implementation code.
They are special classes which cannot be instantiated however other classes can inherit from an abstract class.
Here we defined abstract class WebAutoTool and added implementation for Open and Close methods which can be shared across all browser classes.
We defined another method called Sendkeys and added keyword abstract to it , it means child class can add their implementation for this method
We added child class IE and inherited it from WebAutTool class , we also override the implementation of the SendKeys method.
Well that's it, now we will look at some differences between abstract class and interfaces
The interface gives freedom with regard to base class and an abstract class gives you the freedom to add new methods later
An Interface contains definitions for a group of related functionalities that a class can implement. Interfaces are contracts that your class should abide by.
Requirement
Suppose you are creating a Web Automation tool, that targets IE and FF browser and supports the functionality of Opening and Closing the browser.
Here you can notice that the functionalities will remain the same for both the browsers and also other new browsers which you can support in the future.
We can have individual classes implementing these functionalities for respective browsers however we want all the classes to implement the functionalities(Opening and Closing Browser) which we defined earlier.
So how can we force the class to implement the functionalities or contracts which we defined?
Well that's where interfaces help
We can define an interface or contract with all our requirements and let other browser classes provide their implementations for those functionalities
Here IE class implements WebAutoTool interface, you can do this for FF class.
Point to note here is we are creating an instance of IE class and storing in WebAutoTool interface variable
WebAutoTool webAutoTool = new IE();
This satifies one of the design principles which states as "Program to interfaces rather than concrete class"
Once you depend on interfaces than you are decoupled from the implementation and it can vary which should not bother you.
In our above code, we are free to pass a webAutoTool object across the solution and we don't care whether that's an IE, FF or any other browser instance.
Also if we are going to decide our browser at runtime than programming to interfaces helps
All in all, an interface acts as insulation from the implementation details 👱
Abstract Class
We will stick to our earlier example of Web Automation tool, here we assume say opening, closing and some more functionalities of the browsers share the same code.
So if we are using interfaces here then we will be writing the same code for those functionalities.
Wouldnt it be great if I can add some implementation inside interface and that could be shared across the browser classes?
Well, interfaces cannot contain implementation code. So what next ?
Abstract classes can achieve above requirement, they may contain implementation code.
They are special classes which cannot be instantiated however other classes can inherit from an abstract class.
Here we defined abstract class WebAutoTool and added implementation for Open and Close methods which can be shared across all browser classes.
We defined another method called Sendkeys and added keyword abstract to it , it means child class can add their implementation for this method
We added child class IE and inherited it from WebAutTool class , we also override the implementation of the SendKeys method.
Well that's it, now we will look at some differences between abstract class and interfaces
- An abstract class may contain implementation code, interfaces cannot.
- A class can inherit from a single abstract class however it can implement multiple interfaces
- Abstract class members can contain access modifiers however in case of interfaces members are automatically public
- An abstract class can contain anything similar to concrete class however interfaces can only contain methods, properties, events and indexes, and no constructor/destructor etc.
Important points
When you add a new method to an interface than you can break all your client code.
The interface gives freedom with regard to base class and an abstract class gives you the freedom to add new methods later
Comments
Post a Comment