Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#

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 23-Nov-09 5:42am
Edited 25-Nov-09 16:43pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.

  Permalink  
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

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. 

 

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I Just find a suitable reference... Pls refer to this...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

  Permalink  

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

  Print Answers RSS
0 OriginalGriff 330
1 Richard Deeming 240
2 Nirav Prabtani 212
3 CHill60 170
4 _Amy 165
0 OriginalGriff 8,104
1 Sergey Alexandrovich Kryukov 7,045
2 Maciej Los 4,039
3 Peter Leow 3,738
4 CHill60 2,912


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 25 Oct 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100