File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Super and subclass 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 "Super and subclass" Watch "Super and subclass" New topic
Author

Super and subclass

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
In the following code:
1. b contains object of Derived.
2. Print method is called by object reference of superclass 'Base'(b), having object of subclass 'Derived'.
3. It is calling method print(Base b). Here b = object of Derived.
4. So, i was expecting to call of show method in Derived which will print "Derived".
5. It is printing "Base"

Thank you in advance

(Please format your code)
[ December 03, 2004: Message edited by: Barry Gaunt ]

"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Derived has no method called show.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Thank you Barry,

Right, so it will execute superclass method (show()) and then i was expecting s in Base is hidden by s in Derived. So, it should consider s in Derived (like overriding method). Looks, like return statement in show works like - return this.s. Is it correct?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
So, it should consider s in Derived (like overriding method)


No, that's not so. Member variables are not overidden like methods, The base class' variable will be used by the base class's method.
Francis Palattao
Ranch Hand

Joined: Sep 22, 2004
Posts: 91
Is it correct to see this as you inherited the show method then when it runs the show method the value s should return the derived object s instead of the base. Because Base S is not visible but Derived S is visible.


SCJP 1.4 <br />SCWCD 1.4 (preparing)<br />SCBCD 1.3 (coming soon)
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Francis, you exactly transalated my doubt in words. Thank you !
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The variable s in Base.show() is decided and fixed at compile time, it is the member s in Base. If the subclass Derived hides s and calls Base's show() method then show() still uses Base's variable s. The decision as to what variables to use is made at compile-time.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Thank you Barry!

When I use show method in Derived (override show in Base), class it prints �Derived�. So, looks like, methods gives priority to the variables in the class from which that method it is called.

1. If show is called from Base, it is giving priority to Base variable.

2. If show is called from Derived, it is giving priority to Derived variable. (Variable in Base is hidden by variable of Derived)

3. If no variable is declared in Derived, then it considers variable in Base (Inherited variable)

Please correct if I am wrong.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Please consider what it means for a reference to be evaluated at compile time. The compiler processing the base class method will create bytecode accessing the base class's local variables, member variables, and inherited member variables. Classes don't inherit from their subclasses. At execution time, the method will only use the variables it was told to use.

Invocations of instance methods, not variables, are evaluated at execution time. The compiler creates explicit bytecode to examine the actual type of the object on which the method was invoked and to compute the proper overridden method to invoke. Again, this special processing, called "late binding", is not done for variables, only methods.


Mike Gershman
SCJP 1.4, SCWCD in process
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
2. If show is called from Derived, it is giving priority to Derived variable. (Variable in Base is hidden by variable of Derived)


This is wrong, it still uses the Base class' s. Change your code so it uses a Derived object reference instead of a Base object reference and you will see it still prints "Base".



There, I changed it for you. Output:


Mike has explained it in a more formal detailed way, it's very important to understand what's happening. So play with your code, use a debugger, or whatever to convince yourself of what is going on.
[ December 04, 2004: Message edited by: Barry Gaunt ]
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Thank you Mike and Barry, helped me a lot.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Super and subclass