Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Pseudo Virtual Overloading?

807603Oct 11 2007 — edited Oct 20 2007
I tired to find a similar post about this but was unable to. I discovered this design issue when working with C#. C# didn't handle like I thought it should, (at least it didn't handle as I was used to Java handling). But the more I thought about it the more I was confused about why Java worked this way. consider the following c style Pseudo code:

class List {
public add() {...}
}

class AList : List {
public add() { ... }
}

function foo(List list) { ... }
function foo(AList list) { ... }

List list = new AList();
list.add();
foo(list);

Now the surprise here is that AList::add() is called while foo(List) is called in the next line. Not very consistent in my view.
Please note that List::add is NOT abstract.

I ran a test in both C# and C++ and they both call List::add() and foo(List) unless List::add() is declared as virtual. This could be just a convenience thing. Having to make List::add abstract would of course force List to be abstract, and therefore you could not have a concrete instance of the parent class (in this case List) which could maybe get a little tedious. I guess we could debate how a solid OOP should handle a case like this , but I believe constancy should almost always beat convenience .. which is kinda why I am a little surprised that MS's C# worked the way it did :)

Also note that I have only tested this in 1.5

thanks for your comments
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2007
Added on Oct 11 2007
17 comments
262 views