Skip to main content

Class modifiers


Class Modifiers
• Whenever we are writing our own classes compulsory we have to provide some
information about our class to the JVM. Like
1) Better this class can be accessed from anywhere or not.
2) Better child class creation is possible or not.
3) Whether object creation is possible or not etc.
• We can specify this information by using the corresponding modifiers.
• The only applicable modifiers for Top Level classes are:
1) Public
2) Default
3) Final
4) Abstract
5) Strictfp
If we are using any other modifier we will get a compile-time error.
Example:
         private class Test
            {
                  public static void main(String args[])
                       {
                             int i=0;
                             for(int j=0;j<3;j++)
                                {
                                      i=i+j;
                                }
                                    System.out.println(i);
                        }
           }
OUTPUT:
Compile time error.
D:\Java>javac Test.java
Test.java:1: modifier private not allowed here
private class Test
But For the inner classes, the following modifiers are allowed.
Diagram:
What is the difference between access specifier and access modifier?
In old languages ‘C’ (or) ‘C++’ public, private, protected, default is considered as
access specifiers and all the remaining are considered as access modifiers. 
But in java, there is no such type of division all are considered as access modifiers.
Public Classes:
If a class declared as public then we can access that class from anywhere.
EXAMPLE:
Program1:
    package pack1;
    public class Test
       {
           public void methodOne()
              {
                     System.out.println("test class methodone is executed");
               }
     } 
Compile the above program:
D:\Java>javac -d . Test.java
Program2:
      package pack2;
      import pack1.Test;
        class Test1
             {
                 public static void main(String args[])
                     {
                          Test t=new Test();
                          t.methodOne();
                      }
              }
OUTPUT:
D:\Java>javac -d . Test1.java
D:\Java>java pack2.Test1
Test class methodone is executed.
If class Test is not public then while compiling Test1 class we will get compile time error
saying pack1.Test is not public in pack1; cannot be accessed from outside package.
Default Classes:
If a class declared as the default then we can access that class only within the current
package hence default access is also known as “package level access”.
Example:
Program 1:
          package pack1;
          class Test
              {
                   public void methodOne()
                        {
                               System.out.println("test class methodone is executed"); 
                        }
              }  
Program 2:
package pack1;
           import pack1.Test;
           class Test1
             {
                  public static void main(String args[])
                     {
                         Test t=new Test();
                          t.methodOne();
                      }
              }
OUTPUT:
D:\Java>javac -d . Test.java
D:\Java>javac -d . Test1.java
D:\Java>java pack1.Test1
Test class methodone is executed
Final Modifier:
Final is the modifier applicable for classes, methods and variables.
Final Methods:
• Whatever the method's parent has by default available to the child.
• If the child is not allowed to override any method, that method we have to declare with
final in a parent class. That is final methods cannot be overridden.
Example:
Program 1:
      class Parent
          { 
               public void property()
                  {
                      System.out.println("cash+gold+land");
                  }
              public final void marriage()
                 {
                      System.out.println("subbalakshmi");
                 }
          }
Program 2:
      class child extends Parent
            {
                public void marriage()
                    {
                        System.out.println("Thamanna");
                     }
            }
OUTPUT:
Compile time error.
D:\Java>javac Parent.java
D:\Java>javac child.java
child.java:3: marriage() in child cannot override marriage() in Parent; overridden method is
final
public void marriage(){
Final Class:
If a class declared as the final then we can't create the child class that is an inheritance
the concept is not applicable to final classes.
EXAMPLE:
Program 1:
final class Parent
{
}
Program 2:
class child extends Parent
{
}
OUTPUT:
Compile time error.
D:\Java>javac Parent.java
D:\Java>javac child.java
child.java:1: cannot inherit from final Parent
class Child extends Parent
• Note: Every method present inside a final class is always final by default whether we are
declaring or not. But every variable present inside a final class need not be final.
Example:
     final class parent
        {
             static int x=10;
             static
                {
                   x=999;
                }
          }
• The main advantage of the final keyword is we can achieve security. Whereas the main
the disadvantage is we are missing the key benefits of oops: Polymorsim (because of final
methods), inheritance (because of final classes) hence if there is no specific requirement
never recommended using the final keyboard.
Abstract Modifier:
The abstract is the modifier applicable only for methods and classes but not for variables.
Abstract Methods:
Even though we don’t have implementation still we can declare a method with an abstract
modifier. That is abstract methods have only declaration but not implementation. Hence
abstract method declaration should compulsory ends with a semicolon.
EXAMPLE:
Child classes are responsible to provide an implementation for parent class abstract
methods.
EXAMPLE:
PROGRAM:
The main advantage of abstract methods is, by declaring an abstract method in the parent
class, we can provide guidelines to the child class such that which methods they should
compulsory implement.
• The abstract method never talks about implementation whereas if any modifier talks about
implementation is always an illegal combination.
The following are the various illegal combinations for methods. 
Diagram:
All 6 combinations are illegal.
Abstract class:
For any java class if we are not allowed to create an object such type of class we have to
declare with an abstract modifier that is for abstract class instantiation is not possible.
Example:
         abstract class Test
             {
                public static void main(String args[])
                   {
                       Test t=new Test();
                    }
             }
Output:
Compile time error.
D:\Java>javac Test.java
Test.java:4: Test is abstract; cannot be instantiated
Test t=new Test();
What is the difference between abstract class and abstract method?
If a class contains at least one abstract method then compulsory the corresponding class
should be declared with an abstract modifier. Because implementation is not complete and
hence we can’t create an object of that class.
Even though class doesn’t contain any abstract methods still we can declare the class as
an abstract that is an abstract class that can contain zero no of abstract methods also.
Example1: HttpServlet class is abstract but it doesn’t contain any abstract method.
Example2: Every adapter class is abstract but it doesn’t contain any abstract method.
Example1:
class Parent
{
public void methodOne();
}
Output:
Compile time error.
D:\Java>javac Parent.java
Parent.java:3: missing method body, or declare abstract
public void methodOne();
Example2:
       class Parent
           {
                public abstract void methodOne(){}
           }
Output:
Compile time error.
Parent.java:3: abstract methods cannot have a body
public abstract void methodOne(){}
Example3:
         class Parent
           {
               public abstract void methodOne();
           }
Output:
Compile time error.
D:\Java>javac Parent.java
Parent.java:1: Parent is not abstract and does not override abstract method methadone() in
Parent 
class Parent
• If a class extends an abstract class then compulsory we should provide implementation
for every abstract method of the parent class otherwise, we have to declare child class as
abstract.
Example:
       abstract class Parent
             {
                      public abstract void methodOne();
                      public abstract void methodTwo();
             }
      class child extends Parent
            {
                public void methodOne(){}
            }
Output:
Compile time error.
D:\Java>javac Parent.java
Parent.java:6: the child is not abstract and does not override abstract method methodTwo() in
Parent
class Child extends Parent
       • If we declare the class child as abstract then the code compiles fine but a child of a child is
          responsible to provide implementation for methodTwo().
What is the difference between the final and abstract?
       • For abstract methods compulsory we should override in the child class to provide
         the implementation. Whereas for final methods we can’t override hence abstract final combination is illegal for methods.
      • For abstract classes, we should compulsorily create child class to provide implementation
        whereas for the final class, we can’t create child class. Hence final abstract combination is illegal for classes.
      • A final class cannot contain abstract methods whereas an abstract class can contain the final
method. 
Example:
Note:
Usage of abstract methods, abstract classes and interfaces is always good programming
practice.
Strictfp:
    • Strictfp is the modifier applicable for methods and classes but not for variables.
    • strictfp modifier introduced in 1.2 versions.
    • If a method declares as the Strictfp then all the floating-point calculations in that method
have to follow the IEEE754 standard. So that we will get flat from independent results.
Example:
If a class declares as the Strictfp then every concrete method(which has a body) of that
the class has to follow the IEEE754 standard for floating-point arithmetic.
What is the difference between abstract and strictfp?
Strictfp method talks about implementation whereas abstract method never talks
about implementation hence abstract, strictfp combination is illegal for methods.
But we can declare a class with abstract and strictfp modifier simultaneously. That is
abstract strictfp combination is legal for classes but illegal for methods.
Example:

Comments

Popular posts from this blog

Core Java

Content 1 - Language fundamental   Identifier Reserved words Data types Literals Arrays Types of variables Var arg method Main method Java coding standards 2 -  Declaration and Access Modifiers Java source file structure Class modifiers Member modifiers Interfaces 3 - Exception Handling Introduction Runtime stack mechanism Default exception handling in java Exception hierarchy Customized exception handling by try-catch  Control flow in try-catch Methods to print exception information Try with multiple catch blocks Finally Difference between final, finally, finalize Control flow in try-catch-finally Control flow in nested try-catch-finally Various possible combinations of try-catch-finally throw keyword throws keyword Exception handling keywords summary Various possible compile-time errors in exception handling Customized exceptions Top-10 exceptions  4 - String Concept of String Immutable String String Comparison String Conca