Your SlideShare is downloading. ×
Interface Vs Abstact
Interface Vs Abstact
Interface Vs Abstact
Interface Vs Abstact
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Interface Vs Abstact

210

Published on

Interface vs Abstract Class

Interface vs Abstract Class

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
210
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. INTERFACE VS ABSTARCT CLASS Ever thought of their differences in real time? This is the frequent important question asked inthe interviews. Many people give a canned response like “You can define default functionality in anabstract class and you can just define functions in an interface”. The curve ball is thrown when you ask“Why would you use one over the other?” That will earn you the ‘deer in headlights’ look. Some reply “Inever had to use that so I don’t know”. So what are the great differences these two entities have inSoftware Development?At the top level, there are a few basic differences. Abstract classes allow for default function definition.This means that whatever class extends the abstract class will have access to this. If we have a base classwhere all the classes will perform the same function, then we can define that in our Abstract class. Aninterface is a list of functions or properties that if a class implements it, it will have to have thosefunctions defined within it. It is a situation of “Is-A” vs. “Can-Do-this”. Objects that extends an Abstractclass “Is-A” base class. Objects that implement “Can-Do-This”. Now if I asked this question and got theanswer, yes, that would be the correct answer. However, I want to know why one would want to use aninterface over an abstract class, and vice versa.In this post I explain where exactly the interface is used and so do the abstract class.Interface and its use: • It is Purely Abstract • It’s having Fixed Functionality • If an Interface changed, all the underlined classes will change.Example: Inject Business Method to an Interface A (New Story or Feature) Where to add it?Belongs to Interface A? Not belongs to Existing Interfaces?Add to Interface A Add a new Interface BAll underlying class changes May be used in new classes or some (not all) of the Underlying classes of Interface ACalled Extensibility of Interface A Called Multiple Inheritance. • This scenario is not applicable when Interfaces (fixed) are sold across Business. o Example: Service Contracts
  • 2. • This is used when no need of Massive hierarchical framework.Example 1: Inherits implementsInterface A Class B <--------------- Class BExample 2:Let’s have an interface for a Director and another interface for an Actor. Public interface Actor { Performance say (Line l); } Public interface Director { Movie direct (boolean goodMovie); } • In reality, there are Actors who are also Directors. If we are using interfaces rather than abstract classes, we can implement both Actor and Director. • We could even define an ActorDirector interface that extends both like this: Public interface ActorDirector extends Actor, Director { ... } • We could achieve the same thing using abstract classes. • By using Abstract class would require up to 2^n (where n is the number of attributes) possible combinations in order to support all possibilities. Ex: ActorDirector, DirectorActor…..Why to prefer an Abstract Class? Abstract classes allow you to provide default functionality for the subclasses. Commonknowledge at this point. Why is this extremely important though? If you plan on updating this base classthroughout the life of your program, it is best to allow that base class to be an abstract class. Why?Because you can make a change to it and all of the inheriting classes will now have this newfunctionality. If the base class will be changing often and an interface was used instead of an abstractclass, we are going to run into problems. Once an interface is changed, any class that implements thatwill be broken. Now if it’s just you working on the project, that’s no big deal. However, once yourinterface is published to the client, that interface needs to be locked down. At that point, you will bebreaking the client’s code. • Its contains Default Functionality & abstract methods Concrete Methods Used in Versioning by overriding the default functionality in new version (class that extends previous abstract class)
  • 3. • Abstraction used with polymorphism is called Versioning. • If we have common functionality used in a hierarchy of classes then put them in Abstract class. Whenever we need to override the existing functionality we should cut a branch of underlying classes and inherit from parent abstract class (which contains common functionality). Example: Some common functionality is used across the hierarchy B-C-D-E-F. If you want to change the functionality how the D object works then you need to cut the tree and attach E-F to A. Common Common functionalit Abstract Class functionalit y AA y A A B CA Used D Overridden With new functionality Cut TreeE EF F Frameworks are a good place to show when and where to use both an abstract class and an interface. Another general rule is if you are creating something that provides common functionality to unrelated classes, use an interface. If you are creating something for objects that are closely related in a hierarchy, use an abstract class. An example of this would be something like a business rules engine. This engine would take in multiple BusinessRules as classes perhaps? Each one of these classes will have an analyze function on it. public interface BusinessRule{ Boolean analyze(Object o); }
  • 4. This can be used ANYWHERE. It can be used to verify the state of your application. Verify data iscorrect. Verify that the user is logged in. Each one of these classes just needs to implement the analyzefunction, which will be different for each rule. Where as if we were creating a generic List object, the use of abstract classes would be better.Every single List object is going to display the data in a list in some form or another. The basefunctionality would be to have it go through its dataprovider and build that list. If we want to changethat List object, we just extend it, override our build list function, change what we want and callsuper.buildList(); Almost everyone knows that interfaces means you are just defining a list of functions and thatabstract classes has the option of providing default functionality. The snags come when you drop the‘why would I use one over the other?’ Abstract classes and interfaces are some of the most importantfundamentals of object oriented programming. Just knowing the differences between the two is notenough. When you can look at a situation and make a strong recommendation, you will know you havea much stronger knowledge of object oriented programming. Also it helps during interviews.

×