• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question from Javacertificate.com

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider the code fragment below, assume classes are in different source files, what will be the output if you try to run the code?
1. public class Base {
2. protected int size = 100;
3. public int getSize() {
4. return size;
5. }
6. }
1. public class Runtime extends Base {
2. protected int size = 10;
3. public int getSize(){
4. return size;
5. }
6. public static void main(String[] args) {
7. Base b = new Runtime();
8. System.out.println (b.size + "," + b.getSize());
9. }
10. }

The answer is 100,10
My thoughts are the answer is 10,10
Why: Because the size field is 'replaced' by the subclass - I serously need to understand what is going on here ?
Mondli M
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mondli Mabaso:
Why: Because the size field is 'replaced' by the subclass - I serously need to understand what is going on here ?
Mondli M


Note: Dynamic Method Dispatch (Polymorphism) works only in case of instance methods.
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Variables are class bound which happens early at compile-time, whereas overridden instance methods are dynamically bound at run-time (but polymorphism doesn't apply to overloaded methods). So, the key line here is
A reference of a superclass pointing to a subclass object is created. Since it's a superclass type, the superclass variables will be used if accessed directly from the instance. getSize() is overridden and therefore is invoked polymorphically using b reference (it points to a subclass object). Now, getSize() accesses the size instance variable, which is redeclared in Runtime (we can also say that int size variable of Base is hidden by int size of Runtime). Had it not been redeclared in the subclass, it would've been inherited.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am confused here. I always believed that a subclass cannot access the protected variable of its superclass. Can anyone please explain how this works here.
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe you mean the situation where Subclass and Superclass are declared in different packages. Even then, the Subclass inherits the protected members of its Superclass and can freely use 'em as if they are declared in the Subclass. The only limitation is that Superclass protected members aren't accessible from the Subclass using a reference to an instance of Superclass. Yet, you can access 'em using super.myVariable call. Inherited protected members become private to the Subclass and are not accessible to other classes even in the same package. Subclasses of that Subclass down the inheritance tree can still inherit protected members all way up the hierarchy.
 
stable boy
Posts: 425
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the Java language the behaviour of 'overriding' methods and fields are different. At line 8 of the code example, the compiler associates the getSize method with the class of b. That is, the getSize method of Runtime will be invoked. This is called dynamic binding and is how Java implements polymorphism.
However, for field declarations dynamic binding is not applied. For the size field the compiler will associate the field with the class type of b (Base). Therefore when the program is run the code at line 8 displays
10,100.
In fact, because of the difference in behaviour it is not correct to use the term 'overriding' for fields. Instead if a class declares a field, then the declaration of that field is said to hide any and all fields with the same name in the superclasses of the class.
Hope this explanation clarifies the example code.
Thomas De Vos
[ October 05, 2003: Message edited by: Thomas De Vos ]
[ October 05, 2003: Message edited by: Thomas De Vos ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic