Govt. Exams
Entrance Exams
The 'extends' keyword creates an inheritance relationship where Child inherits properties and methods from Parent. This is an 'is-a' relationship.
The 'private' access modifier restricts access to only the members of the same class. It provides the highest level of encapsulation.
class A { A() { System.out.println("A"); } }
class B extends A { B() { super(); System.out.println("B"); } }
What will be printed when 'new B()' is executed?
The super() call invokes the parent class constructor first, printing 'A', then the child constructor completes, printing 'B'.
The 'super' keyword is used to refer to parent class methods, constructors, and variables. It allows a child class to access parent class members.
class Parent { void display() { System.out.println("Parent"); } }
class Child extends Parent { void display() { System.out.println("Child"); } }
public class Test { public static void main(String[] args) { Parent p = new Child(); p.display(); } }
This demonstrates runtime polymorphism. Even though p is a Parent reference, it points to a Child object, so the overridden display() method in Child class is executed.
In Java, a class implements an interface. A class extends another class. An interface can extend another interface. This is the correct terminology and relationship structure.
Encapsulation is the bundling of data and methods into a single unit (class) while hiding internal implementation details using access modifiers.
'super' is used to refer to parent class methods, constructors, and variables. It helps in accessing hidden or overridden members of the parent class.
The 'final' keyword when applied to a reference variable makes it immutable, meaning it cannot be reassigned to point to another object.
The 'private' access modifier restricts access to only within the same class. It provides the highest level of encapsulation.