user11319000
user11319000

Reputation:

How to hide a class method within the same package

So I have a package called ABC with class A, class B and class C. Now my main class is outside the package and calls a Class A method called show() which is a public static method. Basically Class A acts like a gateway for package ABC. Now I have class C extend class B and I have an abstract method called execute() in class B thats been overridden in class C. Now the access modifier for execute() is protected. Now I can't access execute() from main which is what I want but I can still access it from Class A because it's inside the same package. How can I hide execute inside the same package, i.e how do I hide execute() in Class A but still be able to access it in Class C?

Upvotes: 1

Views: 599

Answers (2)

user1234SI.
user1234SI.

Reputation: 1869

The only way you can hide the execute() from class A is to make it private in class C. But since the execute() is an abstract method in class B, this combination is illegal (private + abstract). Even if you make it protected, you can't override it and make it private in the class B, like in this example:

abstract class B 
{
 protected abstract void execute(); 
}

and :

class C
{
 @Override
 private void execute() {} // not working
}

Because it's illegal to assign a weaker access privileges when you override a method.

Upvotes: 1

Alex Sveshnikov
Alex Sveshnikov

Reputation: 4339

Unfortunately Java does not have the corresponding visibility modifiers, protected is a superset of package-private.

Upvotes: 1

Related Questions