File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How do you prevent overloading? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do you prevent overloading?" Watch "How do you prevent overloading?" New topic
Author

How do you prevent overloading?

Dave Tong
Greenhorn

Joined: Jul 06, 2005
Posts: 10
Suppose I have the following:

class A {
int foo() {
return foo(true);
}
int foo(boolean flag) {
return 1;
}
}
class B extends A {
int foo() {
return foo(true);
}
int foo(boolean flag) {
return super.foo();
}
}

How do I ensure that A.foo() always calls A.foo(boolean) not B.foo(boolean)?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Dave Tong:
...How do I ensure that A.foo() always calls A.foo(boolean) not B.foo(boolean)?

I think you're really asking, "How do I avoid polymorphism?" If you don't want this behavior, then you probably shouldn't have overridden methods.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Make A.foo(boolean) private or final.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Its a very interesting and useful feature of java.In case you do not want then
follow Ilja's suggestion.


Rahul Bhattacharjee
LinkedIn - Blog
Dave Tong
Greenhorn

Joined: Jul 06, 2005
Posts: 10
The simplest solution was to modify the code so that B.foo(boolean) could call super.foo(boolean) instead of super.foo()

Making A.foo(boolean) final would have broken the code
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Dave Tong:
The simplest solution was to modify the code so that B.foo(boolean) could call super.foo(boolean)...

If that's the behavior you want, then why override foo(boolean) at all?
Dave Tong
Greenhorn

Joined: Jul 06, 2005
Posts: 10
Originally posted by marc weber:

If that's the behavior you want, then why override foo(boolean) at all?


Because it does more than just that. I simplified for the sake of clarity.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you prevent overloading?
 
Similar Threads
For loop running on Booleans
infinite loop problem
PGJC Exam - clarification?