File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes overloaded methods in derived class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overloaded methods in derived class" Watch "overloaded methods in derived class" New topic

overloaded methods in derived class

william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
class BaseClass{
public methodA(Object a){
System.out.println("base class called");
class DerivedClass extends BaseClass{
public methodA(String a){
System.out.prinltn("derived class called");
public static void main(String args[]){
BaseClass bc=new DerivedClass();
DerivedClass dc=new DerivedClass();
In the code shown above the derived class implenetation is never called either using a base calss refernce of derived class reference.
Should not no compiler object to such a code???
Or is there a way of invoking the derived classes implementation?

Help me!Help you!!!
Argm Mastoi
Ranch Hand

Joined: Oct 16, 2001
Posts: 35
I think that when u overload a method in a derived class. and call the method through the object of derived class from outside, the method in base class is first called. so if the parameter suits then method of base calss executes. so try changing the type of parameter :roll: its only my opinion, i might be wrong.
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
A couple of notes...
1. Your code won't compile. This will:

The output is not what you said it would be.
2. I recommend reading How My Dog Learned Polymorphism.
Good Luck

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
but thats the whole point, i donot want to change the parameter.All that i want to know is why is the compiler allowing code that can never be accessed to get compiled.Again is there a way to access it?
It can object to it as in the case of expections where ,when u try to catch a derived class of exception after catching the base class the compiler objects
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
I guess I don't understand the question. When I compile your code (after adding "void" as return type to the methods):

I get the output:

base class called
derived class called

What do you mean by, "In the code shown above the derived class implenetation is never called"?

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
william kane
Ranch Hand

Joined: Nov 21, 2000
Posts: 260
I profusely apologise for the time and effort wasted due to error on my side.
Will try not repeat in future
sorry once again
I agree. Here's the link:
subject: overloaded methods in derived class
It's not a secret anymore!