abstract and interface class
i am confuse with abstract and interface object creation.can u give any clarification?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Praveen NPosted Feb 9, 2014, 11:00 PM
eatures Interfaces abstract class
Multiple inheritance
A class may inherit several interfaces.
A class may inherit only one abstract class.
Default implementation
An interface cannot provide any code, just the signature.
An abstract class can provide complete, default code and/or just the details that have to be overridden.
Core VS Peripheral
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity
If various implementations only share method signatures then it is better to use Interfaces.
If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed
Requires more time to find the actual method in the corresponding classes.
Fast
Regards,
Praveen Nelge
VulpesPosted Feb 9, 2014, 10:08 AM
What you can do is to:
1. Declare a variable or parameter of the abstract class's type. This variable can then contain a reference to any object which inherits from the abstract class.
2. Declare a variable or parameter of the interface's type. This variable can then contain a reference to any object which implements the interface.
This allows you to write generic code which applies to any class derived from the abstract class or any class which implements the interface.
Normally, there is no relation between classes which implement an interface except for the common functionality which it represents. However, there usually is a relationship between an abstract base class and its derivatives.
A class can implement any number of interfaces but can only inherit from a single class (abstract or not).
For more on the differences between the two, check the links below your post.