• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super and subclass

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Derived has no method called show.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fran Kindred
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Francis, you exactly transalated my doubt in words. Thank you !
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mike and Barry, helped me a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic