Class Modifiers
In this tutorial, we are going to discuss about class modifiers in Java. In object-oriented programming languages like Java, class modifiers are keywords that modify the declaration of a class, providing additional information about its behavior or visibility.
- When ever we are writing our own java class compulsory we have to provide some information about our class to the JVM like,
- Whether our class can be accessible from any where or not
- Whether child class creation is possible for our class or not.
- Whether instantiation is possible or not etc
- We can specify this information by declaring with appropriate modifier.
- The only applicable modifiers for top level classes are
- public
- <default>
- final
- abstract
- strictfp
- If we are using any other modifier we will get compile time error saying “Modifier XXXX not allowed here”.
private class test {
public static void main(String[] args) {
int x = 0;
for(int y = 0; y<3; y++) {
x = x + y;
}
System.out.println(x);
}
}
C.E: Modifier private not allowed here
- But for the inner classes the following modifiers are allowed
- public
- <default>
- final
- abstract
- strictfp
- private
- protected
- static
Access specifiers Vs Access Modifiers
- In old languages like C and C++ public, private, protected and default are considered as access specifiers and all the remaining like final, static are considered as access modifiers
- But in java there in no such type of division all are considered as access modifiers
public classes
If a class declared as the public then we can access that class from any where
package pack1;
public class A {
public void m1() {
System.out.println("Hai this is Ashok");
}
}
package pack2;
import pack1.A;
class B {
public static void main() {
A a = new A();
a.m1();
}
}
If we are not declaring class A as public then we will get compile time error while compiling B class, saying pack1.A is not public in pack1 can’t be accessed from outside package.
default classes
- If a class declared as default then we can access that class only with in that current package i.e., from outside of the package we can’t access..
- If you don’t use any modifier, it is treated as default by default.
package pack1;
class A{
void msg(){
System.out.println("Hello This is Ashok");
}
}
package pack2;
import pack1.A;
class B{
public static void main(String args[]){
A obj = new A();//C.E
obj.msg();//C.E
}
}
Final Modifier
Final is the modifier applicable for classes, methods and variables
Final Method
- Whatever methods parent has has by default available to the child through inheritance. If the child not satisfied with parent method implementation then child is allowed to redefine that method based on it’s requirement. This process is called overriding.
- If the parent class method is declared as final then we cannot override that method in the child class because it’s implementation is final.
class P {
public void property() {
System.out.println("Money + Gold + Land");
}
public final void marry() {
System.out.println("Alekya");
}
}
class C extends P {
public void marry() {
System.out.println("Manasa");
}
}
C.E: marry() in C cannot override marry() in P; Overrider method is final.
Final Class
If a class declared as final we cannot extend functionality of that class i.e., we can’t create child class for that class. i.e., inheritance is not possible for final classes.
final class P {
}
class C extends P {
}
C.E: cannot inherit from final P
Note
Every method present inside a final class is always final by default. But every variable present inside final class need not be final.
final class P {
static int x = 10;
public static void main(String[] args) {
x = 100;
System.out.println(x); // 100
}
}
- The main advantage of final keyword is we can achieve security and we can provide unique implementation.
- The main disadvantage of 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 it is not recommended to use final keyword.
abstract modifier
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 methods with abstract modifier i.e., abstract methods can have only declaration but not implementation. Hence, every abstract method declaration should compulsory ends with “;”.
public abstract void m1() { } // Error
public abstract void m1(); // Fine
- Child classes are responsible to provide implementation for parent class abstract methods
E.g
abstract class Vechicle {
public abstract int getNoOfWheels();
}
class Bus extends Vehicle {
public int getNoOfWheels() {
return 6;
}
}
class Auto extends Vehicle {
public int getNoOfWheels() {
return 3;
}
}
- By declaring abstract methods in parent class we can define guidelines to the child classes which describes the methods those are to be compulsory implemented by child class.
- abstract modifier never talks about implementation, if any modifier talks about implementation then it is always illegal combination with abstract.
- Following are various illegal combinations of modifiers for methods.
abstract + final // Illegal
abstract + static // Illegal
abstract + synchronized // Illegal
abstract + native // Illegal
abstract + strictfp // Illegal
abstract + private // Illegal
abstract class
- For any java class if we don’t want instantiation then we have to declare that class as abstract i.e., for abstract classes instantiation (Creation of object) not possible.
abstract class Test {
}
Test t = new test(); C.E: Test is abstract; cannot be instantiated
abstract class Vs abstract method
If a class contains at least one abstract method then compulsory we should declare class as abstract otherwise we will get compile time error.
Reason
- Â If a class contains at least one abstract method then implementation is not complete and hence it is not recommended to create object to restrict object instantiation compulsory we should declare class as abstract.
- Even though class doesn’t contains any abstract method still we can declare class as abstract if we don’t want instantiation i.e., abstract class can contains zero number of abstract methods also.
E.g
HttpServlet class is abstract but it doesn’t contains any abstract methods
- If we are extending abstract class then for each and every abstract method of parent class we should provide implementation otherwise we have to declare child class as abstract.
final Vs abstract
- abstract methods compulsory we should override in child classes to provide implementation. Where as we can’t override final methods. Here final, abstract combination is illegal combination for methods.
- For final classes we can’t create child class where as for abstract classes we should create child class to provide implementation. Hence final, abstract combination is illegal for classes.
- abstract class can contain final method where as final class can’t.
abstract class Test {
public final void m1() {
}
} // Fine
final class Test {
public abstract void m1() {
}
} // Error
Note
It is highly recommend to use abstract modifier because it promotes several OOP features like inheritance and polymorphism.
strictfp modifier
- Introduced in 1.2 version
- We can use strictfp for classes and methods but not for variables
- Usually the result of floating point arithmetic is varied from platform if we want platform independent results for floating point arithmetic then we should go for strictfp modifier
strictfp method
- If a method declared as strictfp all floating point calculations in that method has to follow IEEE754 standard so that we will get platform independent results.
- abstract modifier never talks about implementation. Where as strictfp method always talks about implementation. Hence abstract strictfp combination
strictfp class
- If a class declared as strictfp then every floating point calculation present in every concrete method has to follow IEEE 754 standard so that we will get platform independent results.
- We can declare abstract strictfp combination for classes i.e., abstract strictfp combination is legal for classes but illegal for methods.
That’s all about Class Modifiers in Java. If you have any queries or feedback, please write us at contact@waytoeasylearn.com. Enjoy learning, Enjoy Java.!!