This question already has answers here:
how to restrict protected method access to only subclasses
(2 answers)
Closed 5 years ago.
I have a question I hope you can help me with:
I have an abstract class that contains some utility abstract methods, but I want these methods to be restricted only to subclasses, I obviously can't use a private modifier, but protected is not enough because in the same package there are also classes that don't extend it, but they can use these methods if the extended ones are instantiated in them:
What can I do?
Thanks!
You can't do it. The only way is to rearrange your package structure in a way, that only the base class and its children were inside the package. To achive this, you may move all the classes that aren't children into a subpackage - they won't see protected methods.
Out of curiosity - why do you need this? The only reason that comes to my mind is to prevent programmers error, because malicious code may access these methods using reflection anyway.
Related
This question already has answers here:
Understanding the purpose of Abstract Classes in Java
(8 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I understand basics of abstraction but still I have some uncertainties.
We cannot create obj of abstract class.
Abstract methods are declared in abstract class but defined in child class.
And we call abstract methods using obj of child class.
I don't get why we using abstract classes if we can do all the things through objects of the child class. We defined abstract methods in child class so it also means we declared it in child class too and we calling that method so it working like normal classes and child.
What is practical purpose of abstract classes?
from what I can see from your question you look at abstract classes as interfaces, but you could and should use them differently.
In an abstract class there could be not abstract methods, meaning a method where all of the child classes use the super class method, this way you can avoid duplicate code.
You can take a look here for an explenation : https://softwareengineering.stackexchange.com/questions/106601/in-simple-words-what-are-are-the-purposes-of-abstract-classes-and-or-interfaces
This question already has answers here:
Is it possible to hide or lower access to Inherited Methods in Java?
(8 answers)
Closed 5 years ago.
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.
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.
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.
This question already has answers here:
What is the difference between public, protected, package-private and private in Java?
(30 answers)
Closed 7 years ago.
I am learning Java and there's something bothering me and the textbook doesn't explain it.
I understand that you use modifiers to declare methods inside classes and all. But I suddenly got to a class declared like
static void(){
}
Why is there no public or private modifier and it still works? Can I avoid using the public modifier everywhere else or how does that work? I understand that static means member of the class and void that it doesn't return a value. Yet why not public or private for that matter.
For the sake of this explanation, the terms "functions" and "methods" are used interchangably. There is a small difference between them, for more information, ask Google.
Methods in Java that do not explicitly specify a modifier are by default package-private, so the method is visible to all the classes in the same package as the class where the method is declared.
Public functions are callable by all classes that have access to the class (i.e your whole project) and private methods are only callable within the class the method was written in. There is also the protected modifier, which specifies that the functions can only be accessed by the class, all its subclasses and classes in the same package.
"Why is that important?", you may ask. Good question!
You should use modifiers to hide methods/properties from other classes which may (ab)use them or in a bad case could lead to unexpected behaviour (not necessarily technically, but semantically... some methods just need a little more privacy just like we do). So a good place to start is private, which means only the class it is declared in is able to call it. More often than not, you'll need to give other classes access to methods, which is why the package-private, protected and public modifiers exist.
Data encapsulation is an important paradigm in programming, and these modifiers help you achieve just that.
This question already has answers here:
Why use an abstract class without abstract methods?
(11 answers)
Closed 7 years ago.
I am still confused when abstract class doesn't contain any abstract method, what a purpose of it? why don't use regular class rather than abstract class if it doesn't contain any abstract method ? In fact, I was saw this situation is applied on java and libgdx library or perhaps for every library.
So, because this situation, I was thinking is it very important to know why use abstract class without abstract method rather than regular class.
When you make a class abstract (either with or without abstract methods), you are forcing the users of this class to create concrete sub-classes of it, since they can't instantiate it.
A user of an abstract class must create a concrete derived class.
This can be useful since it allows the author of an abstract class to introduce abstract functions at a later date. The amount of refactoring necessary at that time is then significantly reduced.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What should the accessablity of Fields in a Abstract Class be?
Is it bad practice to use public fields in abstract classes? The reason I'm asking this is because when I inherit from an abstract class I cannot access a private field from the subclass, the only way(s) around this as far as I can tell is to either make the field public, or create get/set methods to access the field... Which practice is best?
there are always protected fields
Best practice is to use getters and setters, possibly protected or public.
If using getters and setters seems like over kill because all your implementations will be developed with the abstract class itself in the same package or module, you can use protected fields. I don't think its best practice but a pragmatic choice.
In your case a protected member is propably the better choice as it will allow access to the member from within the class or any derived class.
As seen in this article on Member function visibility in Java programs.
Article, includes a nice table for different Java accessors and when/why to use them.