Final keyword, this keyword, and Super Keyword in java [Full-Explanation with solved examples]

In this article, you will learn how we use the Final, This, and Super keywords in Java /OOP in the very easiest way and with solve Examples..........

Usage of Final, This, and Super Keyword

In this article, you will learn how we use the Final, This, and Super keywords in Java /OOP in the very easiest way and with solve Examples..........

Final keyword, this keyword, and Super Keyword in java [Full-Explanation with solved examples]

1. 'Final' Keyword In Java:

  • What is final?

                            Static very last is likewise a keyword this is used for a category, variable, and technique. Using the keyword “very last” elegance, variable, and techniques are declared as the very last keywords. You can not alter a variable as software once you make software. You should claim the very last variable earlier than the time of declaration. Final variables do now no longer have an area on a reminiscence-like per-example basis. Only one replica is created, and all of the very last variables percentages the identical replica of the very last variable. By the subclass of elegance, the approach can not be declared as very last and can not be overridden. 

  • Final Keyword Characteristics:

                              A very last elegance can not be subclassed. This is achieved for motives of safety and efficiency. Accordingly, most of the popular Java library lessons are very last, for instance, java. lang.System and java. lang.String. All techniques in a very last elegance are implicitly very last.
Final myclassP
{
 Void approach(); ///final
} 

  • Final Keyword Features:

                            A (final keyword) approach cannot be overridden through subclasses. This is used to save you surprising conduct from a subclass changing a technique that can be important to the feature or consistency of the class. 
                            A final variable can most effectively be initialized once, both through an initializer or a task statement. It does now no longer want to be initialized on the declaration factor, which is known as a clean last variable. A clean very last example variable of a category should be without a doubt assigned on the giving up of each constructor of the elegance wherein its miles declared; similarly, a clean very last static variable should be without a doubt assigned in a static initializer of the elegance wherein it's miles declared; otherwise, a compile-time blunder takes place in each case. 

  • Difference Between Static and Final Keyword:


Static Final

  Static is a keyword in java that is used to define the class member that can be used independently of any object of class.

Final keyword in java is used to declare a constant variable that cannot be overridden and a class that cannot be inherited.
Static variables cannot be modified. A final variable can be modified.
A static class object cannot be created. A final class object can be created.
Static block is supported in static keyword. The final block is not supported in the final keyword.


2. 'This'  Keyword in Java:

                  In java, ‘this’ is a reference variable that refers back to the modern object. 

  • Advantages of 'this' Keyword:

           this will be used to refer to the modern elegance example variable. 
          ⇒ this will be used to invoke the modern elegance approach (implicitly) 
          ⇒ this() may be used to invoke the modern elegance constructor. 
          ⇒ this will be exceeded as a controversy withinside the approach call. 
          ⇒ this will be exceeded as an argument withinside the constructor call. 
          ⇒ this will be used to go back to the modern elegance example from the approach. 

Usage of "this" keyword in OOP java - Code To Coding

For Instance:
class Student{  
int rollno;  
String name;  
float fee;  
Student(int rollno,String name,float fee){  
this.rollno=rollno;  
this.name=name;  
this.fee=fee;  
}  
void display(){System.out.println(rollno+" "+name+" "+fee);}  
}  
  
class TestThis2{  
public static void main(String args[]){  
Student s1=new Student(111,"ankit",5000f);  
Student s2=new Student(112,"sumit",6000f);  
s1.display();  
s2.display();  
}}  
Output:
111 ankit 5000
112 sumit 6000

3. 'Super' Keyword:

        The super keyword in Java is a reference variable that is used to refer to the spot discern elegance object. 
  • Advantages of 'super' Keyword:
             ⇒ super may be used to refer to the spot discern elegance example variable. 
             ⇒ super may be used to invoke a spot discern elegance approach. 
             ⇒ super() may be used to invoke the spot discern elegance constructor.

Usage of "super" keyword in OOP java - Code To Coding


For Instance:
class Animal{  
String color="white";  
}  
class Dog extends Animal{  
String color="black";  
void printColor(){  
System.out.println(color);//prints color of Dog class  
System.out.println(super.color);//prints color of Animal class  
}  
}  
class TestSuper1{  
public static void main(String args[]){  
Dog d=new Dog();  
d.printColor();  
}
}
Output
black
white



If You Have any Queries Regarding our Topic Then Contact Us! by clicking or via the Comment icon .....

............💖Thank You for Reading💖............