We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

abstract class vs interface

gudii9
gudii9 asked
on
Medium Priority
547 Views
Last Modified: 2012-05-11
>>>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
Comment
Watch Question

Awarded 2011
Awarded 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Awarded 2011
Awarded 2011

Commented:
Discard the last sentence from my previous post - it was something  which inadvertently got pasted with the
text I wanted to post
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


Commented:
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.

Author

Commented:
>>

 "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
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.