harshal deshpande wrote:that is why i am posting here same question "why to use abstract methods??" and "what are their advantages".
Simply put: They allow you to separate design from implementation, or the "what" from the "how".
When you start creating more complex classes, you'll discover that the first thing you need to know is what it does. You DON'T need to know how it does it; and worrying about that prematurely can often get in the way of what you're trying to concentrate on: Design.
One of the very first books I read about OO programming had this quote on Page 1:
Lucius Cary, 2nd Viscount Falkland wrote:When it is not necessary to make a decision, it is necessary NOT to make a decision.
and abstract methods are one tool for doing this. They allow you to define the "what", and postpone decisions about "how" until you're ready to make them.
The above only scratches the surface; but hopefully it'll get you started.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
As you probably know, abstract methods are used in the context of abstract classes (any class with an abstract method is automatically abstract). Are you familiar with that concept?
These methods actually represent placeholders for concrete implementation of some functionality implemented in subclasses.
Suppose you want to implement a Java program that will work with geometry object (e.g. polygons). Now, once you start implementing Polygon class you'll try to encapsulate as much of the features that all polygons have in common, like list of points representing vertices, methods for calculating area or cicumference of it. On the other hand, you can't know in advance how many vertices a polygon will have, so you can't provide a useful implementation of these methods (well, you might, but let's say you can't for the purpose of example). That's when you handle the situation with abstract methods.
Now, you can define the class representing the simplest polygon - Triangle as:
Note that all the concrete implementations from Polygon class are inherited in subclasses.
As Winston said in it's reply and you can probably notice that, by using abstract methods you just say what functionality subclasses should provide, and that's it. It is for subclasses to implement that functionality, and inherit the default behavior you might put in your superclass (the abstract one). It is that common behavior that makes you use them instead of interfaces.
The quieter you are, the more you are able to hear.
To add to what Kemal illustrated:- What he has shown you is commonly called the template pattern. Template pattern is built using abstract classes. You might want to google template pattern if you want to find more examples.
Abstract classes are those classes which contain abstract, i.e. non-implemented methods. Abstract classes are mainly used in inheritance using which we can acheive type compatiblity and extensibility.
It can be better expressed as follows:
Suppose you have a student class which is abstract having a method public abstract void study(). Also, there is one CollegeStudent class and one SchoolStudent class, both of which extend the Student class. Now both the CollegeStudent and SchoolStudent both study but in a different manner. So they both override the method study from the Student class and write the implementation specific to them.
Hence, we can conclude that using abstraction we can acheive extensibility and type compatiblity, which are the main use of inheritance.