Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why this is not compiling

 
Sekhar Chand
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class A
{
int a=5;
public void exam()
{
System.out.println("this is java");
}
}
class B extends A
{
int a=10;
public void exam()
{
System.out.println("This is java+j2EE");
}
}
class Base
{
public static void main(String args[])
{
A a1=new B();
System.out.println(a1.a);//value edi vastundi
B b1=(A)new A();//Actually it have to be ocmpile but fails when we try to run
System.out.println(b1.a);//value enta
}
}
 
Saurabh Vyas
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you put your comments in English so that we can understand as to what you want to say.

Also write your explaination as to why it should compile !!! -- This will help to clarify your doubt.
 
Krishna Srinivasan
Ranch Hand
Posts: 1862
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It shd be like this :
B b1=(B)new A();

You have written like B b1=(A)new A();
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ratna,
The problem is with this
B b1=(A)new A();
it is never required to typecast a object into its owntype.
However a narrowing/upcasting is required which can be done by
B b1=(B)new A();
[ November 16, 2006: Message edited by: Sanjeev Kumar Singh ]
 
Ramu Malur S R
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you say B b1 = new A(),
The objects that b1 can refer to should be of type B or anything that is a sub class of B.
Here A is not a sub class(but the base class) of B. Hence the error.

If it was B b1 = (B) new A(), where you explicity cast the object to B type, it would have compiled but resulted in ClasscastException at runtime.

When you print the value of the member variable, the value will be dependent on the reference type.
a1.a will print 5(Since a1 is declared of type A)

- Ramu

P.S. : Please use English in your queries
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

When we say subclass ref = superclass obj, results in compile time error but when there is a cast like subclass ref = (subclass) superclass obj, results in a run time error.

Anyone on this??
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting me guys.
I again have a doubt why the rumtime exception why it cann't be traced at the compile time i.e. one is trying to downcast a superclass reference?
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error message did you get when you tried to compile it?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic