aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Annonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Annonymous class" Watch "Annonymous class" New topic
Author

Annonymous class

Khurram Fakhar
Ranch Hand

Joined: May 29, 2000
Posts: 65
Do your think that an Anonymous Class can extend, a class, and implement, a interface, at the same declaration?
plz give reason as well.
Regards
Khurram Fakhar
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi,
anonymous classes can either
1)extend a class
example
addWindowListener(new WindowAdapter(){
//only those required implemented
}
2)implement an interface
addWindowListener(new WindowListener(){
//all methods required
}

3) but not do both.

regds.
Rahul.
Jason
Ranch Hand

Joined: Dec 06, 2000
Posts: 35
Hi,
Not 100% true, the fact of matter is that when an anonymous class implements an interface, it also implicitly extends Object class.
Regards,
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Jason,
u have got thngs wrong when u say
"Not 100% true, the fact of matter is that when an anonymous class implements an interface, it also implicitly extends Object class."
I quote from JLS 9.1.3 to make things clear for u that
"There is no analogue of the class Object for interfaces; that is, while every class is an extension of class Object, there is no single interface of which all interfaces are extensions."
Thus interfaces do not implicit extend from Object.

Regards.
Rahul
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Rahul- that's the original version of the JLS. It was written before anonymous classes were added to the language. In the JLS second edition your quote above is modified to (Section 9.1.2):
<blockquote>While every class is an extension of class Object, there is no single interface of which all interfaces are extensions.</blockquote>
The first part of your quote has been removed since it causes confusion now. Later in section 15.9.1, "Determining the Class Being Instantiated" we have:
<blockquote>If the class instance creation expression ends in a class body, then the class being instantiated in an anonymous class. Then:
If the class instance creation expression is an unqualified class instance creation expression, then let T be the ClassOrInterfaceType after the <code>new</code> token. [...] If T is the name of an interface then an anonymous direct subclass of Object that implements the interface named by T is declared.
</blockquote>
So yes, all classes descend from Object; even anonymous classes.
As for the original question: yes, it's technically possible, but probably not in the way you mean. Jason has already shown one way; another is:
<code><pre>interface I {
void method();
}

class C implements I {
public void method() {
System.out.println("old implementation");
}
}

class Test {
public static void main(String[] s) {
C instance = new C() {
public void method() {
System.out.println("new implementation");
}
};
instance.method();
}
}</pre></code>
Here the anonymous class extends a class which has already implemented an interface, so we can also say that the anonymous class implements the interface.
What's not possible, however, is for an anonymous class to directly implement an interface while also extending a class other than Object. Which may or may not be what was meant by the original question.

"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hi Jim,
u r right. Yesterday i was in a hurry so i misunderstood the question that "do interfaces extend from Object". Anyhow things r clarified by u now.
By the way r there any significant changes between JLS 1 and JLS 2. I have gone through JLS 1 thoroughly but am a bit lazy with JLS 2 as it almost contains the same things as JLS 1.
so should i go through JLS 2 too.
Regds.
Rahul.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Annonymous class
 
Similar Threads
can non-static inner class access any member
WA #1.....word association
Doubt about diagram class (realization)
andrew has wii
Doubt about diagram class