Geeks With Blogs

News

Server Error in Application An unhandled exception occurred during the execution of the current web request


Introduction:

                        There are lost of discussion on the internet about the Interface vs Abstract class. Also, as base class whether we have to use interface, abstract class or normal class.

 I am trying to point out few considerations on which we can take decision about Interface vs Abstract class vs Class.

 

Abstract Class vs Interface

I am assuming you are having all the basic knowledge of abstract and interface keyword. I am just briefing the basics.

We can not make instance of Abstract Class as well as Interface.

Here are few differences in Abstract class and Interface as per the definition.

Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).

Interface can only contain abstract methods, properties but we don’t need to put abstract and public keyword. All the methods and properties defined in Interface are by default public and abstract.

 

            //Abstarct Class

public abstract class Vehicles

      {

        private int noOfWheel;

        private string color;

        public abstract string Engine

        {  

            get;

            set;

        }

        public abstract void Accelerator();

      }

      //Interface

public interface Vehicles

      {

        string Engine

        {  

            get;

            set;

        }

        void Accelerator();

      }

 

We can see abstract class contains private members also we can put some methods with implementation also. But in case of interface only methods and properties allowed.

We use abstract class and Interface for the base class in our application.

 

This is all about the language defination. Now million doller question:

How can we take decision about when we have to use Interface and when Abstract Class.

Basicly abstact class is a abstract view of any realword entity and interface is more abstract one. When we thinking about the entity there are two things one is intention and one is implemntation. Intention means I know about the entity and  also may have idea about its state as well as behaviour but don’t know about how its looks or works or may know partially. Implementation means actual state and behaviour of entity.  

Enough theory lets take an example.

I am trying to make a Content Management System where content is a genralize form of article, reviews, blogs etc.

                                   

 

CONTENT

ARTICLE

BLOGS

REVIEW

 

So content is our base class now how we make a decision whether content class should be Abstract class, Interface or normal class.

First normal class vs other type (abstract and interface). If content is not a core entity of my application means as per the business logic if content is nothing in my application only Article, Blogs, Review are the core part of business logic then content class should not be a normal class  because I’ll never make instance of that class. So if you will never make instance of base class then Abstract class and Interface are the more appropriate choice.

Second between Interface and Abstract Class.

CONTENT

Publish ()

ARTICLE

BLOGS

REVIEW

As you can see content having behavior named “Publish”. If according to my business logic Publish having some default behavior which apply to all I’ll prefer content class as an Abstract class. If there is no default behavior for the “Publish” and every drive class makes their own implementation then there is no need to implement “Publish” behavior in  the base case I’ll prefer Interface.

These are the in general idea of taking decision between abstract class, interface and normal class. But there is one catch. As we all know there is one constant in software that is “CHANGE”. If I made content class as Interface then it is difficult to make changes in base class because if I add new method or property in content interface then I have to implement new method in every drive class. These problems will over come if you are using abstract class for content class and new method is not an abstract type. So we can replace interface with abstract class except multiple inheritance.

CAN-DO and IS-A relationship is also define the deference between Interface and abstract class. As we already discuss Interface can be use for multiple inheritance for example we have another interface named “ICopy” which having behavior copy and every drive class have to implements its own implementation of Copy. If “Article” class drive from abstract class Content as well as ICopy then article “CAN-DO” copy also.

IS-A is for “generalization” and “specialization” means content is a generalize form of Article, Blogs, Review and Article, Blogs, Review are a specialize form of Content.

So, abstract class defines core identity. If we are thinking in term of speed then abstract is fast then interface because interface requires extra in-direction.

So as per my view Abstract class having upper-hand in compare to interface. Using  interface having only advantage of multiple inheritance. If you don’t understand the things then don’t worry because it’s my mistake because I am not able to describe the topic.

 


Related Post:

Association, Aggregation, Composition

Talent vs Attitude – Harsha Bhogle

Does writing good content is good enough for reaching out people

 

Posted on Wednesday, July 5, 2006 10:38 AM | Back to top


Comments on this post: Abstract Class vs Interface

# Abstract Class vs Interface
Requesting Gravatar...
Trackback from DotNetKicks.com
Left by DotNetKicks.com on Jul 05, 2006 11:13 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Good analogy, especially the "CAN-DO" and "IS-A" relationship.
Left by basant on Jul 25, 2006 3:25 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Its very useful!! more informative
Left by bhuvana on Oct 12, 2006 2:38 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Hi, good article, please elaborate "CAN-DO" and "IS-A" fully.. give more examples...
Left by Mubashir hameed on Oct 27, 2006 5:30 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Hi, Thanks for your comment. I'll update this article for the "CAN-DO" and "IS-A" relationship. for any question you can mail me maheshsingh21@hotmail.com
Left by Mahesh on Oct 27, 2006 5:46 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Good!! My doubts are clear
Left by Arvind Pande on Nov 14, 2006 9:54 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
It's Preety good
Left by Murugan on Dec 20, 2006 1:02 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Abstract Class And Interface Are Real Powerful Concept
Left by Chetan on Feb 15, 2007 9:28 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
seems to be gud
Left by Animesh on Feb 19, 2007 11:14 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Very good article!! :)
It really helps to make clear understanding about interface and abstract class.
cheers!!
Left by Sahil on Feb 27, 2007 4:25 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Good Article for intermediate user. Thanks
Left by Sankar Venkat on Mar 02, 2007 5:42 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
The example is very good for understanding. For having hands on, a good example should also be included in C#.net,vb.net etc.
Left by Sumit on Mar 13, 2007 10:50 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
hi.. its really very useful...thanks
Left by Suresh M on Mar 19, 2007 7:49 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Thats cool !
Left by Tajdar on Mar 20, 2007 8:13 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
really great article,very useful..
Left by Shebna Naufel on Mar 20, 2007 12:32 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Its helpfull, I feel is bit confusing in few areas. though is confusing, its giving so much information.. thanks.
Left by chinna on Apr 03, 2007 5:23 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
A very good articles. Looking for more on same topic with more examples and detail.
Left by Balbir Singh on Apr 06, 2007 8:21 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Make it more detail, u can Include:
1. Memory management
2. How to use shared members
3. Some information related to events of abstract class.
Left by Prasant on Apr 09, 2007 7:04 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Good Artical.
Left by Kedar on Apr 19, 2007 1:19 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
I want some more info about Abstract and Interface .
Left by Nikesh on Apr 27, 2007 8:26 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Hi Mahesh,
It's really good article

but if we have two class for CONTENT
1) Normal_CONTENT
2) Interface_CONTENT

So whatever methods which are implemented(non-abstract) can be placed in Normal_CONTENT class and whatever methods which are not implemented(abstract) can be placed in Interface_CONTENT

Now if ARTICLE extends Normal_CONTENT and Implements Interface_CONTENT so if any new non-abstract method needs to declared and define then we can place it to Normal_CONTENT and if any new abstract method is needed then we can declare it to Interface_CONTENT

So, now my question is in the above scenario we not at all need abstract classes!!

Please get me some strong points which opposes my point for above scenario

Please give me some strong view of yours which proves my statement "no use of abstract class in above scenarios" false.



Left by Yagnesh on Apr 29, 2007 1:32 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Good explantion. see also:

http://maordavid.blogspot.com/2007/05/interface-vs-abstract-class.html
Left by Jo on May 11, 2007 5:28 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
clean explanation ..thanks.....
Left by Balaji on Jun 15, 2007 1:24 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
How about coupling ... using an interface you are de-coupled from implementation
Left by sim on Jun 18, 2007 9:21 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
thanks.

but please make it little more elaborate.
Left by marudham on Jun 26, 2007 12:28 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
This is the best example in the world.Keep posting good articles like this

Good luck
Sai
USA

Left by sai on Jul 31, 2007 8:47 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
great article. my vision is put on blog
Left by iatanasov on Aug 07, 2007 9:11 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Thanks. You have cleared my all time doubts on interface n abstract.
Left by Divya on Aug 16, 2007 7:20 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Get me a job
Left by problema on Sep 12, 2007 9:16 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
this is goood
Left by shiv on Sep 17, 2007 6:03 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
this is goood class
Left by shivam on Sep 17, 2007 6:05 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Part I

You are given two classes, Vehicle and Location, and two interfaces, Moveable and VehicleTransporter.

Add the rest of the classes shown in the picture below and establish the inheritance relationships shown. Note that Vehicle, Boat, and Automobile are abstract. Also, note that Vehicle implements Moveable, and Ferry and TransportTruck both implement VehicleTransporter.

For now, don’t worry about implementing methods that make sense – simply write dummy methods and make sure that the whole structure compiles. For example, if you are required to add a method called foo() that returns a Boolean value, it is enough (for now) to write it as follows:

public boolean foo()
{
return false;
}





Part II

1. Add a method called reportLocation() to the Vehicle class. This method should return a string beginning with “Location is: “ followed by the X and Y coordinates of the vehicle’s location.
2. Add a new field to Vehicle, called distanceTravelled. This should be a private field of type double. Add a get and a set method for this field.
3. Implement the move() functionality, as follows:
• Any vehicle that moves to a new location should update its location field to reflect the new location.
• Boats and Automobiles move differently. A boat can travel in any straight line, including a diagonal. An automobile, however, can only change either its X-location or its Y-location at one time. For example, to move from (1,2) to (4,6), a boat travels directly to (4,6), but an automobile first travels to (4,2) and then from there to (4,6).
• So, the distance that a boat travels in traveling to its destination is different from that of an automobile. The formula for the distance traveled for a boat is given by the Pythagorean Theorem – it is the square root of the sum of the squares of the two legs, whereas for an automobile, the distance traveled is the sum of the two legs. (The “legs” are the individual segments that an automobile follows – in the example above, the first leg is from (1,2) to (4,2), and the second leg is from (4,2) to (4,6)
• To implement this you will need the abs and sqrt functions in the Math library, which return, respectively, the absolute value and the square root of a number. You can use these functions simply by writing Math.sqrt(…). For example Math.sqrt(4) returns 2.
• To complete the example above, the distance traveled by an automobile on leg1 is 4 – 1 = 3. The distance on the second leg is 6 – 2 = 4. So the distance for an automobile is 3 + 4 = 7, whereas for a boat it is sqrt((3*3)+(4*4)) = sqrt(25) = 5.


Part III

Write a test class for your code and test your new functionality thoroughly. Make sure you include tests to show that the move() function works properly, including the distance calculation. (Each time you move an object you should report its new location and the total distance traveled – add methods for this if you wish).
Left by damaa80_hezen on Sep 26, 2007 9:59 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
nobody can help
Left by damaa80_hezen on Sep 27, 2007 8:18 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Left by Mahesh on Dec 26, 2007 6:41 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Left by Mahesh on Dec 26, 2007 6:41 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Nice explanation

http://www.codepal.co.in
Left by Bino on Dec 27, 2007 4:36 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
this infor mation solve my prblem very efficently.thenks for thet
Left by shrikant garg on Jan 16, 2008 2:53 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
nice explanation . and i got the difference between them ,thz
Left by manish on Jan 27, 2008 6:58 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
I had a gr8 understanding from this article, thank you keep it up.
Left by Ravi Shankar on Jan 28, 2008 4:09 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Hi good explanation
Left by Ashok on Feb 03, 2008 5:53 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
nice one now i know the difference
Left by ganesh on Feb 14, 2008 4:13 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Actually I know the difference b/c abstract class and interface. But I like to read articles. This article is really too good. I appreciated it.
Left by lalit lakhchaura on Feb 22, 2008 5:14 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
hello mahesh,
really thanx for simple clarification.i was not able to answer this question properly in interview but now i can. thnx
Left by dipali on Feb 29, 2008 2:13 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
That is what I was searching for...for last 1 months.
Left by Parth Dave on Mar 10, 2008 12:59 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Great...!

You have put this in very simple words. you really deserve Thanks...
Left by Narayan on Apr 04, 2008 3:17 AM

# re: Abstract Class vs Interface
Requesting Gravatar...
Hi,
I just want to know what i have doubt in my mind. Here are the things.
I have Interface IBLogic. This interface contain 10 methods. My hundred of clients implementing this interface. For some reason, I have added 2 more methods in interface. So all my clients need to implements these methods. That is fine. Again for some reason, I added 10 more methods in IBLogic interface. Then waht should be my approach?.

Actaully this question is asked me in interview. Please let us know your feedback on this.
Thanks for advance help
Left by Kiran on Aug 21, 2008 5:47 PM

# re: Abstract Class vs Interface
Requesting Gravatar...
Nice Article...! Keep the good work going...!
Left by Hemant on Aug 29, 2008 11:01 PM

Your comment:
 (will show your gravatar)


Copyright © Mahesh Singh | Powered by: GeeksWithBlogs.net | Join free