0

I am writing a program where there are quite a lot number of methods in a particular class, so I decided to write an abstract class to help in keeping track of the methods.

Now, consider this: I have declared a method as abstract in the abstract class, and in the other class (which extends the abstract class), I want to override this method, but with access privilege reduced to private. Here, the compiler is giving a problem. It says that an attempt to assign weaker access privileges is being met with, which cannot be allowed. If I try to declare the method in the abstract class as protected (I have also changed the private ones to protected in the sub-class), it says that modifiers abstract and protected cannot be used together.

So, what can I do? Do I have to make the methods package access or public in both classes? If so, is there no way that I can declare these methods private?

Please note that I'm asking only for abstract classes, and not all classes in general.

Wrichik Basu
  • 1,005
  • 17
  • 28

2 Answers2

1

What do you mean it can not be protected abstract - of course it can.

And the thing that you want to do is basically prohibited by the compiler and the language itself in the first place.

Eugene
  • 117,005
  • 15
  • 201
  • 306
0

The answer to your question is: there's nothing you can do to reduce the visibility of a method declared in a parent class.

If you can restate what you're trying to accomplish by "keeping track of the methods" in an abstract parent class, you might get a different solution.

Isaac Truett
  • 8,734
  • 1
  • 29
  • 48