wood burning stoves*
The moose likes Beginning Java and the fly likes How to call a protected method in other packages non-subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to call a protected method in other packages non-subclass" Watch "How to call a protected method in other packages non-subclass" New topic
Author

How to call a protected method in other packages non-subclass

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
I want to call a protected method from a nonsubclass in other package .
How to do it.

Suppose I have a class named c1 which has a protected method printing() in package p1.

package p1;
class C1
{

protected void printing()
{
System.out.println("Sai");
}
}


Suppose this method printing() I need to call in another package's nonsubclass how that can be done

package p2;
import p1.*;
class c2 extends c3
{
public static void main(String[] args)
{
C1 c1inst=new C1()
//Here i want to call the protected method
//The following line will certainly throw error protected property cannot //non-subclassbe accessed in a nonsubclass in other package
c1inst.printing();
}
}


How that can be achieved?
Any comments.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

protected access modifier allows your subclasses be outside your superclass package and yet still inherit pieces of the class including methods and constructors..
The only way the subclass can access the protected methods is by inheritance...
Correct me if I am wrong but protected applies only to inheritance.


Regards, Sud.
SCJP 5 ScjpFAQ JLS
ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear Shekhar,
Thanks for your suggestions.
I've found out how to invoke the protected methods outside the package i.e. by using Reflection API.

Original code:
package p2;
import p1.*;
class c2 extends c3
{
public static void main(String[] args)
{
C1 c1inst=new C1()
//Here i want to call the protected method
//The following line will certainly throw error protected property cannot //non-subclassbe accessed in a nonsubclass in other package
c1inst.printing();
}
}


Code using Reflection API to invoke protected methods
package p2;
import java.lang.reflect.*;
import p1.*;
class c2 extends c3
{
public static void main(String[] args)
{
C1 c1inst=new C1()
Method m = c1inst.getClass().getDeclaredMethod("printing", null);
m.setAccessible(true);
m.invoke(t, null);

}
}


After ignoring the warning when you run it:
Sai

is printed.
That's all .
Regards.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Agreed!!!



cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Using Reflexion is just violation of Object Oriented Programming
Its very strong because it violates what we learned can't be done.
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Originally posted by Chandra Bhatt:
Using Reflexion is just violation of Object Oriented Programming


Thank you Mr. Bhatt.

What can be done is always not legal...



Following the OOP norms in Java is very important, which has made Java so secure and powerful language.


Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Also, if I would find out that someone was doing that, I would crank the security on my protected class so that reflection is not allowed and then your application would be broken. *Evil laugh*
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by Ove Lindstr�m:
*Evil laugh*


That should be spelt mwaaaaaahaaaaahaaaaaahaaaaaaaahaaaaaaaaa.

What you do is ask what product he is working on, so you can make sure never to use it.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Originally posted by Ove Lindstr�m:
Also, if I would find out that someone was doing that, I would crank the security on my protected class so that reflection is not allowed and then your application would be broken. *Evil laugh*
Funny thing, I actually found I had to use that exact technique last week.

I was working on a Swing project which uses drag-and-drop on JTrees. The basic functionality for DnD is built in to JTree, but you can't extend it by writing a subclass of JTree. That's because the functions are performed by a TransferHandler and not by methods of the JTree itself.

You can get the TransferHandler, but of course you can't directly subclass an object. But you can delegate to an object. All that requires is to write a new DelegatingTransferHandler class which implements all of the methods. Each method just calls the corresponding method of the original TransferHandler with the same parameters and returns its result, if any. Then you can create subclasses of that class.

Only problem is, some of the TransferHandler methods are protected. That's where this trick comes in. It's working fine and I don't feel bad about it at all. After all, what I am doing there is equivalent to writing a subclass of TransferHandler.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
That is similar to Rob Prime's complaints about classes in the java.lang package with default access. There are classes which ought to have been given less restrictive access.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

I found just another perfect example just yesterday.

AbstractMap has a static classes called SimpleEntry. It is public in Java 6.

I've tried to access it in Java 1.4, and got a nasty surprise - default access. With a note in its Javadoc:

I got curious and checked Java 5.0 - still default access, still the same Javadoc. Guess ASAP for Sun means skipping an entire major release.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
Agreed with sudipto
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call a protected method in other packages non-subclass