Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sub class fundu question

 
parag bharambe
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends.
I encountered following line of code in vivak exam.
class Super
{ int index = 5;
public void printVal()
{ System.out.println( "Super" );
}
}
class Sub extends Super
{ int index = 2;
public void printVal()
{ System.out.println( "Sub" );
}
}
public class Runner
{ public static void main( String argv[] )
{ Super sup = new Sub();
System.out.print( sup.index + "," );
sup.printVal();
}
}
What will be printed to standard output?
a) The code will not compile.
b) The code compiles and "5, Super" is printed to standard output.
c) The code compiles and "5, Sub" is printed to standard output.
d) The code compiles and "2, Super" is printed to standard output.
e) The code compiles and "2, Sub" is printed to standard output.
f) The code compiles, but throws an exception.
By my guessing ans should be e ie display be 2 , sub. But Ans is c ie 5, sub.
So please tell me why it is 5 and not 2;
Question 2)
This is on assignment and automatic conversion.
Why the following line of code hold true.
byte b=3;// int assigned to byte.ok why?
Thanking in Advance
parag


[This message has been edited by parag bharambe (edited November 15, 2000).]
 
Rajesh Hegde
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Parag,
1. When it comes to variables, the compiler goes by the class trpe of the reference value . In case of "Sup" the type of the reference value is "Super". Now in Class Super- the value of index is 5. So u get that answer.
2. I am not too sure. But i think the compiler goes by the range.If u give more than 127 to a byte var, there will be a compiler error.
Rajesh
 
Suresh Selvaraj
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The correct answer is 'c', i.e. " 5 and Sub " will be printed.
Explanation:
Super sup = new Sub();
Here sup is defined to be of type Super and refers to Object Sub. The variable that is invoked depends on the type and the method that is invoked depends on the Object reference itself.
Hence, sup.index will refer to the variable "index" in class Super (as sup is defined to be of type Super) and sup.printVal() will invoke the printVal() method in class Sub.
- Suresh Selvaraj
 
amrit singh
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi!!!
well in short
NO POLYMORPHISM IN CASE OF VARIABLES.
i think so .
am i right pls tell me
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

  1. Suresh is right. Fields are accessed based on the object's declared type at compile time. Methods are accessed based on the object's actual type at runtime.

  2. <pre>
    byte b = 3; // works even though 3 is an int
    </pre>
    This works as for byte, short and char assignments the compiler checks if the value will fit the declared type. If it does, it uses narrowing conversion. (See JLS §5.2 Assignment Conversion).

  3. Hope that helps.
    ------------------
    Jane
    The cure for boredom is curiosity.
    There is no cure for curiosity.
    -- Dorothy Parker
 
rajan raju
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi parag
As you know we can instantiate these calsses in three ways.
1. Super sup = new Sub() // o/p is super int (5), Sub
2. Super sup = new Super() // super int (5)' Super
3. Sub sub = new Sub() // sub int (2), Super

case 1. I may not be using right words, but here object is referencing to super class, so super int will come in to picture. of course method will be o/r in case 1 and 3.
case 2. here it is clear and only super class is constructd so no o/r.
case 3. here object ref is created to sub so it can see both classes variables and methods anyway.
One more interesting point is similar related principle is applicable to object ref casting also. pl. check it out or go to maha annna discussions of lang. fund on this site.

I think the above will help you to some extent.There maybe typo mistakes, as i have to write exam on 21st i can't spend more time here.

rajan.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic