Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

abstract class vs interface

>>>It is far easier to evolve an abstract class than an interface.
I was reading above statements. I have not understood the statements, concept behind clearly.
Please advise. Any ideas, sugestions, sample code, links, resources highly appreciated. thanks in advance
SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Discard the last sentence from my previous post - it was something  which inadvertently got pasted with the
text I wanted to post
Avatar of OklahomaDave
OklahomaDave

This can be a tough issue to keep straight, but I'll see if I can help some.

First, at the most basic level, an "Interface" is a "contract" that defines how an object implementing itis accessed by the "outside world." An interface carries nothing more than method declarations. Period. The details of the methods are left to the designer of the class that implementis that interface - and an implementing class *must* provide implementations for every method defined in the interface (hence the contract metaphor). Now, an abstract base class is almost the same thing, except that it is, itself, a class, albeit a special variety.

An abstract base class can also define nothing more than method signatures, which at a certain level makes it operationally very similar to interfaces. However, an abstract base class allows for the notion of providing some implementation details that will be available to classes that derive from it. That's a marked departure from the interface concept.

More conceptually, an abstract base class is typically intended to serve as the root of what might be termed an inheritance tree of ever-evolving functionality; a first-tier class inherits from the base class and provides implementations for all the abstract methods; a second-tier inherits from the first tier, and so on. Interfaces, on the other hand, only describe the behavior of a single implementing class, although other classes may be derived from that implementing class and gain the benefit of that original interface contract.

An academic example of an Interface could be that of a Vehicle, sporting method signatures for a TurnOn method, a TurnOff method, an Accelerate method, a Stop method, and a Steer method. Any number of concrete classes could implement the Vehicle interface and provide implementations for all these methods, yet represent physically quite different types of implementations.

An example of an abstract base class could be an authentication class that provides a shell for a Username and Password method, but hard implementation for an Encryption method to secure the provided credentials. The author of the derived class can use the stock Encryption method, or override it with their own implementation.

Some languages use a combination of abstract base classes and interfaces to implement what's called "multiple inheritance." C-Sharp is an example of just such a language, in that it allows you to define a class that is inherited from exactly one class, but can implement multiple interfaces.

In a nutshell, Interfaces define contracts that are always implemented, whereas abstract base classes define contracts with some implementation, but are always subclassed or implemented in a derived class.

I hope that helps you in your studies!
-David


OklahomaDave is correct.  I would also add (or stress) that an abstract class cannot be instantiated.  You MUST derive a class from the abstract class (i.e. you must define a class that is not abstract and that inherits from the abstract class).  Assume A, B and C are abstract.  You can define a non-abstract class D that inherits from C and C can inherit from B and B can inherit from A.  D is the only class in this example that can be instantiated.  Of course, A, B, C and D's constructors are executed when D is instantiated.

The classic example is the Animal class.  There are no "Animals" in the world, but there are Cats and Dogs.  Animal is the abstract class.
Avatar of gudii9

ASKER

>>

 "You correctly point out that it's easier to evolve an abstract
class than an interface.  ... Where evolution is of paramount importance,
abstract classes may be preferable to interfaces.


can  we use annotations with interface to easily evolve where you can define additional annotation for new method, contract etc.

>>Some languages use a combination of abstract base classes and interfaces to implement what's called "multiple inheritance." C-Sharp is an example of just such a language, in that it allows you to define a class that is inherited from exactly one class, but can implement multiple interfaces.


we can extend one class and also implement 3,4 etc multiple interfaces in java also right like

class x extends y implements z,l,m,n {...





>>
Assume A, B and C are abstract.  You can define a non-abstract class D that inherits from C and C can inherit from B and B can inherit from A.  D is the only class in this example that can be instantiated.
>>
There are no "Animals" in the world, but there are Cats and Dogs.  Animal is the abstract class.
In real world how abstract class can extend other abstract class. Since Animal is imaginary how can we extend some other say Crazyanimal  abstract class extend Animal class forgetting about class C, D for now.



please advise
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial