This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Abstract methods-why to use them? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract methods-why to use them?" Watch "Abstract methods-why to use them?" New topic
Author

Abstract methods-why to use them?

harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
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

Joined: Mar 17, 2011
Posts: 7718
    
  20

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
nir sharma
Ranch Hand

Joined: Sep 11, 2012
Posts: 72

try these.

http://www.javatutorialhub.com/java-abstract-class-method.html

http://www.javabeginner.com/learn-java/java-abstract-class-and-interface
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
thanks winston and nir.

definitely it got me started.








Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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.
harshal deshpande
Ranch Hand

Joined: Sep 06, 2012
Posts: 34
Thanks kemal
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2347
    
  28

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

Joined: Sep 14, 2012
Posts: 5
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

Joined: Oct 13, 2005
Posts: 38519
    
  23
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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).
  •  
    Consider Paul's rocket mass heater.
     
    subject: Abstract methods-why to use them?