Final Keyword
Final Keyword
final keyword in java is used to restrict the user. The final keyword can be used in many
context.
Final Variables
final variables are nothing but constants. We cannot change the value of a final variable
once it is initialized.
Syntax – final datatype variablename=value;
Example
public class finalvardemo{
public static void main(String args[])
final int x=10;
int y=25;
x++;
y++;
System.out.println(“The value of x and y are”+x+” “+y);
}
}
Output error: cannot assign a value to final variable x
Polymorphism in java
Polymorphism in java is a concept by which we can perform a single action in different ways.
So polymorphism means many forms. There are two types of polymorphism in java compile
time and runtime polymorphism.
Overloading or compile time polymorphism
An example of compile time polymorphism is method overloading i.e same method
name can be used for multiple purposes eg: add method name can be used to add two
numbers, add objects to vectors or add a textbox to a window.
Rules
o Overloaded methods must have different argument list
o They may have different return types if argument list is different
o May have different access modifiers.
Example
class Multiply{
//method with 2 parameter
static int mul(int a,int b){
return a*b;
}
// method with 2 parameters and no return type
static void mul(double a, double b){
System.out.println(a*b);
}
}
public class overloadingdemo{
public static void main(String args[]){
System.out.println(Multiply.mul(5,2);
Multiply.mul(4.0,2.2);
}
}
Output:
10
8.8
Example
class shape{
void draw(){
System.out.println("drawing a shape with size ");
}
}
class circle extends shape{
void draw(){
System.out.println("drawing a circle with radius r");
}
}
class square extends shape{
void draw(){
System.out.println("drawing a square");
}
}
class polygon extends shape{
void draw(){
System.out.println("drawing a polygon ");
}
}
public class drawingdemo{
public static void main(String args[]){
shape s=new shape();
shape c= new circle(); //shape reference but circle object
shape sq=new square();//shape reference but square object
shape p=new polygon();//shape reference but polygon object
s.draw();
c.draw(); //calls draw method of circle
sq.draw();//calls draw method of square
p.draw();//calls draw method of polygon
}
}
Output
drawing a shape with size
drawing a circle with radius r
drawing a square
drawing a polygon
Final methods
If you don’t want a method in parent class to be overridden by its sub classes then we need to
make that method final. A final method cannot be overridden. This means even though a sub
class can call the final method of parent class without any issues but it cannot override it.
Syntax
accessspecifier final returntype methodname(parameter list){ }
example
class Bike{
final void run(){System.out.println("running");}
}
Final class
When a class is declared with final keyword, it is called a final class. The main purpose of using
a class being declared as final is to prevent the class from being subclassed. If a class is marked
as final then no class can inherit any feature from the final class.
You cannot extend a final class. If you try it gives you a compile time error.
Example: Wrapper class and String class
final class XYZ{
void demo(){
System.out.println("My Method");
}
}
class ABC extends XYZ{
void demo(){
System.out.println("My Method");
}
}
public class xyzdemo{
public static void main(String args[]){
ABC obj= new ABC();
obj.demo();
}
}
Output
/xyzdemo.java:6: error: cannot inherit from final XYZ
class ABC extends XYZ{
Assignment
1. What is the use of final keyword?
2. Can we make a variable final in Java? What is different between a normal variable and
final variable?
3. Can we make an array final in Java? Can you change its elements?
4. What is the use of final class in Java?
5. Can you overload a final method in Java? Explain with an example.
6. Can you override a final method in Java?
7. Write a program to add 2 numbers using method overloading.
8. Differentiate between
a. Overloading and overriding
b. Final variable and instance variable
c. Method and final method.
9. Explain the mechanism or technique to restrict inheriting a class properties in java.