my dog learned polymorphism*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overloaded methods in derived class" Watch "overloaded methods in derived class" New topic
Author

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();
bc.methodA("hello");
dc.methodA("hello");
}
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?
thanks
william


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

Joined: Oct 16, 2001
Posts: 35
Hello,
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
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Hello,
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
Thansk
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
william
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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"?


JavaBeginnersFaq
"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
 
 
subject: overloaded methods in derived class
 
Similar Threads
inheritance thru upcasted reference
inheritance thru upcasted reference
convert base class to derived class
Compiler inserted call to super(), inserted where?
Inherited methods works on hidden instance variable while ....