• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access the parent class variable and execute its method in java  RSS feed

 
Fendy Chang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurus,

I have following "DetailPage" seeded class reside in package oracle.apps.inv.mo.server which i need to extend.
I need to to remove the existing listener for mNextBtn and use my own listener. But the problem is since the mNextBtn is not defined as public, i can used it directly as my extension class is reside in different package.
Is there any solution for this as we have policy to keep the extension class in separate location? Is field reflection can be used ?


Many thanks in advance,
Fendy

Seeded Class
=========


Extension class
==========

 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fendy Chang wrote:Hi Gurus,

I have following "DetailPage" seeded class reside in package oracle.apps.inv.mo.server which i need to extend.
I need to to remove the existing listener for mNextBtn and use my own listener. But the problem is since the mNextBtn is not defined as public, i can used it directly as my extension class is reside in different package.
Is there any solution for this as we have policy to keep the extension class in separate location? Is field reflection can be used ?


I'm guessing you are not allowed to change the super class to make the button protected? Are there other constructors in the super class? Maybe you can call a constructor that doesn't add the listener.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It sounds from what E Armitage says that what you are asking about may be impossible. Please explain What you want to do.
I have added code tags to your post, and doesn't it look better. One comment was too long so I changed it to three lines.
 
Fendy Chang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi E Armitage,

Thanks for your quick response.
There is no other constructor.

Normally, there is a public method that returning the handle of the variable so it can be referenced outside the package like following
public ButtonFieldBean getNextBtn()
{
return mNextBtn;
}

But unfortunately, there is no such method given in seeded class. Is java field reflection getDeclaredFields can solved this issue?

Many thanks in advance,
Fendy
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fendy Chang wrote:Hi E Armitage,

But unfortunately, there is no such method given in seeded class. Is java field reflection getDeclaredFields can solved this issue?

Many thanks in advance,
Fendy


That should be the absolute last resort and be documented as an ugly workaround if used. Either
1.) change the original code to make the button protected or
2.) Write a new class that replaces the super class and has better maintainable code or
3.) Make the child class completely replace the super class by re declaring the button or
4.) Don't change the button. The original code was written in a such a way that only code in the same package can change that button so don't break that rule or
5.) Change your packaging rules and put that subclass in the same package as the super class.

All these suggestions are IMO better than using reflection.
 
Fendy Chang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi E Armitage,

I think the most possible one is point 5.
For the testing I would like to know how the reflection is working as i never use it before, i try following but it is showing "Method addListener not found"

Field fldNextBtn = DetailPage.class.getDeclaredField("mNextBtn");
fldNextBtn.setAccessible(true);
fldNextBtn.addListener(xxListener);

Many thanks in advance,
Fendy
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use option 5 then you don't need to use reflection. Just write a new constructor that doesn't add the listener to the button.
 
Fendy Chang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi E Armitage,

For that part, i already completed whereby the extension class i put in the same package.
I would just want to know how is it done by using reflection.

Many thanks in advance,
Fendy
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't call addListener on a Field because Field does not have a method called addListener. Once you get the Field you must first get the Field's value (there is a get method) and then cast that Field value into a ButtonFieldBean on which you can then call the addListener method.
 
Fendy Chang
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi E Armitage,

I manage to do that. Thanks a lot for your advice.

Regards,
Fendy
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!