• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Call protected method in another package

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package package1;
public class A {
protected void display(){
System.out.println(“run display method“);}}




package package2;
import package1.A;
class B extends A{}
public class C {
public static void main(String[] args){
new B().display(); // Line A
}
}

Explain the reason for the compile error at Line A
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What compiler error? Be sure to include the text of any error messages.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And tell us why you think the error is occurring. Or why you think it shouldn't occur.
 
Damith Priyanka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your response.I am new to java language. I have heard that protected methods
in a superclass canbe invoked/called by the subclasses in another package through an instance of the subclass .In this code protected display method inherits to class B which is in an another package. but why can't I invoke that method by creating an instance of class B.


package package1;
public class A {
protected void display(){
System.out.println(“run display method“);}}


package package2;
import package1.A;
class B extends A{}
class C extends A{
public static void main(String[] args){
new B().display(); // Line A
new C().display();//Line B
}
}


Line B compiles fine.
Line A shows compile error.(CE - display() has protected access in package1.A)

sorry for unclearness . I access the site by mobile phone.
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclass'es of the subclass . (quoted from SCJP certification by K&B)

What this means is as follows;
in your code in package2 there is a file named ClassC that is inheriting from Class A in package 1. no problems there
However in the same class you also have another classB that is inheriting from class A but here lies the fun part now;
In ClassC main method you are creating an instance of another class B and trying to invoke the display method, this will not work? Why, because as stated above, "Once the subclass-outside-the-package inherits the protected member, that member (as inherited by the subclass) becomes private to any code outside the subclass". From this we can infer that if Class B was declared in a separate file inheriting Class A with the main method the code would have compiled and executed BUT if you try creating an instance of Class B in Class C and want to invoke the inherited method it will not compile as this inherited protected method for Class B has now become private for Class C.

Please take a look at the following exception now;





I hope this helps in clarifying your doubts
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can of course override a protected method as public.
 
Damith Priyanka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no any way to call a non static protected method from a outside package if we don't extend that class (which has protected method) to a class with main method.We can call that method in another class in the second package only after inheriting that method to a class with main method in the second package. Am I correct?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damith Priyanka wrote:There is no any way to call a non static protected method from a outside package if we don't extend that class (which has protected method) to a class with main method.We can call that method in another class in the second package only after inheriting that method to a class with main method in the second package. Am I correct?



That's a lot of prose for a straightforward concept... protected methods can be called from classes in the same packages and from sub-classes. Period.

Henry
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Damith Priyanka wrote:Explain the reason for the compile error at Line A


Because you're trying to call the display() method from outside B. Question: where is your main() method?

IMO, Java's implementation of protected is flawed and leads to a lot of confusion. The fact of the matter is that protected methods are meant for use by subclasses - where they are shouldn't be a factor. If you're subclassing, chances are good that your class isn't going to be in the same package as the existing one anyway, so the whole business of "same package" visibility is a red herring.

Winston
reply
    Bookmark Topic Watch Topic
  • New Topic