wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions on inner class & cast down Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions on inner class & cast down" Watch "Questions on inner class & cast down" New topic
Author

Questions on inner class & cast down

Orion beta
Greenhorn

Joined: May 31, 2002
Posts: 1
Would u pls answer my questions on the mock exam from China IT Lab.
Q2. "A class defined within a method can only access final methods of the enclosing method" is true
Why there is 'final methods' the enclosing method?
Does it means that the enclosing method contains final method which is accessible by inner class?

Q7.
class A{
protected int i;

A(int i){
this.i=i;
}

}
The answer said that the following class B is NOT a valid inner class for A.
class B{
B(){
System.out.println("i = "+i );
}
}

But why? I have tried the code and it CAN compile!!

Q83.
What will happen when try to compile and run the following code?
public class CEx{
public static void main(String argv[]){
Base b = new Base();
Sub s = (Sub)b;

}
}
class Base{}
class Sub extends Base{}
class Sub2 extends Base{}
Answer: It will give Runtime exception.
I tried this code and found that it gave ClassCastException.
Is the ClassCastException ALWAYS given when an instance is casted down?
thx a lot!
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Orion
Explanation sample to your 1st question

Explanation to your 2nd question

Is the ClassCastException ALWAYS given when an instance is casted down?

Yes, and ClassCastException is thrown in runtime. When instance is casted down, code compiles correctly.

Jamal Hasanov
www.j-think.com
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Q.2 That sentence makes not much sense for me: a method having another methods ? Instead the following can be considered:
"A class defined within a method can only access final local variables of the enclosing method" This is not true. A local class can also access the fields declared in a containing scope (classes). Even considering this for granted, we could argue that a local class can access the final parameters of the method too.
The exam-proof sentence is: A compiler error is produced if a local class tries to access local non-final variables or parameters of the containing method.

Q.7 I agree with you it's a valid inner class.
Q.83 The CastClassException is thrown at runtime when the JVM checks if the object pointed by the variable (b) has a type at runtime (Base) that is assignment compatible
JLS 5.2 with the type mentioned in the cast operator (Sub). Because Base is not the same class or a subclass of Sub the exception is thrown. The intent of the JVM is to prevent that a reference (s) of a given type (Sub) could point to an object (new Base()) whose type is not the same or a subclass of the type of the variable.
The reason: It would be possible that the type Sub had declared some extra methods not declared in Base. If the runtime check of the cast would allowed the cast, s would end up pointing an object of type Base that wouldn't know how to respond to those extra methods declared in Sub but not in Base. Just because its type is Base and not Sub. That is in Java you can treat an object only as the type really is.
Read more on casting on JLS 5.5


SCJP2. Please Indent your code using UBB Code
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

"Orion Belt"
Please adjust your displayed name to meet the JavaRanch naming policy.
Particularly your name should not be "Obviously Fictitious".
You can change your name here.
Thanks!! and Welcome to the JavaRanch!
 
Don't get me started about those stupid light bulbs.
 
subject: Questions on inner class & cast down
 
Similar Threads
final being overridden??
casting objects
Are my answers correct ?
java
Inner class