Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annonymous class

 
Khurram Fakhar
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic