• Post Reply Bookmark Topic Watch Topic
  • New Topic

override a method in object retruned from super class  RSS feed

 
miro swartch
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the second block of code does not compile please explain me why and what is the workaround ? i can override a method on a object when I initialize with new but cannot override if I get the object from super or somebody constructs why ?


[ October 16, 2008: Message edited by: miro con ]
[ October 16, 2008: Message edited by: miro con ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are not overriding the getBookmarkablePageLink() method, but invoking it, so you have to pass the correct arguments. Which you appear not to do in the second bit of code.
 
miro swartch
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happened?
 
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
Hi Miro,

In the case that works, with the new, you're actually defining a new class, and then creating an instance of it. You're using the special syntax which defines an anonymous class.

In the second case, which doesn't work, you're essentially trying to override a method on an existing object, and that is not legal in Java (although some languages do allow it, Java is not one of them.)

What you can do to modify the behavior of the object returned by the superclass is to provide a wrapper object. The wrapper object implements the behavior of the original class by calling the methods of the object being wrapped, adding additional code as needed. It might look like this:



This is a little complicated, but I hope you get the idea. I've defined a new class which extends BookmarkablePageLink and overrides most methods to call the method of the same name on another instance of BookmarkablePageLink -- the instance returned by the superclass method. Then I return an instance of this new class.
[ October 17, 2008: Message edited by: Ernest Friedman-Hill ]
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, EFH; that appears to be what the customer really wanted.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!