+ 1
Access modifiers in Java
I don't understand very good the access modifiers. Who can help me for understand all this types of modifiers? There are only the access modifiers or there are also other types? Thanks.
5 Respostas
+ 1
In methods, at least, the access modifiers entail who and what can call the methods. A "public" method can be called by anyone or anything; in contrast, a "private" method can only be called by the class in which the method belongs.
I hope this helps.
If not, please reply back, and I will go and study them more in depth, and get back to you with a better answer.
+ 1
Thanks for the answer. I understood private and public but I saw that there are more like final, abstract, default, ecc why? when I need to use one instead of the other and for what exactly?
+ 1
"final" is not actually an access modified, last I checked; final just indicates that, whatever value is stored in this variable, cannot be changed once made.
For example,
final int OVER_NINE_THOUSAND = 9001;
Will never change from the value "9001".
"abstract" denotes that what it is describing is a "concept", so to speak. For example, if you had abstract class "Food", you wouldn't actually be able to make objects of "Food", but classes that inherit from "Food", like "Orange" or "Apple" have everything that the "Food" class does.
I will provide a deeper explanation in a later post.
+ 1
Abstract classes and methods are designed to be inherited. You don't actually make objects using an abstract class; think of an abstract class as a kind of interface, of which any and all subclasses have the same variables / methods.
For example, you don't make objects of "Food" (abstract class), but you make objects of "Apple" and "Orange" (actual class), of which extend the "Food" class.
Similarly, abstract methods are supposed to be inherited; it just denotes that any class that has inherited the abstract method has a method with a given name, return, and parameters, that is the abstract method. How they implement the method can vary, which is why there is no body to an abstract method. It is just meant to denote that the class has a method with a specific name, return, and parameters.
+ 1
Before we try abstract classes, first understand that you can make objects using a blueprint we call a class.
If you understand that, abstract classes!!
An abstract class is a class that objects cannot be made from. Abstract classes are typically used as a class from which subclasses can inherit the same variables and methods as the abstract class, allowing users to understand that a particular group of classes all share some of the same methods / variables
For example, you might have an abstract class called "Food". It has variables "age" and "health". Any and all subclasses will inherit these variables. However, you don't actually make items of "Food"; you make items of, say, "Oranges", or "Apples". Any Orange and Apple objects will have variables "age" and "health" in common, as they both inherit it from their superclass, "Food".
Simply put, an abstract class is a reference of what variables and methods any of its subclasses have.