Core Java - OOPs Concepts: final keyword Interview Questions
1. What is the final variable?
- the final variable is used to restrict the user from updating it.
- If we initialize the final variable, we can’t change its value.
- The final variable - which is not assigned to any value can only be assigned through the class constructor.
The java final keyword can be used in many context.
- variable
- method
- class
The java final keyword can’t
- stop value change
- stop method overridding
- stop inheritance
code:
class Bike{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
}
public static void main(String args[]){
Bike obj=new Bike();
obj.run();
}
}//end of class
Output:Compile Time Error
2.What is the final method?
a method in java is defined a final method, we can’t override it
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Output:Compile Time Error
3.What is the final class?
if we make any class final, we can’t inherit it into any of the subclasses
final class Bike{}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
}
}
Output:Compile Time Error
4.What is blank or uninitialized final variable?
-
A final variable that is not initialized at the time of declaration is known as blank final variable.
-
create a variable is initialized at the time of creating object and once initialized may not be changed
-
It can be initialized only in constructor.
public class Student {
int id;
String name;
final String PAN_CARD_NUMBER;//PAN_CARD_NUMBER is not set value,so we must make constuctor and set PAN_CARD_NUMBER value
public Student(String pan_card_number) {//make make constuctor and set PAN_CARD_NUMBER value
PAN_CARD_NUMBER = pan_card_number;
}
public static void main(String[] args) {
Student s = new Student("123456");
System.out.println(s.PAN_CARD_NUMBER);
}
}
123456
5. Can we initialize blank final variable?
Yes. if it is not static, we can initialize it in the constructor. If it is static blank final variable, it can be initialized only in the static block.
if it is not static, we can initialize it in the constructor.
public class Student {
int id;
String name;
final String PAN_CARD_NUMBER;//PAN_CARD_NUMBER is not set value
public Student() {
PAN_CARD_NUMBER = "78910"; //set value
}
public static void main(String[] args) {
Student s = new Student();
System.out.println(s.PAN_CARD_NUMBER);
}
}
78910
If it is static blank final variable, it can be initialized only in the static block.
public class Student {
int id;
String name;
static final String PAN_CARD_NUMBER;
static{
PAN_CARD_NUMBER = "78910";
}
public static void main(String[] args) {
System.out.println(Student.PAN_CARD_NUMBER);
}
}
78910
6.What is final parameter?
If you declare any parameter as final, you cannot change the value of it.
class Bike{
int cube(final int n){
n=n+2;//can't be changed as n is final
n*n*n;
}
public static void main(String args[]){
Bike b=new Bike();
b.cube(5);
}
}
Output: Compile Time Error
7.Can we declare a constructor as final?
- The constructor can never be declared as final because it is never inherited.
- Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final.
- if you try to do so, The compiler will throw an error.
8.Can we declare an interface as final?
- No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition.
- Therefore, there is no sense to make an interface final.
- if you try to do so, the compiler will show an error.
9.What is the difference between the final method and abstract method?
The main difference between the final method and abstract method is that the abstract method cannot be final as we need to override them in the subclass to give its definition
Core Java - OOPs: Polymorphism Interview Questions
10.What is the difference between compile-time polymorphism and runtime polymorphism?
compile-time polymorphism | Runtime polymorphism |
---|---|
In compile-time polymorphism, call to a method is resolved at compile-time. | In runtime polymorphism, call to an overridden method is resolved at runtime. |
It is also known as static binding, early binding, or overloading. | It is also known as dynamic binding, late binding, overriding, or dynamic method dispatch. |
Overloading is a way to achieve compile-time polymorphism in which, we can define multiple methods or constructors with different signatures. | Overriding is a way to achieve runtime polymorphism in which, we can redefine some particular method or variable in the derived class. By using overriding, we can give some specific implementation to the base class properties in the derived class. |
It provides fast execution because the type of an object is determined at compile-time. | It provides slower execution as compare to compile-time because the type of an object is determined at run-time. |
Compile-time polymorphism provides less flexibility because all the things are resolved at compile-time. | Run-time polymorphism provides more flexibility because all the things are resolved at runtime. |