Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

URGENT - Empty method and abstract method.

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What is the difference between abstract methos and a method without any body i.e. only empty braces are given for methos but no body is there.
Why cant we use empty method in the place of abstract method ?
Pls explain the usage/advantages/disadvantages of both....Its urgent !!!
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
anukampa,

What is the difference between abstract methos and a method without any body i.e. only empty braces are given for methos but no body is there.


One important difference is that an abstract method may only appear in an abstract class. You are right to notice a similarity between an abstract method and a regular method that has no body. They are similar in so far as neither seems to really "do anything," but each serves a distinct purpose.
An abstract method doesn't have a body because implementation of the method is defered to a subclass of the abstract class that contains the method. Declaring an abstract method foo() is like making a promise that some future sublcass will know how to do something that can be invoked by the name foo(). However, since the implementation of the method is defered, you cannot call the original, abstract method.
As to the other option you mention, non-abstract methods with no bodies, I would just point out that, even if a method has no body, it is still perfectly legal to call that method. As, to why you might want to do this, maybe somebody else can fill that in.

Why cant we use empty method in the place of abstract method ?


I'm not sure whatyou mean by this? You can override an abstract method with a non-abstract method that has no body. have a look at the following code:
abstract class Super {
public abstract void foo(); // abstract method
} // class Super
class Sub extends Super {
public void foo() {} // non-abstract, no body code
} // class Sub
Is this what you were asking?

Or are you asking why we would make the method abstract in the first place?
[ April 26, 2002: Message edited by: Dave Winn ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Dave Winn:
As, to why you might want to do this, maybe somebody else can fill that in.


Think about the default constructor. Any time, you create an object, a constructor is called. If you haven't provided a constructor, the compiler provides you with an empty constructor. This method is then called whenever you create that object by passing 0 arguments.
Empty methods are seldom used, but there are rare instances where you'll want to call a method that "does nothing."
Perhaps, you've got something like this:

As you can see, the Fish class is required to have a makeNoise method (because it extends the abstract class Animal, which has that method declared as abstract), but a Fish doesn't really make a sound. Therefore, this might be a case where you'd want to use an empty method.
Of course, if your subclasses are forced to use empty methods because they can't do the same things all the other subclasses can, perhaps your inheritance hierarchy isn't so great.
I hope that helps,
Corey
 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for the great example corey!
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
anukampa
Please do not cross post your questions to multiple forums. Most of the visitors here read in many of the forums and it is frustrating to see the same question repeated over and over. You have a better chance of getting a correct and timely answer if you post it to the most appropriate forum instead of using the shotgun approach and hoping you'll hit something.
This thread will be locked as the post in Java in general - advanced is more appropriate.
this thread here
 
You may have just won ten million dollars! Or, maybe a tiny ad.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
    Bookmark Topic Watch Topic
  • New Topic