• Post Reply 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Advantages of overloading abstract methods

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interface t{ void good();}
interface Interk extends t{
void good(int k);
}
interface Interf extends Interk{
void good(int k, int j);
}
public abstract class TestInterface implements Interf{

public void good(){

}
}
What are the advantages of overloading abstract
methods ?
 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess the advantages are same as those of overloading non abstract methods. You can provide multiple method signatures and use the msot appropriate method .
Look at the methods of the Graphics class for a real life example of overloading abstract methods.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Murgan Sub:
What are the advantages of overloading abstract
methods ?


What are the advantages of overloading non-abstract methods?
When you overload non-abstract methods, you're allowing a method to be called with multiple signatures. For example, let's say you have a similar operation that can be done with a number of different parameters:

In this example, you can tell an Animal to eat, but you can do it two different ways, either by providing it with food or not. The results might be quite different. This flexibility is what is gained by overloading non-abstract methods.
Now, let's address your question: overloading abstract methods. It really does almost the same thing. The only difference is that, rather than supplying an implementation, it just requires subclasses to supply that implementation. Let's make an abstract class that Animal can inherit from:

By doing this, you've made it clear that any class that extends CanEat will be able to eat in a number of ways. Of course, if you're only going to inherit abstract methods from a class, it's no different than implementing an interface. Can you see what's different from implementing an interface in the example I showed?
If you managed to spot the instance variable hunger that is protected in CanEat, you can see the difference. Using an abstract class, you can still inheirit instance variables along with any abstract methods (or non-abstract methods, for that matter). If you were to inherit from an interface, interfaces only have implicitly final static variables.
I hope this (in some round-about fashion) answers your question.
Corey
reply
    Bookmark Topic Watch Topic
  • New Topic