• Post Reply Bookmark Topic Watch Topic
  • New Topic

Call protected method in another package  RSS feed

 
Damith Priyanka
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What compiler error? Be sure to include the text of any error messages.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!