This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Java in General and the fly likes  Anonymous classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark " Anonymous classes" Watch " Anonymous classes" New topic
Author

Anonymous classes

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 203

Hi Folks,

I was going through the concept of Anonymous classes and Inner classes I, I have one query with regard to Anonymous classes is that if we have a super class and we want to override the method of superclass but without subclassisng means without following inheritance then in that case the Anonymous class is the only option..?

Secondly whats the purpose of introducing the Anonymous class in java technically please justify ..!!!
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 4106
    
  19

What do you mean, without inheritance? Anonymous classes are exactly the same as other classes that extend some super class. The difference is that anonymous classes have no name. Inheritance works as usual.


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3719
    
  61

Saral Saxena wrote:Secondly whats the purpose of introducing the Anonymous class in java technically please justify ..!!!

In my opinion, the purpose is to provide yet another way to structure the code. Anything you can do with anonymous classes can be done with other types classes as well. Anonymous classes allow you to save a few keystrokes, as well as to place related pieces of code closer together. The latter reason is probably much more important of the two.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8663
    
  24

Saral Saxena wrote:...and we want to override the method of superclass but without subclassisng...

The statement is fallacious: you cannot override the method of superclass without subclassing. An anonymous class is a subclass; it's just not a named type.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous classes
 
It's not a secret anymore!