Server Error in Application

An unhandled exception occurred during the execution of the current web request
posts - 60 , comments - 75 , trackbacks - 50

My Links

News

Archives

Post Categories

ASP.NET

Abstract Class vs Interface


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

 

Print | posted on Wednesday, July 5, 2006 10:38 AM | Filed Under [ ASP.NET C# Web Services Design Pattern unhandled exception ]

Feedback

Gravatar

# Abstract Class vs Interface

Trackback from DotNetKicks.com
7/5/2006 11:13 AM | DotNetKicks.com
Gravatar

# re: Abstract Class vs Interface

Good analogy, especially the "CAN-DO" and "IS-A" relationship.
7/25/2006 3:25 PM | basant
Gravatar

# re: Abstract Class vs Interface

Its very useful!! more informative
10/12/2006 2:38 AM | bhuvana
Gravatar

# re: Abstract Class vs Interface

Hi, good article, please elaborate "CAN-DO" and "IS-A" fully.. give more examples...
10/27/2006 5:30 AM | Mubashir hameed
Gravatar

# re: Abstract Class vs Interface

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
10/27/2006 5:46 AM | Mahesh
Gravatar

# re: Abstract Class vs Interface

Good!! My doubts are clear
11/14/2006 9:54 AM | Arvind Pande
Gravatar

# re: Abstract Class vs Interface

It's Preety good
12/20/2006 1:02 PM | Murugan
Gravatar

# re: Abstract Class vs Interface

Abstract Class And Interface Are Real Powerful Concept
2/15/2007 9:28 AM | Chetan
Gravatar

# re: Abstract Class vs Interface

seems to be gud
2/19/2007 11:14 PM | Animesh
Gravatar

# re: Abstract Class vs Interface

Very good article!! :)
It really helps to make clear understanding about interface and abstract class.
cheers!!
2/27/2007 4:25 AM | Sahil
Gravatar

# re: Abstract Class vs Interface

Good Article for intermediate user. Thanks
3/2/2007 5:42 AM | Sankar Venkat
Gravatar

# re: Abstract Class vs Interface

The example is very good for understanding. For having hands on, a good example should also be included in C#.net,vb.net etc.
3/13/2007 10:50 AM | Sumit
Gravatar

# re: Abstract Class vs Interface

hi.. its really very useful...thanks
3/19/2007 7:49 AM | Suresh M
Gravatar

# re: Abstract Class vs Interface

Thats cool !
3/20/2007 8:13 AM | Tajdar
Gravatar

# re: Abstract Class vs Interface

really great article,very useful..
3/20/2007 12:32 PM | Shebna Naufel
Gravatar

# re: Abstract Class vs Interface

Its helpfull, I feel is bit confusing in few areas. though is confusing, its giving so much information.. thanks.
4/3/2007 5:23 PM | chinna
Gravatar

# re: Abstract Class vs Interface

A very good articles. Looking for more on same topic with more examples and detail.
4/6/2007 8:21 PM | Balbir Singh
Gravatar

# re: Abstract Class vs Interface

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.
4/9/2007 7:04 AM | Prasant
Gravatar

# re: Abstract Class vs Interface

Good Artical.
4/19/2007 1:19 PM | Kedar
Gravatar

# re: Abstract Class vs Interface

I want some more info about Abstract and Interface .
4/27/2007 8:26 AM | Nikesh
Gravatar

# re: Abstract Class vs Interface

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.



4/29/2007 1:32 PM | Yagnesh
Gravatar

# re: Abstract Class vs Interface

Good explantion. see also:

http://maordavid.blogspot.com/2007/05/interface-vs-abstract-class.html
5/11/2007 5:28 AM | Jo
Gravatar

# re: Abstract Class vs Interface

clean explanation ..thanks.....
6/15/2007 1:24 AM | Balaji
Gravatar

# re: Abstract Class vs Interface

How about coupling ... using an interface you are de-coupled from implementation
6/18/2007 9:21 AM | sim
Gravatar

# re: Abstract Class vs Interface

thanks.

but please make it little more elaborate.
6/26/2007 12:28 AM | marudham
Gravatar

# re: Abstract Class vs Interface

This is the best example in the world.Keep posting good articles like this

Good luck
Sai
USA

7/31/2007 8:47 AM | sai
Gravatar

# re: Abstract Class vs Interface

great article. my vision is put on blog
8/7/2007 9:11 PM | iatanasov
Gravatar

# re: Abstract Class vs Interface

Thanks. You have cleared my all time doubts on interface n abstract.
8/16/2007 7:20 PM | Divya
Gravatar

# re: Abstract Class vs Interface

Get me a job
9/12/2007 9:16 PM | problema
Gravatar

# re: Abstract Class vs Interface

this is goood
9/17/2007 6:03 PM | shiv
Gravatar

# re: Abstract Class vs Interface

this is goood class
9/17/2007 6:05 PM | shivam
Gravatar

# re: Abstract Class vs Interface

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).
9/26/2007 9:59 AM | damaa80_hezen
Gravatar

# re: Abstract Class vs Interface

nobody can help
9/27/2007 8:18 AM | damaa80_hezen
Gravatar

# re: Abstract Class vs Interface

<div style="background-color:Yellow; text-align:center; width:100%">
<br />

Interview Questions
|
ASP.NET Interview Questions
|
.NET Interview Questions

<br />

ASP.NET 2.0 Interview Questions
|
.NET 2.0 Interview Questions
|
SQL Server 2005 Interview Questions

<br />
</div>
12/26/2007 6:41 PM | Mahesh
Gravatar

# re: Abstract Class vs Interface

<div style="background-color:Yellow; text-align:center; width:100%">
<br />

Interview Questions
|
ASP.NET Interview Questions
|
.NET Interview Questions

<br />

ASP.NET 2.0 Interview Questions
|
.NET 2.0 Interview Questions
|
SQL Server 2005 Interview Questions

<br />
</div>
12/26/2007 6:41 PM | Mahesh
Gravatar

# re: Abstract Class vs Interface

Nice explanation

http://www.codepal.co.in
12/27/2007 4:36 AM | Bino
Gravatar

# re: Abstract Class vs Interface

this infor mation solve my prblem very efficently.thenks for thet
1/16/2008 2:53 AM | shrikant garg
Gravatar

# re: Abstract Class vs Interface

nice explanation . and i got the difference between them ,thz
1/27/2008 6:58 AM | manish
Gravatar

# re: Abstract Class vs Interface

I had a gr8 understanding from this article, thank you keep it up.
1/28/2008 4:09 AM | Ravi Shankar
Gravatar

# re: Abstract Class vs Interface

Hi good explanation
2/3/2008 5:53 PM | Ashok
Gravatar

# re: Abstract Class vs Interface

nice one now i know the difference
2/14/2008 4:13 PM | ganesh
Gravatar

# re: Abstract Class vs Interface

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.
2/22/2008 5:14 AM | lalit lakhchaura
Gravatar

# re: Abstract Class vs Interface

hello mahesh,
really thanx for simple clarification.i was not able to answer this question properly in interview but now i can. thnx
2/29/2008 2:13 AM | dipali
Gravatar

# re: Abstract Class vs Interface

That is what I was searching for...for last 1 months.
3/10/2008 12:59 AM | Parth Dave
Gravatar

# re: Abstract Class vs Interface

Great...!

You have put this in very simple words. you really deserve Thanks...
4/4/2008 3:17 AM | Narayan
Gravatar

# re: Abstract Class vs Interface

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
8/21/2008 5:47 PM | Kiran
Gravatar

# re: Abstract Class vs Interface

Nice Article...! Keep the good work going...!
8/29/2008 11:01 PM | Hemant
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: