• Post Reply Bookmark Topic Watch Topic
  • New Topic

anonymous subclass  RSS feed

 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All at the Ranch:

In prior posting I was referred to this article http://www.artima.com/lejava/articles/equality.html,
regarding the Equality Method in Java. It was a good read.

There was a bit of code in the article that kinda scared me.
Please take a look at the following:

We have a Point class


And then pAnon an anonymous subclass of Point.




This anonymous subclass seems to me to be a sneaky thing to do.
Just seems to be creating a debugging/maintenance nightmare.
Why would you ever want to do this?


Best Reagrds

KD




 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Artima? Is that Odersky Spoon and Venners? Did they say that was a potential pitfall that the class returns 2 regardless? That example may be there to illustrate a potential error.
 
kevind duff
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
How to Write an Equality Method in Java
by Martin Odersky, Lex Spoon, and Bill Venners

They are of course just illustrating a point,
but as a general concept, would this anonymous subclass
thing be considered good form.

Just asking.

KD
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing inherently wrong with anonymous subclasses. They can be implemented correctly or poorly just like any other class. That same error could have been made in any subclass of Point, whether anonymous or not, and that kind of error could have been made in any class, even a top level (but for Object) class.

The anon subclass part of it has nothing to do with what's wrong there.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the only thing really wrong there is that the c'tor doesn't agree with what getY() returns. Once it's constructed, though, it will behave correctly, since y is never accessed directly. And since nobody ever sees the (1, 1) c'tor, there won't be any confusion.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!