Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ramya narayanan
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed!!!

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 326
Android Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21129
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49405
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed with sudipto
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic