Abstract Base classes vs Interfaces
-
Can anybody tell me the difference between Abstract Base classes vs Interfaces??
-
Can anybody tell me the difference between Abstract Base classes vs Interfaces??
If I remember correctly (I'm not sure anymore ;) ) an abstract class is a class that contain at least one pure virtual function (a pure virtual function is a function that MUST be redefined in every class that inherits from the base class) A pure virtual function looks like:
virtual void Test() = 0;
If you try to declare an instance of a class containing a pure virtual function, you will have an compile error saying that it cannot create an instance of the class due to a pure virtual function. It is used to 'forbid' an instanciation of the class (the class is just used as base class for another class and cannot be used 'alone'). Concerning the interface, also called pure abstract class, it's a class that contain only pure virtual functions and no member variables (for this last point, I think there are different opinions). So, the class itself is 'empty' because no functions has been defined. Hope this helps -
Can anybody tell me the difference between Abstract Base classes vs Interfaces??
-
Can anybody tell me the difference between Abstract Base classes vs Interfaces??
if you are talking in a pure standard C++ point of view, Cedric already gave you the right answer. now, if you heard with an interface, the keyword that we get as in java or in .NET languages, that's easy, such a thing don't exist. but the difference would be easy to do : when you inherit an abstract class, you get "as a gift" all the methods that are already implemented (and so, you don't have necessarily to write them if they do what you want). you have to be laziness when you implement interfaces because you have to provide the code for each function... to finish, abstract is not a C++ keayword, so sometimes, you could have difficulties to set your class abstract. that happened to me last month, and thanks to Mr Surhedayan, i now have it. the point is, when you have a base class that already provide some function bodies, that will not be overloaded in the children classes, you cannot set the function as pure virtual (with
=0
) ; and if you dont want the users to instanciate this class, the simple thing to do is to set the constructors protected... :D cheers,
TOXCCT >>> GEII power
[toxcct][VisualCalc] -
Can anybody tell me the difference between Abstract Base classes vs Interfaces??
Difference between Abstract classes and Interface Little background: A real world entity is represented in an OOP world as object. Objects are defined as classes any language. A class defines objects in two aspects Object's characteristics - Data Members Object's functionality - Member functions So, when the classes that are forced to provide implementations of the functions prototypes that are defined in either ‘interfaces’ or a ‘abstract class’ are not belonging to the same family (having some inherited parental characteristics from same parent directly or indirectly) but are rather related through some functionalities then 100% we will go with interface and if above said classes are belonging to same family then we should go with abstract class inheritance. Also in case of inheritance as everything is a specialization of base parent object so there is allowance of providing some default definitions of the functions that are needed to be implemented further by specialized classes. This is not any way allowed in interfaces because implementing classes are not belonging to same family and are not any way related by characteristics. An Example: We have four real world objects: • Airplane • Bird • Sparrow • Eagle We have one method that is a functionality of all above objects • Fly Now all objects share common functionality flying in air but differ in the mechanism they use to fly. Also by close look we see that in general there is some common characteristics (as well as some common functionalities) between Sparrow and Eagle that can be a part of common class Bird (that has to be abstract because there is no real thing like bird!). But any way Airplane can't be related to Bird or its specialized classes Sparrow and Eagle. So if want to expose fly functionality to outer world and want their implementations in Airplane and Sparrow (or any bird), so I will 100% go for Interface and Abstract classes will be a big NO! But if we exclude Airplane want that fly functionality is exposed to outer world and its implementing classes are Bird, Sparrow and Eagle then 'Abstract Classes' is the best of best choice. Happy programming! Please feel free in writing to me for further clarifications on other OOP concepts (im.himanshu2006@gmail.com) Himanshu :)