Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday, 20 August 2017

Java Programs Using Final Keyword

Final Keyword in Java With Examples

Java Final keyword Examples

Final keyword in java is used for restriction or it is used to restrict the user. But how, we will learn in given below examples. Here we will see java programs using final keyword.

There are lots of keyword in java such abstract keyword in java, java static keyword, extends keyword, implements keyword, etc. But here we are going to learn only java final keyword.

So let's start.


Final Class in Java With Example

We can use the final keyword with class but if you make any class as a final class then you cannot inherit or extend that class. Let's understand with an example.

final class Parent
{
void show()
{
System.out.println("hi");
}
}
class Child extends Parent
{
void display()
{
System.out.println("how are you");
}
public static void main(String args[])
{
Child c = new Child();
c.show();
c.display();
}
}

It will give you a compile-time error:- cannot inherit from final Parent class Child extends Parent.


Java Final Variable Example

Java final variable, if you make any variable as final then you cannot change the original value of it(because it will be a constant).

class Demo
{
final int age = 50;//final variable
void change()
{
age = 18;
System.out.println(age);
}
public static void main(String args[])
{
Demo d = new Demo();
d.change();
}
}

It will give you an error at compile-time:- cannot assign a value to final variable age  age = 18.

Output: Compile-time error

Blank Final Variable: Blank final variable is a variable which is not initialized at the time of declaration. It also called uninitialized final variable. 

You can initialize blank final variable only in the constructor.

class Demo2
{
final String name;
Demo2()
{
name = "Raghav";
System.out.println(name);
}
public static void main(String args[])
{
Demo2 d = new Demo2();
}
}

Output: Raghav

Static Blank Final Variable: Static blank final variable is a variable which is defined with static and final keyword and not initialized at the time of declaration. But it can be initialized in the static block.

class Demo3
{
static final String name;//static blank final variable
static
{
name = "God";
System.out.println(name);
}
public static void main(String args[])
{
System.out.println("main method");
}
}

First run static block then execute main() method.

Output: God
              main method


Final Method in Java With Example

Java final method is the method which is used with the final keyword and you can't override a final method in java.

class Parent
{
final void show()
{
System.out.println("parent");
}
}
class Child extends Parent
{
void show()
{
System.out.println("child");
}
public static void main(String args[])
{
Child c = new Child();
c.show();
}
}

Output: Compile-time error

In the above example, you cannot override a final method but you can inherit final method in child class.


Java Final Constructor Example

What happens when we use the final keyword with the constructor? It will throw an error at compile-time because we cannot use the final keyword with the constructor.

class Student
{
final Student()
{
System.out.println("final constructor");
}
public static void main(String args[])
{
Student s = new Student();
}
}

Output: Compile-time error


Static Final Method in Java

We can use the static keyword with a final keyword in any method declaration.

class Parent
{
static final void show()
{
System.out.println("hello world");
}
}
class Child extends Parent
{
public static void main(String args[])
{
Parent.show();
}
}

Output: hello world

Java Final Parameter

This is the example of java final parameter, if you make any parameter as the final parameter then you can't  change the value of a final parameter.

class Sample
{
int cube(final int a)
{
a = a+2;//no change because a is final
return *a*a*a;
}
public static void main(String args[])
{
Sample s =
new Sample();
s.cube(4);
}
}

Output: compile time error

Here we have learned, what is final in java and different -different programs in java of java final keyword.

Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate