Class Level Modifers part 1

Class Level Modifers part 1

                 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.
we can specify this information by using the appropriate modifier.

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.

eg-:
      private class Test{
             public static void main(String... args){
                    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. 
               reason-:if a class contains at least one abstract method then implementation is not complete                               and  hence it is not recommended to create an object to restrict object instantiation                                  compulsory we should declare class as abstract.
  • 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.
Previous Post Next Post

Most Recent