What will happen if you compile/run the following code?
1: class Test 2: { 3: static void show() 4: { 5: System.out.println("Show method in Test class"); 6: } 7: } 8: 9: public class Q2 extends Test 10: { 11: static void show() 12: { 13: System.out.println("Show method in Q2 class"); 14: } 15: public static void main(String[] args) 16: { 17: Test t = new Test(); 18: t.show(); 19: Q2 q = new Q2(); 20: q.show(); 21: 22: t = q; 23: t.show(); 24: 25: q = t; 26: q.show(); 27: } 28: }
A) prints "Show method in Test class" "Show method in Q2 class" "Show method in Q2 class" "Show method in Q2 class"
B) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Test class"
C) prints "Show method in Test class" "Show method in Q2 class" "Show method in Test class" "Show method in Q2 class"
D) Compilation error.
the answer is D and the reason given is "explicit casting required at line 25".
if a subclass obj has to refer to a superclass' object how it can be done can anybody tell me pl.
Rao Raghu
Ranch Hand
Joined: Jan 05, 2007
Posts: 100
posted
0
Use the 'super' keyword.
RAGHU<br /> <br />"When the going gets tough, the tough get going"
Philip Shanks
Ranch Hand
Joined: Oct 15, 2002
Posts: 189
posted
0
I'm not real great at explaining this stuff, but I'll try anyway.
In object inheritance, you generally add to or change the methods and properties of the base class, but you do not actually remove any of them, so object type casting only works in one direction.
Consider; what if class Q2 contains methods that do not exist in class Test?
If you could assign q=t with or without first casting it to Q2, then you could also try to call a (nonexistent) Q2 method on an object that is really a Test object.
If you try to make the assignment without a cast, then you get a compile time error; the compiler says, "I got a Test object, but this assignment requires a Q2 object," assuming that you know that Q2 subclasses Test. But you say, "Okay, I'll just cast it to Q2" and it compiles.
Great, but now you have a runtime error, because you can't cast types up the inheritance hierarchy -- the parent class might not have all of the methods, etc that the child class has.
Philip Shanks, SCJP - Castro Valley, CA
My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
Srinivasa Raghavan
Ranch Hand
Joined: Sep 28, 2004
Posts: 1228
posted
0
In addition to this, comment line 25 & run the above code twice. First time with show method as static & next time with non static show method. You 'll get to know more !
i didnt get you exactly.as the statement at 25th line ie "q=t" is wrong how it can be made error free with casting does it like q=super(t)?if not whats the other way
vatsalya rao
Ranch Hand
Joined: Feb 14, 2007
Posts: 63
posted
0
i didnt get you exactly.as the statement at 25th line ie "q=t" is wrong how it can be made error free with casting does it like q=super(t)?if not whats the other way
Srinivasa Raghavan
Ranch Hand
Joined: Sep 28, 2004
Posts: 1228
posted
0
Super key word should be used only for accessing the members of superclass.
The following code is a example of Type Casting object = (ClassName)anotherObject;
[ February 15, 2007: Message edited by: Srinivasa Raghavan ]
Raj Kumar Bindal
Ranch Hand
Joined: Apr 15, 2006
Posts: 409
posted
0
We cannot assign base class object to derived class reference.Through casting also it is not possible. so, q=t cannot be done in any case. Regarding super, In derived class show() method we can write super.show() , but for that you have to remove static from your show method of both the classes because super cannot be used in a static method.
Philip Shanks
Ranch Hand
Joined: Oct 15, 2002
Posts: 189
posted
0
Originally posted by vats rao: i didnt get you exactly.as the statement at 25th line ie "q=t" is wrong how it can be made error free with casting does it like q=super(t)?if not whats the other way
This will allow the code to compile, but will result in a runtime error.