What is modifiers?
- Java access modifiers are used to specify the scope of variable,constructor,class,methods,data members. it is helps to restrict the and secure the access.
Type:-
- Access Modifier.
- Non-Access Modifiers
Access Modifier:-
- default − Visible to the same package. No modifiers are needed.
- private − Visible to the class only.
- public − Visible to the world.
- protected − Visible to the same package and all sub classes.
Access Modifiers:-
- for classes, you can use either public (or) default main class you can use and inner class you can use all access modifier).
- for attributes(fields),methods,constructors for public,private,default ,protected.
Non-Access Modifiers:-
- for class you can use final or abstract
- In final have class can not be inherited(extend) by other classes.
- abstract - The class can not be used to create object (To access an abstract class ,it must be inherited (extend) from another class, you must implement abstract method from parent to child class other wise child class can not create object and this child class also abstract class)
for attributes(fields) and method :
- final method/fileds can not be overridden.
static method can be overloaded and can not be overridden.
In fields can be static,final and access modifier ,final in fields(global variable) once initialize the variable can not be reinitialize.
construct you can use access modifier and final only you can use and can not to be static and abstract.parameter not allowed access modifier and only final only.
Abstract class only to use to abstract method,the method does not have body. Abstract method be overridden.
Abstract method can not to be static ,main purpose abstract method created object from inherited class(child class). static means no object needed.you can define static method in abstract class.
-transient,synchronized,volatile
Package in java :-
- A package is a folder that group related classes together.
- why use packages?
- To organize code,To avoid name conflicts,To reuse classes easily.