Java- final modifier / Chapter -2/ Access Modifier


final Modifier:-
the final is a modifier applicable for method, classes & variables.


1.final method:-

Whatever method parent class has by default available to the child class through inheritance.
If the child class is not satisfied with the parent method implementation than the child is allowed to redefine that method based on its requirement, this process is called "Overriding".

But, 
If the parent class method declared as final then we can't override that method in child class. Because it's implementation is final.

example:-
class Parent
{
           public void property()
         {   
              System.out.println("parent property");
        }
        public  final void marriage()
         {
                  System.out.println("parent choice girl");
        }

class Child extends Parent
{
    public void marriage()
     { 
         System.out.println("Child choice girl");
    }
}

Compile Time Error: marriage () in Child cannot override marriage () in Parent; Overridden method is final.



2. final class:-


If a class declared as final we can't extend the functionality of that class so that we can not create the child class for that class. That's why inheritance is impossible for final classes.

example:-
final class Parent
{
  
        public  void marriage()
         {
                  System.out.println("parent choice girl");
        }

class Child extends Parent
{
    public void marriage()
     { 
         System.out.println("Child choice girl");
    }
}

Compile Time Error: cannot inherit from final Parent.

Note: - Every method present inside final class is always final by default but every variable present inside final class need not be final.


3. final variables:-
As we know in java there are three types of variables.
  1. instance variable 
  2. static variable
  3. local variable 
If we apply final modifier for variable then they will become 
  1. final instance variable
  2. final static variable
  3. final local variable
Let's understand more about final variables one by one:-

1.final instance variable:-

If an instance variable declared as final then compulsory we have to perform initialization explicitly whether we are using or not & JVM won't provide default values.
 example:-
class Test
{
           final int x;
}
Compile-Time Error:- variable x might not have been initialized.


   2.final static variable:-

If a static variable declared as final then compulsory we have to perform initialization explicitly whether we are using or not & JVM won't provide default values.
example:-

class Test
{
           final static  int x;
}
Compile-Time Error:- variable x might not have been initialized.




   3.final local variable:-

If a local variable declared as final then compulsory we have to perform initialization explicitly.
If we are using not using then it is nor required to perform initialization even though it is final.

example:-

class Test
{
          public static void main(String []  args)
          {
                  final  int x;
                  System.out.println("Hello");
         }
}
The Code compiles fine & Hello will be Printed.


Note: The only applicable modifier for the local variable is final by mistake if we are trying to use any other modifier then we will get compile time error.

example: -



class Test

{

          public static void main(String []  args)

          {

                    public int x;
                    private int x;
                    protected int x;

                    static int x;

                    transient int x;

                    volatile int x;



            }

}

Compile Time Error: Illegal start of expression

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com