jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class question from Dan's exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class question from Dan Watch "Inner class question from Dan New topic
Author

Inner class question from Dan's exam

Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
Why doesn't this code compile?
abstract class A {
private int x = 4;
private int y = 2;
public int x() {return x;}
public void x(int x) {this.x = x;}
public int y() {return y;}
public void y(int y) {this.y = y;}
}
interface B {int math();}
class C {
static class D extends A implements B {
public int math() {return x()+y();}
}
static A a1 = new A() implements B{
public int math() {return x()+y();}
};
public static void main(String[] args) {
System.out.print(new C.D().math());
System.out.print(a1.math());
}
}
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Thank you for using my exam.
Code is a lot easier to read when it is posted inside of a code block. Just use the "Instant UBB Code" options that are available on the screen used to post messages. In this case, just click the "CODE" button and then paste the code inside of the blocks.


An anonymous class can extend a superclass or it can implement an interface. However, an anonymous class declaration that extends a superclass is not permitted to also have an implements clause. In this case, the anonymous class referenced by a1 attempts to extend class A but a compile-time error is generated due to the "implements" clause.
I hope that helps.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
I don't see the anonymous class exteding any class here...
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Hemal Mehta:
I don't see the anonymous class exteding any class here...

The following anonymous class declaration extends class A and overrides method x. This anonymous class declaration is legal.

The following anonymous class declaration implements interface B. This declaration is legal.


The following annonymous class declaration attempts to extend class A and implement interface B. This declaration is not legal because an anonymous class declaration can not have an "implements" clause.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class question from Dan's exam