Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
why we create abstract class ,abstractmethod variable etc basically why we create abstract class and etc.

and why we create interfaces and what is interface.
Posted

Lots of explanations here[^].
 
Share this answer
 
Please see this discussion: When we use abstract and when we use interface...?[^].

And also this: Difference between abstract class and interface if they have same no of methods and var[^].

Anyway, learn these concepts and best use practices properly. These are some of the most important aspects of OOP. Make it a point of your regular education, not relying just on particular questions and answers.

In particular, read starting from here:
http://msdn.microsoft.com/en-us/library/9cc659c4%28v=vs.71%29.aspx[^],
http://msdn.microsoft.com/en-us/library/scsyfw1d%28v=vs.71%29.aspx[^].

—SA
 
Share this answer
 
v3
Comments
fjdiewornncalwe 27-Sep-11 11:59am    
Just fixed a typo...
Sergey Alexandrovich Kryukov 27-Sep-11 12:03pm    
Thank you very much, Marcus.
--SA
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.


C#
            //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.
 
Share this answer
 
v2
Comments
luisnike19 27-Sep-11 15:24pm    
* code tag changed.
luisnike19 27-Sep-11 15:26pm    
Btw, devildx2050. You don't need to "copy" everything.
You found that information here: http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx
just provide the link if you think it's a good refence.
Philippe Mori 27-Sep-11 22:02pm    
Don't copy existing site. Give a link. Image in original article are lost and it is a shame to try to get credibility by sopying someone article like that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900