Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed!!!

 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 326
Android Mac OS X Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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*
 
Marshal
Posts: 74695
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 26958
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 74695
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22573
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed with sudipto
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic