Keyword 'final' in JAVA
The general meaning of the word 'final is given below as in the google definitions.
It's also same in JAVA. We use the keyword 'final' in three situations.
class Circle{
double radius;
public final double pi=3.14159265359;
Circle(double radius){
this.radius=radius;
}
double getArea(){
return radius*radius*pi;
}
}
class TestFinal{
public static void main(String args[]){
Circle c1 = new Circle(7);
/* the final field Circle.pi Cannot be assigned
c1.pi=22/7;
*/
System.out.println("Area of the Circle c1: "+ c1.getArea());
}
}
Output:
Area of the Circle c1: 153.93804002591
Output:
Tell your name p1?
p1: My name is Upeksha
Tell your name s1?
s1: I'm not gonna tell my name :p
This may not be the output we expect from a Student. So in these cases we can make the tellYourName method in Person class a final so that it will not be overridden by any means.
Output:
Tell your name p1?
p1: My name is Upeksha
Tell your name s1?
s1: My name is Upeksha
final class Circle{
double radius;
public final double pi=3.14159265359;
Circle(double radius){
this.radius=radius;
}
double getArea(){
return radius*radius*pi;
}
}
/* the type oval cannot subclass the final class Circle
class oval extends Circle{
}
*/
class oval{
}
The key points you should remember when using final keyword are
final
It's also same in JAVA. We use the keyword 'final' in three situations.
- Variables
- Methods
- Classes
as in the general definition given above we use the keyword final to tell the compiler that variable, method or the class it used is the last of it's series and it cannot be modified, override or inherit afterwords.
Final Variables
the variable use the keyword final to represent constants. so that the value cannot be modified by any other methods.
class Circle{
double radius;
public final double pi=3.14159265359;
Circle(double radius){
this.radius=radius;
}
double getArea(){
return radius*radius*pi;
}
}
class TestFinal{
public static void main(String args[]){
Circle c1 = new Circle(7);
/* the final field Circle.pi Cannot be assigned
c1.pi=22/7;
*/
System.out.println("Area of the Circle c1: "+ c1.getArea());
}
}
Output:
Area of the Circle c1: 153.93804002591
Final Methods
final in methods indicate that a method used in a super class cannot be overridden in any of it sub classes.
In below example the student class extends the Person class which inherit all the methods and variables of the Person class to Student class. but the Student class has overridden the tellYourName method in Person class.
In below example the student class extends the Person class which inherit all the methods and variables of the Person class to Student class. but the Student class has overridden the tellYourName method in Person class.
public class Person {
String name;
int age;
public Person(String name,int age) {
this.name= name;
this.age=age;
}
public String tellYourName(){
return "My name is "+this.name;
}
}
class Student extends Person{
public Student(String name, int age) {
super(name, age);
}
@Override
public String tellYourName(){
return "I'm not gonna tell my name :p";
}
}
class TestFinal{
public static void main(String args[]){
Person p1 = new Person("Upeksha",19);
Student s1 = new Student("Upeksha",19);
System.out.println("Tell your name p1? \np1: "+p1.tellYourName());
System.out.println("Tell your name s1? \ns1: "+s1.tellYourName());
}
}
Output:
Tell your name p1?
p1: My name is Upeksha
Tell your name s1?
s1: I'm not gonna tell my name :p
This may not be the output we expect from a Student. So in these cases we can make the tellYourName method in Person class a final so that it will not be overridden by any means.
public class Person {
String name;
int age;
public Person(String name,int age) {
this.name= name;
this.age=age;
}
public final String tellYourName(){
return "My name is "+this.name;
}
}
class Student extends Person{
public Student(String name, int age) {
super(name, age);
}
/* cannot override the final method in Person
/* cannot override the final method in Person
@Override
public String tellYourName(){
return "I'm not gonna tell my name :p";
}
*/
*/
}
class TestFinal{
public static void main(String args[]){
Person p1 = new Person("Upeksha",19);
Student s1 = new Student("Upeksha",19);
System.out.println("Tell your name p1? \np1: "+p1.tellYourName());
System.out.println("Tell your name s1? \ns1: "+s1.tellYourName());
}
}
Output:
Tell your name p1?
p1: My name is Upeksha
Tell your name s1?
s1: My name is Upeksha
Final Classes
Classes use the keyword final to indicate the compiler that this class cannot be inherited or extended by any other class.
final class Circle{
double radius;
public final double pi=3.14159265359;
Circle(double radius){
this.radius=radius;
}
double getArea(){
return radius*radius*pi;
}
}
/* the type oval cannot subclass the final class Circle
class oval extends Circle{
}
*/
class oval{
}
The key points you should remember when using final keyword are
- The methods declared as private or static are implicitly final.
- All the methods inside a final class are implicitly final.
- Objects made from a final class cannot be modified by its methods which we also call as immutable objects
Comments
Post a Comment