DZone Forums
Go Back   DZone Forums > Community > Languages & Frameworks > Java
Reload this Page override a method in object retruned from super class
Notices
Reply
 
LinkBack Thread Tools Display Modes
  (#1 (permalink)) Old
Member
 
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Join Date: Aug 2008
Default override a method in object retruned from super class - 10-16-2008, 04:02 PM

Code:
		customMenuComponent.addCustomLink(new CustomLinkComponenet("strlink","Storage Inbox",Index.class) {
			protected BookmarkablePageLink getBookmarkablePageLink() {
				
				BookmarkablePageLink bookmarkablePageLink=   new BookmarkablePageLink("link",clazz){
					protected void onBeforeRender() {
						super.onBeforeRender();
					}
				};
				return bookmarkablePageLink;
			}
		});
		
		
		customMenuComponent.addCustomLink(new CustomLinkComponenet("strlink","Storage Inbox",Index.class) {
			protected BookmarkablePageLink getBookmarkablePageLink() {
				
				BookmarkablePageLink bookmarkablePageLink=   super.getBookmarkablePageLink(){
					protected void onBeforeRender() {
						super.onBeforeRender();
					}
				};
				return bookmarkablePageLink;
			}
		});
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 ?
Reply With Quote
  (#2 (permalink)) Old
Member
 
Posts: 5
Thanks: 0
Thanked 1 Time in 1 Post
Join Date: Jun 2008
Default 10-20-2008, 07:18 PM

simply put

new BookmarkablePageLink("link",clazz){....}

is defining a class and instantiating it at the same time. Its called an annoynomous class. the one lne above is effectively doing:

Class AnnonClass extends BookmarkablePageLink {.....};
new AnnonClass("link",clazz);

super.getBookmarkablePageLink() is call to a method. It returns an instance of BookmarkablePageLink. You can't redefine an instance on the fly.

Why are you trying to the 2nd approach?

Andy
Reply With Quote
  (#3 (permalink)) Old
Member
 
Posts: 12
Thanks: 3
Thanked 1 Time in 1 Post
Join Date: Jan 2009
Default 01-29-2009, 10:41 AM

As a suggestion, I've seen API's that use a construction technique where every object is extended from an interface.

If you have access to the BookmarkablePageLink class instead of:

BookmarkablePageLink bookmarkablePageLink = super.getBookmarkablePageLink(){
protected void onBeforeRender() {
super.onBeforeRender();
}
};

you could try something like

BookmarkablePageLink bookmarkablePageLink = new BookmarkablPangeLink(super.getBookmarkablePageLink ()) {
protected void onBeforeRender() {
super.onBeforeRender();
}
};

Assuming that BookmarkablePageLink is an extension of whatever super.getBookmarkablePageLink() returns you can use the constructor in the BookmarkablePageLink class to construct the object. Then you should be able to override the BookmarkablePageLink they way you were approaching it.

If you don't have access to the BookmarkablePageLink class you should extend it and add the constructor to create the object from whatever the super.getBookmarkablePageLink() object is... Unless BookmarkablePageLink is final... I hate it when people don't let me extend stuff.

Last edited by pjupson; 01-29-2009 at 10:41 AM. Reason: <code> tags didn't work the way I expected
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Widget Method Handler and Retargetting Actions Niels Bech Nielsen Eclipse 0 01-07-2009 05:52 AM
Override a 3rd party plugin class - RCP SatishP Eclipse 0 11-25-2008 05:18 AM
beanshell function override edmund Java 0 08-05-2008 05:30 AM
Override Doubleclick event of a folder in Resources view of Eclipse divyanair Eclipse 0 06-30-2008 04:50 PM
Accessing data structures within methods from outside the method patwa Java 0 04-11-2008 03:19 PM


Copyright 1997-2009, DZone, Inc.
vBulletin Skin developed by: vBStyles.com