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 ?
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.
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.
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.
Links at Stackoverflow
Others
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With