• Post Reply Bookmark Topic Watch Topic
  • New Topic

MyClass.class.getName().equals(this.getClass().getName())  RSS feed

 
Abu Nene
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all I'm wondering there any concern using the subject as a condition in the parent class when the subclass is instantiated?


}
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its weird coding but anyway....the codition will return true where "this" in an instance of SubClass, false for an instance of ParentClass
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Johnson wrote:Its weird coding but anyway....the condition will return true where "this" in an instance of SubClass, false for an instance of ParentClass
Trying to restrict a subclass from a superclass looks to me like

It is conceivable you could use such coding in an equals method, but it is by no means common usage.
 
Abu Nene
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Tom Johnson wrote:Its weird coding but anyway....the condition will return true where "this" in an instance of SubClass, false for an instance of ParentClass
Trying to restrict a subclass from a superclass looks to me like

It is conceivable you could use such coding in an equals method, but it is by no means common usage.


Haa... ya I found it weird too. Guess I'll try other ways to implement this. Thanks guys for your comments!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ABu NeNe wrote:
Campbell Ritchie wrote:
Tom Johnson wrote:Its weird coding but anyway....the condition will return true where "this" in an instance of SubClass, false for an instance of ParentClass
Trying to restrict a subclass from a superclass looks to me like

It is conceivable you could use such coding in an equals method, but it is by no means common usage.


Haa... ya I found it weird too. Guess I'll try other ways to implement this. Thanks guys for your comments!


What are you trying to implement? Consider whether you couldn't just do it in the subclass constructor if the superclass constructor were rearranged a bit. Another alternative is to use the "template method" pattern: put the behavior that's different for that one subclass into a protected instance method, and call it from the superclass constructor. Then in the subclass, override that method with the new behavior.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!