aspose file tools*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sub class fundu question" Watch "Sub class fundu question" New topic
Author

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 + "," );
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

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.
Rajesh
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
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


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

Joined: Nov 03, 2000
Posts: 42
hi!!!
well in short
NO POLYMORPHISM IN CASE OF VARIABLES.
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
    </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


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

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.

rajan.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sub class fundu question