Class Level Modifiers
whenever writing our own classes we have to provide some information about our class to the JVM like.
- whether this class can be accessed from anywhere are not.
- whether child class creation or not.
- whether object creation is possible or not.
the only applicable modifiers for top-level classes are-:
public default abstract final strictfp
but for inner classes applicable modifiers are -:
public default abstract final strictfp static private protected
Accesses spacifier vs accesses modifiers.
public protected default and private are considered as specifier accept these remaining are considered as modifiers.
but this rule is applicable only to old languages like C++
but in java, all are considered as modifiers only there is no word like specifier.
but this rule is applicable only to old languages like C++
but in java, all are considered as modifiers only there is no word like specifier.
eg-:
private class Test{
public static void main(String... args){
System.out.print("Hello");
}
}
System.out.print("Hello");
}
}
compile time error-: modifier private not allowed here
Top-Level Modifiers-:
public
public classes-:if a class is declared public, we can access that class from anywhere.
/*
package pack1;
class A{
public static void m(){
System.out.print("Hello");
}
}
command for run= javac -d . A.java
package pack2;
class B{
public static void main(String[] args){
A a=new A();
a.m();
}
}
command for run = javac -d . B.java
compile time error
if class a is not public then while compiling B class we will get compile time error saying pack1.A is not public in pack1 and cannot be accessed from the outside package.
*/
eg-:
package pack1;
public class A{
public static void m(){
System.out.print("Hello");
}
}
command for run= javac -d . A.java
package pack2;
import pack1.A;
class B{
public static void main(String[] args){
A a=new A();
a.m();
}
}
command for run = javac -d . B.java
java pack2.B
output is-: Hello
default
default classes-: if a class is declared as default then we can access that class only within the current package that is from the outside package we can't access hence default access is known as package-level access.
eg-:
package pack1;
class A{
public static void m(){
System.out.print("Hello");
}
}
command for run= javac -d . A.java
package pack2;
import pack1.A;
class B{
public static void main(String[] args){
A a=new A();
a.m();
}
}
command for run = javac -d . B.java
compile time error because A is a default class we can't access outside of the package.
Final
final modifier-:final is a modifier applicable for classes methods and variables.
final method-: whatever method the parent has by default to the available to child through inheritance if the child is not satisfied with the parent method implementation the child is allowed to redefine that method based on its requirement this process is called overriding .
if the parent class method is declared as the final then we can't override that method in the child class because its implementation is final.
eg-:
class P{
public void property(){
System.out.print("Cash land Gold");
}
public final void marry(){
System.out.print("Sobha");
}
}
class A extends P{
public void marry(){
System.out.print("Rita");
}
}
compile time error-: marry() in A can't override marry() in P; overridden method is final.
final class-:if a class is declared as final we can't extend functionality to that class which means we can't create a child class for that class as inheritance is not possible for final classes.
eg-:
final class A{
}
class B extends A{
}
command for run=> javac A.java
javac B.java compile Error->can't inherit from final A class B extends A
Note-:
- every method present inside the final class is always final by default but every variable present inside the final class need not be final.
- the main advantage of the final keyword is we can achieve security and we can provide the unique implementation.
- the main disadvantage of the final keyword is we are missing key benefits of OOPs: inheritance (because of final classes ) and polymorphism (because of final methods) hence if there is no specific requirement then its not recommended to use the final keyword.
eg-:
final class{
static int x=10;
public static void main(String... args){
x=888;
System.out.print(x);
}
}
output=>888;
Abstract
the abstract is the modifier applicable for classes and methods but not for variables
abstract method-:even Though we don't know about implementation still we can declare a method with an abstract modifier that is for abstract method only declaration is available but not implementation hence abstract method declaration should end with a semicolon.
chile class is responsible to provide an implementation for parent class abstract methods.
by declaring an abstract method in the parent class we can provide guidelines to child classes such that which method a compulsory child has to implement.
the abstract method never talks about implementation if any modifier talks about implementation then it forms an illegal combination with the abstract modifier.
syntax-:
public abstract void m(); //valid
public abstract void m(){} // invalid
eg-:
abstract class Vehicle{
public abstract int getNoOfWheel();
}
class Bus extends Vehicle{
public int getNoOfWheels(){
return 6;
}
}
class Bike extends Vehicle{
public int getNoOfWheels(){
return 2;
}
}
the following are various illegal combinations of modifiers for the method with respect to abstract.
eg-:
abstract final void m();
compile time error-: illegal combinations of modifier abstract and final.
abstract class-: for any java class if we are not allowed to create an object (because of partial implementation ) such type of class we have to declare with abstract modifier. that is for abstract classes instantiation is not possible.
abstract class Vs abstract method
- if a class contains at least one abstract method the compulsory we should declear class as abstract otherwise we will get compile time error.
- even though the class doesn't contain any abstract method still we can declare class as abstract if we don't want instantiation that is abstract class can contain a zero number of the abstract method. also.
eg-:
1-: HTTP class servlet class is abstract but it doesn't contain any abstract method.
2-: every adapter class is recommended to declare as abstract but it doesn't contain any abstract method.
eg-:
abstract class Test{
public static void main(String... args){
Test t=new Test();
}
}
compile time erro-: Test is abstract cannot be instantiated
Test t=new Test();
some examples -:
/*
class P
{
public void m();
}
compile time error-: missing body or declare abstract
class P
{
public abstract void m(){}
}
compile time error-: abstract method can't have the body
class P{
public abstract void m();
}
compile time error-: P is not abstract and does not override abstract method m() in P.
abstract class P{
public abstract void m();
}
//is valid
if we are extending the abstract class then for each and every abstract method of the parent class we should provide implementation otherwise we have to declare the child class as abstract. in this case, the next-level child class is responsible to provide the implementation.
eg-:
abstract class P{
public abstract void m();
public abstract void m1();
}
class C extends P{
public void m(){}
}
compile time error-: C is not abstract and does not override abstract m1() in P.
*/
Final Vs Abstract
- abstract method compulsory we should override in child classes to provide implementation whereas we can't override the final method hence final abstract combination is an illegal combination for methods.
- for final classes we can't create a child class whereas for abstract classes we should create a child class to provide implementation hence final abstract combination is illegal for classes.
- abstract class contains the final method whereas the final class can't contain the abstract method.
eg-:
abstract class P{
public final void m1(){
}
}
//valid
final class P{
public abstract void m1();
}
//invalid
Note-:it is highly recommended to use abstract modifiers because its promotes several OOPs features like- Inheritance and Polymorphism.
strictfp(strict floating point)
- introduced in 1.2v .
- we can declare strictfp for classes and methods but not for variables.
- usually, the result of floating point arithmetic is varied from platform to a platform if we want to be platform-independent for floating arithmetic then we should go for strictfp modifier.
strictfp method-:if a method is declared as strictfp all floating point calculation in that method has to follow IEEE 754 standard so that we will get a platform-independent result.
abstract modifier never talks about implementation where as strictfp method always talks about implementation hence abstract strictfp combination is illegal for methods.
eg-:
public abstract strictfp void m();//is invalid
compile time error-: illegal combination of modifier abstract and strictfp
strictfp class-:if a class is declared as strictfp then every floating calculation present in every concrete method has to follow IEEE 754 standards so that we will get the platform-independent result.we can declare abstract strictfp combination for the class abstract strictfp combination is legal for class but illegal for method.
eg-:
abstract strictfp class Test{
}
Member Modifier(method or variable level modifiers)
public members-:if a member is declared as public then we can access that member from anywhere but the corresponding class should be visible that is before checking member visibility we have to check class visibility.
eg-:
package pack1;
class A{
public void m(){
System.out.print("Hello");
}
}
package pack2
import pack1.A;
class B{
public static void main(String... args){
A a=new A();
a.m();
}
}
compile time error-: pack1.A is not public in pack1 cannot access to outside package.
in the above example even though m() is public we can't access from the outside package because a corresponding class is not public that is if both class and method then only we can access outside of the package.
defaults member-:if a member declares as default then we can access that member only within the current package from outside of the package we can't access hence pack default access is also known as package level access.
private members-: if a member is private then we can access members only within the class that is from outside we can't access.
abstract method should be available to the child class to provide implementation whereas private methods are not available to the child classes hence private abstract combination is illegal for methods.
protected members-: if a member is declared protected, then we can access that member anywhere within the current package but only in a child class outside of the package.
we can access protected members within the current package anywhere either by using parent reference or by using child reference
but we can access protected in outside package only in child classes and we should use child references only that is parent references can't be used to access protected members from the outside package.
protected =<default>+child
eg-:
package pack1;
public class A{
protected void m1(){
System.out.print("Hello");
}
}
class B extends A{
public static void main(String... args){
A a=new A();
a.m1(); //valid
B b=new B();
b.m1(); // valid
A aa=new B();
aa.m1(); //valid
}
}
package pack2;
import pacak1.A;
class C extends A{
public static void main(String... args){
A a=new A();
a.m1(); //inValid
C c=new C();
c.m1(); //Valid
A ac=new C();
ac.m1(); //inValid
}
}
we can access protected members from the outside package only in child classes and we should use that child class reference only.
for example-:
from d class if we want to access we should use D class reference only.
package pack1;
public class A{
protected void m1(){
System.out.print("Hello");
}
}
class B extends A{
public static void main(String... args){
A a=new A();
a.m1(); //valid
B b=new B();
b.m1(); // valid
A aa=new B();
aa.m1(); //valid
}
}
package pack2;
class C extends A{
}
class D extends C{
public static void main(String... args){
A a=new A();
a.m1(); //inValid
A ac=new C();
ac.m1(); //inValid
A a1=new D();
a1.m1(); //inValid
C c=new C();
c.m1(); //inValid
C c1=new D();
c1.m1(); //inValid
D d=new D();
d.m1(); //Valid
}
}
compile time error-: m1() has protected access in pack1.A
Summary Table-:
the most restricted accessible modifier is private.
the most accessible modifier is public.
recommended modifier for data members (variable is private) but recommended for methods is public.
private<default<protected<public