• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(Oracle) Java SE 7 Programmer I -Sample Question no 7

 
mrinal utkarsh
Greenhorn
Posts: 8
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question given is as follows

What is the result?
A) cougar c c
B) cougar c f
C) feline cougar c c
D) feline cougar c f
E) Compilation fails
F) An exception is thrown at run time.
Link to the page
The answer is C.

Please explain the solution. Why c c (why not f c) ? If I have to call a non-static superclass variable how it can be done?
Thank you!
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35269
383
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's only one type variable in that code. Search for "String type" to see where it is declared. This means when you change the value on line 15, it is changing that one variable. Since there is only one variable, this.type and super.type refer to the same variable (and same value.)

this.type and super.type is the correct way to differentiate between a subclass/superclass variable when you in fact have two different variables present.
 
mrinal utkarsh
Greenhorn
Posts: 8
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic