This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have David & Dawn Griffiths on-line!
See this thread for details.
Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

overloaded methods in derived class

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"?
 
william kane
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I profusely apologise for the time and effort wasted due to error on my side.
Will try not repeat in future
sorry once again
 
reply
    Bookmark Topic Watch Topic
  • New Topic