I have studies that, if the class is so general that it should be specialized then declare the class as abstract.
But I dont understand why to declare it as abstract.What is the use. Instead of keeping it as abstract keep it as it is.
I know it will add to the type but still whats the use?
Further now if there is an abstract class concept then why do i need an interface which is by default abstract.
Well i know there must be some reason why these features are given.
But I am not able to figure out.
Please help me with the correct description of these or by using an example.
Please refer my comments with the code.
As you can see we have an interface to define the contract.
We have an abstract class for the common attributes.
We have the concrete class to provide the actual implementation of speak();
Now consider the following method
As you can see, the method does not do any processing or typecasting at all. It just invokes the speak();
Abstract classes are classes that cannot logically create instances. Things that does not make sense if they have instances, for example a Shape. Ask your self what this means "new Shape()"? How can you create a shape without knowing what kind of shape it is. But you can surely do this "new Circle()" because circles are concrete enough.
Most of the time (if not all), abstracting a class is done to allow the sharing of invocational interface (those fields/methods). For example, Shape can draw so all shapes must have "void draw(Canvas C)" method. This way, all of the more-concrete classes that can draw may "inherit" or "extend" the class Shape so that we can ask it to draw without knowing if it is a circle or square (as soon as we know it is a Shape).
Interfaces are a special kind of abstract classes. There are two aspects of Interface that we should consider.
First an Interface is an abstract class WITHOUT any implementation. This means that there is no functionality given when a class "realise" or "implement" an interface.
To understand this, we should see why a class (not interface) may be abstract in the first place. For example, Shape is a class that have an abstract method "draw(...)". This method is abstract because THERE IS NO WAY TO KNOW HOW TO DRAW WITHOUT KNOWING WHAT SHAPE THIS IS. BUT, Shape (as an abstract class) CAN have other methods that is NOT abstract for example "void drawLine(...)". This method can be non-abstract because, we an create a LOGICAL IMPLEMENTATION of this method.
By making Shape an ABSTRACT CLASS allows us to share an invocation interface (method "draw()") as well as the implementation (method "drawLine(...)"). This implies that, draw() method of each shape will always differ depending on what type of shape this is while drawLine() is likely to be the same as it derived from Shape (share functionality).
In case, of an Interface, since there is no functionality, the implementation of an interface only inherit the invocational interface of it. For example, an Interface "Drawable" have a method "draw(...)".
From here, you may ask "So an interface is an all-abstract-method class? why bother have another name?". This is how the second aspect come in so keep going ...
The second aspect of an Interface (the most important of all) is that "Java does not allows multiple inheritance". This means that you cannot create a class which inherits "invocation interface" and functionality from more than one class. This because doing so can create confusion (Google "Diamond inheritance") as the shared functionalities from those inherited classes may not get along. But multiple inherit of "invocation interface" is very useful or even required in some cases so Interface is created as a way to allows "multiple inheritance of invocation interface" without "multiple inheritance of functionality".
So, a class can inherit from only one class (inherit the invocation interface as well as some functionality) and it can implements multiple Interfaces (inherit all of those Interfaces' "invocation interface"). An example is Circle inherit Shape and implements Comparable.
I am not a native tougne so please forgive me for my inconcise explanation.
Hope this help though.
Joined: Jul 24, 2008
Thank you very much the description is really very nice and helpful