wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What is going on here Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "What is going on here" Watch "What is going on here" New topic
Author

What is going on here

Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

public class Derived extends Base
{
public static void main(String[] args)
{
Base b = new Derived ();
b.someMethod();
}
void someMethod()
{
System.out.println("one");
}
}

class Base
{
}

Why cant the compiler find the 'b.someMethod()' method?
[ December 09, 2007: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extermely hard to read and most people will just go elsewhere. Please read this for more information.

You can go back and change your post to add code tags by clicking the .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by John Reilly:
...Why cant the compiler find the 'b.someMethod()' method?

Because 'b' is a variable of type Base, and Base has no such method. To use someMethod, you would need to either downcast the reference back to type Derived, or declare the method in Base.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
nico dotti
Ranch Hand

Joined: Oct 09, 2007
Posts: 124
So when you override a method using the polymorphism of, per your example:
Base b = new Derived();
b.foo();// foo has to be defined in Base!

Here's the confusion (conquer this and you'll have it!):
The reference variable (in this case the Base or Super) provides for what method CAN be called. So you have to have it defined or implemented in the Base or Interface. However, IF IT IS defined in the base/super class, it's the object instance that's being pointed to (in your case the subclass) that will be the method ran. So it's a bit confusing because the reference decides if the method is allowed while the object BEING referenced determines which code will run (after it passes the first condition of being available). And of course this is easier if I try to code it for you:


I did the code off the top of my head so there my be a mistake but the fact is that you have to define a method in the Super/Base/Parent for you to actually be overriding anything in the Sub/Derived/Child. In your example, you actually have a method that is unique to the child class so no overriding is even taking place. Ok, I've described this about as many ways as I can think...let me know if it's making sense now
Timmy Ryan
Ranch Hand

Joined: Dec 09, 2007
Posts: 63

Thanks Nico,

It makes sense now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is going on here