Click here to Skip to main content
15,881,831 members
Please Sign up or sign in to vote.
4.60/5 (3 votes)
See more:

According to my knowledge following are the determination rules

Interface: It’s a contract between interface and implemented class. Interface defines the protocol which emphasis how implemented classes will behave and communicate. It simplifies design complexity and brings more consistency in code writing.

Abstract Class: It helps to consolidate common functionality of all extended class.

 Can anybody share their idea too.

Posted
Updated 25-Nov-09 16:43pm
v2

These are what I consider to be the main points:

If you use an interface, all objects that implement it will have to provide their own implementations of your interface specification.

If you use an abstract class, then you can provide a base implementation so the derived classes do not need to have their own implementation. Alternatively you can mark properties/methods as virtual so that deriving classes can optionally provide their own implentation or mark them as abstract so they must provide their own implementation. An abstract class with only abstract methods and properties behaves much the same as an interface.

An object can implement many interfaces directly but only one abstract class (no multiple inheritance in C#).

Structs can only implement interfaces and not abstract classes.

 
Share this answer
 

Well, I'm willing to have a try.

An interface is a complete definition but without the code. When you implement the interface you must provide the implementation of the relevant code.

An abstract class is a base class that can be built upon to create related classes. Think animal as an abstract base, with cat, dog, monkey etc. as related classes.

 
Share this answer
 

This isn't a universal answer but two "mechanical" ways to decide:

If the class has any data, it must be an Abstract class...Interfaces may not include data.

An Interface can be added to a class that has already been derived from another class.  It is a form of multiple inheritance.  If you need to add behavior to existing classes, you'll need an Interface.

 
Share this answer
 

See below for an answer from another thread.

An abstract class is a class that you cannot create an instance of. It can provide basic functionality, but in order for that functionality to be used, one or more other classes must derive from the abstract class. One of the major benefits of abstract classes is that you can reuse code without having to retype it. That has a plethora of benefits, such as reducing bugs and making coding faster. A concrete example of an abstract class would be a class called Animal. You see many animals in real life, but there are only kinds of animals. That is, you never look at something purple and furry and say "that is an animal and there is no more specific way of defining it". Instead, you see a dog or a cat or a pig... all animals. The point is, that you can never see an animal walking around that isn't more specifically something else (duck, pig, etc.). The Animal is the abstract class and Duck/Pig/Cat are all classes that derive from that base class. Animals might provide a function called "Age" that adds 1 year of life to the animals. It might also provide an abstract method called "IsDead" that, when called, will tell you if the animal has died. Since IsDead is abstract, each animal must implement it. So, a Cat might decide it is dead after it reaches 14 years of age, but a Duck might decide it dies after 5 years of age. The abstract class Animal provides the Age function to all classes that derive from it, but each of those classes has to implement IsDead on their own.

Now, an interface is like an abstract class, except it does not contain any logic. Rather, it specifies an interface. So, there might be an interface called IFly. This might have the methods GoForward and GoDown. Those methods would not actually contain any logic... each class that implements interface IFly would have to implement those GoForward and GoDown methods. You could have classes Duck and Finch implement interface IFly. Then, if you want to keep a list of instances that can fly, you just create a list that contains items of type IFly. That way, you can add Ducks and Finches and any other instance of a class the implements IFly to the list.

 So, abstract classes can be used to consolidate and share functionality, while interfaces can be used to specify what the common functionality that will be shared between different instances will be, without actually building that functionality for them. Both can help you make your code smaller, just in different ways. There are other differences between interfaces and abstract classes, but those depend on the programming language, so I won't go into those other differences here.

 
Share this answer
 
v6
I Just find a suitable reference... Pls refer to this...
 
Share this answer
 

According to me,

        Interfaces can be used to declare the methods and initializing the variables that to be declared.

        In Abstract class, we can not create instance of that class. We can access the methods and functionalities of this class by using the objects of its derived class.

        An abstract class should have methods and functionalities. But an interface should not have method definitions within it.

         If you have a situation to keep a class safely, u can use abstract class. And if you have to define many more methods, we can use interfaces to declare them. 

 

 
Share this answer
 
 
Share this answer
 

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