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

Final Keyword

The document explains the use of the final keyword in Java, which restricts modifications to variables, methods, and classes. It covers final variables (constants), final methods (which cannot be overridden), and final classes (which cannot be subclassed). Additionally, it discusses polymorphism, including method overloading and overriding, with examples and rules for each concept.

Uploaded by

Chaya Anu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Final Keyword

The document explains the use of the final keyword in Java, which restricts modifications to variables, methods, and classes. It covers final variables (constants), final methods (which cannot be overridden), and final classes (which cannot be subclassed). Additionally, it discusses polymorphism, including method overloading and overriding, with examples and rules for each concept.

Uploaded by

Chaya Anu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

 Overriding or runtime polymorphism


Method Overriding in Java is a condition when a subclass has the same method as
declared in the parent class. If the same method is defined in both the superclass and the
subclass, then the method of the subclass overrides the method of the superclass. This is
known as method overriding.

Example

Consider a shape class which has a draw method


which is used to draw a shape. The shape has three
types circle, square and hexagon which are
subclasses of shape class. All these subclass
contains the same method draw with the same
signature i.e, access specifier, return type and
parameters.

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

Rules for Method Overriding


 The argument list should be exactly the same as that of the overridden method.
 The return type should be the same or a subtype of the return type declared in the
original overridden method in the superclass.
 The access level cannot be more restrictive than the overridden method's access
level. For example: If the superclass method is declared public then the overridding
method in the sub class cannot be either private or protected.
 Instance methods can be overridden only if they are inherited by the subclass.
 A method declared final cannot be overridden.
 A method declared static cannot be overridden but can be re-declared.
 If a method cannot be inherited, then it cannot be overridden.
 A subclass within the same package as the instance's superclass can override any
superclass method that is not declared private or final.
 A subclass in a different package can only override the non-final methods declared
public or protected.
 An overriding method can throw any uncheck exceptions, regardless of whether the
overridden method throws exceptions or not. However, the overriding method
should not throw checked exceptions that are new or broader than the ones declared
by the overridden method. The overriding method can throw narrower or fewer
exceptions than the overridden method.
 Constructors cannot be overridden.

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");}
}

class Honda extends Bike{


void run(){
System.out.println("running safely with 100kmph");}
}
public class vehidemo{
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Output
/vehidemo.java:6: error: run() in Honda cannot override final method run() in Bike

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.

You might also like