• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Var-args

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The above code prints "Base.print" while I was expecting the derived class' method to be called. why?

[ May 13, 2007: Message edited by: M Krishnan ]
[ May 14, 2007: Message edited by: M Krishnan ]
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

M Krishnan


you are overloading the printSrt() method.In the base that method has parameters ,which accepts 0 to n arguments.

And in the derived class,it is not accepting parameters.

If you override that method in the derived class you will get the result you want.

Thanks
Anil Kumar
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


And in the derived class,it is not accepting parameters.



Yes that's correct, since I am calling a method with no arguments I would expect the derived class' method to be called.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Krishnan:


The above code prints "Base.print" while I was expecting the derived class' method to be called. why?

[ May 13, 2007: Message edited by: M Krishnan ]

[ May 14, 2007: Message edited by: M Krishnan ]



The thing to notice here is that you are calling the method printStr using a Base1 reference.

That means that even though the object is of runtime type Derived1, you can only call methods on the Base1 reference that are visible to the Base1 class.

In this case that means that the only method you can call on the reference is the method printStr defined in Base1 and inherited by Derived1.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic