File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sub class fundu question 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 "Sub class fundu question" Watch "Sub class fundu question" New topic

Sub class fundu question

parag bharambe
Ranch Hand

Joined: Sep 01, 2000
Posts: 41
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 + "," );
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

[This message has been edited by parag bharambe (edited November 15, 2000).]
Rajesh Hegde
Ranch Hand

Joined: Sep 15, 2000
Posts: 112
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.
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
The correct answer is 'c', i.e. " 5 and Sub " will be printed.
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

Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="" target="_blank" rel="nofollow"></a>
amrit singh
Ranch Hand

Joined: Nov 03, 2000
Posts: 42
well in short
i think so .
am i right pls tell me
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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
    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.
    The cure for boredom is curiosity.
    There is no cure for curiosity.
    -- Dorothy Parker

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
rajan raju

Joined: Sep 22, 2000
Posts: 14
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.

subject: Sub class fundu question
It's not a secret anymore!