java Program to illustrate the use of final keyword.

in this java tutorial we are learn about final keyword in java.
the final keyword is use to restrict the user in java.in the java final keyword is used in many context.
  1. variable
  2. method
  3. class
  •  if you want to make variable as final, you can not change the value of final variable(it will be constant).
  • if you want to make method as final you can not overload it
  • if you want to make class as a final you can not extend it.

Code :

 

final class person

 

{

 

int id;

String name;

 

public person(int n,String str)

 

{

 

System.out.println("this is constructor of class person");

id=n;

 

name=str;

 

}

 

public final void display()

{

 

System.out.println("your no.:"+id); System.out.println("your name:"+name);

 

}

 

}

class emp02 extends person{

 

float salary;

 

public emp02(int n,String str)

 

{

super(n,str);

 

System.out.println("this constructor is of class emp "); salary=7000.00f;

 

}

public void disp()

 

{

super.display();

 

System.out.println("your salary:"+salary);

}

}

 

class fin

 

{

 

public static void main(String[] args) { final int a=40;

 

emp02 obj=new emp02(121,"Niriksha");

 

obj.disp();

 

System.out.println("the value of a is"+a);

}

 

}

 

Output :-

final keyword


Post a Comment

0 Comments