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

Questions on inner class & cast down

 
Orion beta
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic