Govt. Exams
Entrance Exams
The 'final' keyword prevents a class from being extended. String, Integer, and other wrapper classes are marked final for security and immutability.
Method overloading requires methods to have the same name but different parameter lists (number, type, or order of parameters). Return type alone is insufficient.
Encapsulation involves bundling data (variables) and methods, hiding implementation details, and providing public methods for access. It protects data integrity.
Interfaces cannot be instantiated directly. You must create a class that implements the interface. Attempting this causes a compilation error.
Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation). They cannot be instantiated directly.
class A {
int x = 10;
}
class B extends A {
int x = 20;
}
public class Test {
public static void main(String[] args) {
A obj = new B();
System.out.println(obj.x);
}
}
Variable overriding doesn't work in Java like method overriding. obj.x accesses the variable from reference type A, which has value 10.