jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why this is not compiling 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 "why this is not compiling" Watch "why this is not compiling" New topic

why this is not compiling

Sekhar Chand
Ranch Hand

Joined: Apr 05, 2006
Posts: 73
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

Joined: Sep 02, 2003
Posts: 72
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

Joined: Jul 28, 2003
Posts: 1850

It shd be like this :
B b1=(B)new A();

You have written like B b1=(A)new A();

Krishna Srinivasan
OCAJP 7/8 Exam, 400+ OCPJP Mock Questions
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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 ]

~Sanjeev Singh<br />SCJP 1.5
Ramu Malur S R

Joined: Oct 17, 2006
Posts: 26

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

"Excellency is rarely found, more rarely valued"
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

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??

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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

Joined: Sep 14, 1999
Posts: 2813
What error message did you get when you tried to compile it?

SCWCD: Online Course, 50,000+ words and 200+ questions
I agree. Here's the link: http://aspose.com/file-tools
subject: why this is not compiling
It's not a secret anymore!