Question: QUESTION 9 Consider the following Java code. class MyTest { public static void main(String[] args) { SubClass s = new SubClass(); s.superClassField = 20; System.out.println(s.toString());
QUESTION 9
- Consider the following Java code. class MyTest { public static void main(String[] args) { SubClass s = new SubClass(); s.superClassField = 20; System.out.println(s.toString()); } } class SuperClass{ int superClassField = 10; public String toString(){ return "Superclass field is " + superClassField; } } class SubClass extends SuperClass{ public String toString(){ return "Subclass field is " + super.superClassField; } } Which of the following statements are correct?
- Given the object s, any other class could change the value of superClassField.
- Making superClassField to be private would improve encapsulation, and the code would compile.
- Making superClassField to be private would improve encapsulation, but the code will not compile.
- Making superClassField to be protected would improve encapsulation, and the code would compile.
- Without any modification the code output is Subclass field is 20.
- Without any modification the code will not compile.
| 1, 3, 4, and 5 only. |
| 3, 4, and 5 only. |
| 1, 2, and 5 only. |
| 1, 3, 4 and 6 only. |
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
