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.
Java supports multiple interface implementation but single class inheritance. A class uses 'implements' keyword for interfaces and 'extends' for classes. This is a fundamental OOP concept.
The String class is declared as 'final' in Java, which prevents inheritance and ensures immutability. Strings are immutable, 'new' creates objects in heap not pool, and StringBuilder is generally better for concatenation.
'private' access modifier restricts the method to be accessible only within the same class, making it ideal for internal helper methods. This follows encapsulation principles.
Method overloading requires methods with the same name but different parameter types or number of parameters. Option A has different return types (not sufficient), C has different access modifiers (not overloading), D has different case names (different methods).