• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract methods-why to use them?

 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello everyone

i did start to study on "abstract" keyword in java.

i came till abstract methods:"defined but not implemented"

i searched on the Google for one question i.e:why to use abstract methods?? or what are their advantages.

i could not get answers.may be i am asking wrong question....

that is why i am posting here same question "why to use abstract methods??" and "what are their advantages".

one request i cannot understand the this so if you want to refer some site please refer another site.

thanks in advance.
 
Winston Gutkowski
Bartender
Pie
Posts: 10273
60
Eclipse IDE Hibernate Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks winston and nir.

definitely it got me started.








 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
harshal deshpande
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks kemal
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richa Sinha
Greenhorn
Posts: 5
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richa Sinha wrote:Abstract classes are those classes which contain abstract, i.e. non-implemented methods. . . .
Not quite. Abstract classes are those are marked abstract. You can have abstract classes without abstract methods.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually the other way around:
  • If a class contains abstract method(s), then it is abstract.
  • If the class is abstract it doesn't necessarily contain abstract method(s).
  •  
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic