11. In Java, can a subclass access protected members (fields and methods) of its superclass?
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();
}
}
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();
}
}
13. In Java, can a subclass have more than one superclass?
14. What is the purpose of the "final" keyword in Java inheritance?
15. In Java, can a subclass inherit private constructors from its superclass?
16. What is the concept of method overriding in Java inheritance?
17. In Java, can a subclass inherit static methods from its superclass?
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();
}
}
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();
}
}
19. In Java, can a subclass access package-private members (fields and methods) of its superclass defined in another package?
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();
}
}
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();
}
}