0% found this document useful (0 votes)
26 views

Final

The document discusses the use of the final keyword in Java. It states that final can be used with variables, methods, and classes to make them constant. A final variable's value cannot be changed after initialization. A final method cannot be overridden in a subclass. A final class cannot be extended by another class. Examples are provided to demonstrate making a variable, method, and class final.

Uploaded by

Pinky Sodhi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Final

The document discusses the use of the final keyword in Java. It states that final can be used with variables, methods, and classes to make them constant. A final variable's value cannot be changed after initialization. A final method cannot be overridden in a subclass. A final class cannot be extended by another class. Examples are provided to demonstrate making a variable, method, and class final.

Uploaded by

Pinky Sodhi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

FINAL KEYWORD

final Keyword-Definition
In Java, Final keyword is used as a constant type keyword. Used to create a variable, method, and a class as constant. Can be used with respect to:
Variable Method Class

The element defined as final

cannot be changed later.

final Variable
Is used to make a variable constant. Once a variable is made constant, its value cannot be changed, so a default value is supplied to it initially. Syntax:

final [static/private/protected] <type> <varname> [=Value]; Example: final int x; //initialised with 0 final int y=300;

final Method
Cannot be overridden in any sub class. One method has to be used throughout the inheritance. Syntax:

final <return type> <method name([argument list])> Example: final void add()

Example
class Add { private float x,y; Add() { x=10; y=20; } Add(float a, float b) { x=a; y=b; } final void addition() {
System.out.println(Additio n of Floats: +(x+y)); } } class Plus extends Add { private String x,y; Plus() { x= New; y=Delhi; } Plus(String a, String b) { x=a; y=b; } void addition() { super.addition(); System.out.println(Addition of Strings: +(x+y)); } }

An error will be produced because method addition() is defined as final in class Add, so it cannot be overridden. To remove the error, we can rename the addition() method in sub class.

Example- revised
class Add { private float x,y; Add() { x=10; y=20; } Add(float a, float b) { x=a; y=b; } final void addition() { System.out.println(Addition of Floats: +(x+y)); } } class Plus extends Add { private String x,y; Plus() { x= New; y=Delhi; } Plus(String a, String b) { x=a; y=b; } void adding() { super.addition(); System.out.println(Addition of Strings: +(x+y)); } }

class OvrMain

{
public static void main(String arg[]) { Add ob1= new Add(10, 20); ob1.addition();

Plus ob2=new Plus(Gwalior, City);


ob2.addition(); ob2.adding(); } }

final Class
Cannot be extended in other classes. Can only be used by its objects. Properties of a final class are not inheritable. Works as a stand-alone class. Syntax: final class <class name> Example:

final class Calculate

Example-final class
final class DemoFinal { private int x=5, y=7; DemoFinal() { x=10; y=20; } class MainClass { public static void main(String arg[])

class AB extends DemoFinal { } //Error

{
DemoFinal ob= new DemoFinal(); ob.show(); DemoFinal ob1= new DemoFinal(20, 30);

DemoFinal(int a, int b)
{ x=a; y=b; } } }

ob1.show();

void show()
{ System.out.println(X=+x+Y=+y); } }

You might also like