Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract classes and interfaces in C# [duplicate]

Tags:

c#

Possible Duplicates:
Interface vs Base class
Interface or abstract class?

Hi All,

I am just thinking about the abstract classes and interfaces, I know how they work technically but i don't understand the real use of abstract classes and interfaces. I mean why should we use abstract classes when we know that we can't create it's object, we have to anyway extend this class to use it so why don't we put everything in the derived class.

Same is with interfaces, we have to implement the interface so why don't we put all the methods which are defined in the interface in the class in which we implement the interface.

Can someone please clear my doubts with some examples ?

like image 326
Embedd_0913 Avatar asked Feb 22 '10 04:02

Embedd_0913


People also ask

What is difference between interface and abstract class?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.

What is the difference between an interface and an abstract class C#?

In C#, an Interface provides only those public services declared in the interface, whereas an abstract class provides the public services defined in an abstract class and those members that are inherited from the abstract class's base class.

What is abstract class in C?

An abstract class is a class that is designed to be specifically used as a base class. An abstract class contains at least one pure virtual function. You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.


2 Answers

Links at Stackoverflow

Others

like image 124
Asad Avatar answered Sep 26 '22 17:09

Asad


Also of note, in C# you can only inherit one class but multiple interfaces. An abstract class is a partial implementation of a class with reusable code for multiple subclasses. An interface is just that, an interface to the object for a specific purpose.

A good example would be objects in a game. If the object is Drawable and Collideable you may have common routines to handle that but no common base class, so you have the class implement IDrawable and ICollidable.

like image 30
smelch Avatar answered Sep 25 '22 17:09

smelch