Results 1 to 8 of 8
- 02-24-2009, 06:55 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 14
- Rep Power
- 0
anonymous (unnamed) Classes by default always folllows Overriding concept
hi all,
my question is related to anonymous (unnamed) classes.
Anonymous classes are always sub-class of the some "new ClassName()". so my question is that if it is always sub-class of some class and if it is define the method that is already defined in super class then it is overriding the method. or we can say that it is by default follow the overriding principle.
I need your thoughts on this Topic.
Example Attached from SCJP Kathy Siera Book.
class Popcorn {
public void pop() {
System.out.println("popcorn");
}
}
class Food {
Popcorn p = new Popcorn() {
public void pop() {
System.out.println("anonymous popcorn");
}
};
}
The Popcorn reference variable refers not to an instance of Popcorn, but to an
instance of an anonymous (unnamed) subclass of Popcorn.
Let's look at just the anonymous class code:
2. Popcorn p = new Popcorn() {
3. public void pop() {
4. System.out.println("anonymous popcorn");
5. }
6. };
Regards,
Mahendra Athneria
Mumbai- India
- 02-24-2009, 08:47 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Cross post asked and answered forums.sun.com/thread.jspa?messageID=10620074
- 02-24-2009, 03:44 PM #3
Anonymous classes follow the same rules as named classes. They inherit from the base class or interface, they can override non-final methods, and they can declare new methods.
- 02-25-2009, 07:14 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
While it's true that an anonymous class can declare a new method, that new method can only be accessed within the anonymous class or it's inner classes, even if it's public.
...except by reflection. There is some crazy crap code out there, I tell you what.Last edited by toadaly; 02-26-2009 at 06:45 AM. Reason: oops, meant inner classes, not subclasses
- 02-25-2009, 02:20 PM #5
Toadaly is right about defining new methods. I was making a general observation, but in practice, I don't remember seeing it done.
- 02-25-2009, 10:25 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Part of the problem about new methods in an anonymous class is that the class itself becomes longer and more complex - so that it is less likely to be a candidate being anonymous.
But what do subclasses of an anonymous class look like? Maybe I'm not thinking straight, but I couldn't figure out what toadaly was getting at there.
- 02-26-2009, 04:08 AM #7
Extending an anonymous class? Who started this discussion, anyway? Oh, yeah... All I meant to get at is that anonymous classes don't follow any special rules regarding methods. In practice, I use them sparingly, usually just to invoke a method of the containing class...
- 02-26-2009, 06:46 AM #8
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
Anonymous classes ...
By Nicholas Jordan in forum Advanced JavaReplies: 6Last Post: 11-27-2008, 06:02 PM -
[SOLVED] Using classes and overriding one class for another
By StealthRT in forum New To JavaReplies: 3Last Post: 04-08-2008, 07:12 AM -
is overriding static method possible
By raghu in forum Advanced JavaReplies: 1Last Post: 01-22-2008, 12:38 AM -
Name of Anonymous class
By eva in forum New To JavaReplies: 1Last Post: 12-31-2007, 01:07 PM -
Anonymous class
By ravian in forum Advanced JavaReplies: 3Last Post: 12-25-2007, 10:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks