Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding subclassing

 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the 'super' keyword.
 
Philip Shanks
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic