Final keyword with Class and Variable

In this blog, we will work on final keyword with class and variable. Final class can’t inheritance with other class and final variable can’t modify after assign value.

Let’s see both example in one blog

 

Final keyword with Class

final class A

{

                // Create some method

}

class B extends A             // Not possible becuase of final A class

{

                // Create some method

}

class inh8

{

                public static void main(String args[])

                {

                                                B b1 = new B();

                }             

}

Output:

Note: This program generates compile time error.

 

About Programs.

In this program, final class can’t inheritance with other class, so when we run this program, program generate compile time errors.

 

Video available on YouTube.


Final keyword Variable

class inh9

{

                public static void main(String args[])

                {

                                final int x = 10;

                                x++;       // Create an error because of final x variable

                }

}

Output:

Note: This program generates compile time error.

 

About Programs.

In this program, variable x is final so when we increment in x value using x++ that time program gives compile time error in programs.

 

Video available on YouTube.


[sbscrbr_form]

If you have any query on blog example, you can directly comment us or subscribe our website


Earn Money Online

1140total visits,3visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

3 responses to “Final keyword with Class and Variable”

  1. This post is really interesting. I have bookmarked it.
    Do you allow guest post on your blog ? I can write high quality articles for you.
    Let me know.

  2. I’m amazed, I have to admit. Rarely do I come across a blog that’s
    both educative and interesting, and let me tell you, you’ve hit the nail on the head.
    The problem is an issue that not enough people are speaking intelligently about.
    I’m very happy that I came across this in my search for something regarding this.