ExamVeda
Login
Home
11
In Java, can a subclass access protected members (fields and methods) of its superclass?
Discuss
Answer & Solution
Answer: Option D
Solution:
In Java, protected members (fields and methods) of a superclass are accessible to its subclasses.

Unlike private members, which are inaccessible outside the class, protected members can be accessed:

1. Within the same package: Any class within the same package can access protected members.

2. In subclasses (even if in a different package): A subclass can access protected members of its superclass, even if they are in different packages, provided they are accessed through inheritance (i.e., using super or via an instance of the subclass).

This ensures that subclasses get access to important superclass members while still maintaining some level of encapsulation.
12
What is the result of the following code snippet?

class Parent {
protected void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
void show() {
display();
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.show();
}
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
13
In Java, can a subclass have more than one superclass?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
14
What is the purpose of the "final" keyword in Java inheritance?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
15
In Java, can a subclass inherit private constructors from its superclass?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
16
What is the concept of method overriding in Java inheritance?
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
17
In Java, can a subclass inherit static methods from its superclass?
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board
18
What is the result of the following code snippet?

class Parent {
void display() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display() {
super.display();
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
19
In Java, can a subclass access package-private members (fields and methods) of its superclass defined in another package?
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
20
What is the result of the following code snippet?

class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void display() {
System.out.println(super.x + " " + x);
}
}
public class Main {
public static void main(String[] args) {
Child obj = new Child();
obj.display();
}
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board