• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Annonymous class

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic