• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 531
  • Last Modified:

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
0
gudii9
Asked:
gudii9
2 Solutions
 
for_yanCommented:


   This statement shows the perspective of a person/programmer who is developing interfaces
for use by general (wide) public:

        "If you write a java interface and others use it, you cannot add code to it. This may cause others code compile error."

  So you may not develop (evolve)  interfaces further, becuase if you add method to once established
and alreday used by someone interface, you'll break that someone's code.

In this sense abstract classes are more convenient for those
who are providing them for other folks to use.

Howvere , this is the response to that argument (basically interfaces
have much greater flxibility and are very useful):

 "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.  Generally speaking,
however, the increased flexibility of implementation afforded by
interfaces make them preferable. ...Interfaces are the heart and soul of the Java programming
language. "

You can read this whole discussion here:

http://www.codeproject.com/KB/java/EmailWithJoshuaBloch.aspx


But this is of course philosophical level far beyond anything, which would be necessary to a person
more or less beginning to study java








    I have check there are so many java awt/swing event use java interface for event listener and abstract class for event  interface adapter,
0
 
for_yanCommented:
Discard the last sentence from my previous post - it was something  which inadvertently got pasted with the
text I wanted to post
0
 
OklahomaDaveCommented:
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


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
mbmastCommented:
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.
0
 
gudii9Author 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
0
 
OklahomaDaveCommented:
An abstract class may provide at least a partial implementation of some of its methods, what might be termed a "base" implementation that would be seen as a "lowest common denominator" of behavior that would be common to all concrete implementations of that abstract class. The class, however, is abstract in that there are other methods that are defined by signature alone, and thus must be implemented by other class or classes extending it .

Now, in the scenario you're describing, there's little point in creating a subclass that extends an abstract base class that, itself, is abstract but doesn't provide any implementation. Presumably, the point of creating the subclass is to define at least some measure of a real world problem model.

For the sake of argument, you could extend the animal and cat metaphor even further; just as a Cat is an Animal, but no one Cat is "just" a Cat, you could have concrete implementations for SiameseCat, Tiger, and Lion that extend the abstract Cat class which, itself, is an extension of the abstract Animal class. You could extend Animal into CrazyAnimal just as you would any other class/subclass relationship.

Keep in mind, too, that extending classes isn't limited to abstract classes; the whole paradigm of object orientation is to facilitate reuse by extending functionality of existing base classes. Defining, then extending an abstract framework merely allows for the foundation of a structure within a broader problem domain.

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now