C# .NET - oops

Asked By madhu krishna on 09-Apr-11 01:44 AM
1) From normal user point of view, what’s the Difference between Abstract class and Interface class?
Reena Jain replied to madhu krishna on 09-Apr-11 01:51 AM
hi,

Abstract Class:

  • Abstract Class Can contain Abstract Methods and Non- Abstract Methods.
  • When a Non-Abstract Class is Inherited from an Abstract Class, the Non-Abstract Class should provide all the implementations for the inherited Abstract Method.
Interface:

  • Interface is nothing but Pure Abstract Class ie Interface can contain only the function declaration.
  • All the members of the interface are Public by Default and you cannot provide any access modifiers.
  • When a class is inherited from the interface, the inherited class should provide actual implementations for the inherited members.
Hope this will help you
Nikhil Mahajan replied to madhu krishna on 09-Apr-11 01:59 AM
hey madhu..
i know you there many definitions on net which which tell you difference between Abstract class and interface class...

but in user point of view.....in very simple language.. below is the example



Interface - Add behavior to a class, a contract to perform certain operations on a object

Abstract class - Define a base class for several types, like the stream class - I know I will read the data from a stream (which is an object, not a behavior of a certain object) but I don't realy care where the stream comes from.
Reena Jain replied to madhu krishna on 09-Apr-11 02:45 AM
hi,

here are some more good Differences between Abstract Class and Interface

  • A class may inherit only one abstract class, but may implement multiple number of Interfaces. Say a class named Car needs to inherit some basic features of a vehicle, it may inherit from an Aabstract class named Vehicle. A car may be of any kind, it may be a vintage car, a sedan, a coupe, or a racing car. For these kind of requirements, say a car needs to have only two seats (means it is a coupe), then the class Car needs to implement a member field from an interface, that we make, say ICoupe.
  • Members of an abstract class may have any access modifier, but members of an interface are public by default, and cant have any other access modifier.
  • Abstract class methods may OR may not have an implementation, while methods in an Interface only have a definition, no implementation.
Hope this will help you
dipa ahuja replied to madhu krishna on 09-Apr-11 03:15 AM
Hi.. In very simple language :

Abstract classes can have implementations for some of its members (Methods)

Interface can contain only method declaration , the class which implement interface while define the methods.

Difference:
In Abstract Class at least one method is undefined where as in interface all method is undefined

A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.

Similarity

Neither Abstract classes nor Interface can be instantiated.

Jitendra Faye replied to madhu krishna on 11-Apr-11 01:47 AM
abstact class is a abstract view of any realword entity and interface is more abstract one.

What is an Abstract Class?

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

What is an Interface?

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn�t support multiple inheritance, interfaces are used to implement multiple inheritance.

Fore more detail follow these links-

http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx
and
http://geekswithblogs.net/mahesh/archive/2006/07/05/84120.aspx

I hope this will help you.