Govt Exams
Interfaces cannot be instantiated directly. You must create a class that implements the interface. Attempting this causes a compilation error.
Set interface ensures uniqueness and doesn't allow duplicate elements. HashSet, TreeSet are implementations of Set.
The 'super' keyword is used to refer to the immediate parent class object. It's used to access parent class methods and constructors.
Abstract classes can contain both abstract methods (without implementation) and concrete methods (with implementation). They cannot be instantiated directly.
class Parent {
void show() {
System.out.println("Parent");
}
}
class Child extends Parent {
void show() {
System.out.println("Child");
}
}
parent obj = new Child();
obj.show();
This demonstrates method overriding and polymorphism. The actual object type is Child, so Child's show() method is called.
Default access (no modifier) allows access only within the same package. Protected allows same package and subclasses.
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.
The four pillars of OOP are Abstraction, Encapsulation, Inheritance, and Polymorphism. Compilation is a process, not a pillar of OOP.
All three declarations are syntactically valid in Java and represent the same 2D array structure. The position of brackets doesn't matter for 2D array declaration - they all create a 3x4 integer array.
String s = "Java";
s = s.concat(" Programming");
System.out.println(s.length());
"Java" has 4 characters. After concatenation with " Programming" (12 characters including space), the total length is 4 + 13 = 17 characters.