nerdexam
Oracle

1Z0-803 · Question #229

Given: class Base { // insert code here } public class Derived extends Base{ public static void main(String[] args) { Derived obj = new Derived(); obj.setNum(3); System.out.println("Square = " + obj.g

The correct answer is A. private int num;public int getNum() {return num;} D. protected int num;public int getNum() {return num;}public. Not B: illegal combination of modifiers: protected and public not C: setNum method cannot be private. not E: getNum method cannot be private.

Working with Methods and Encapsulation

Question

Given:

class Base { // insert code here } public class Derived extends Base{ public static void main(String[] args) { Derived obj = new Derived(); obj.setNum(3); System.out.println("Square = " + obj.getNum() * obj.getNum()); } } Which two options, when inserted independently inside class Base, ensure that the class is being properly encapsulated and allow the program to execute and print the square of the number?

Options

  • Aprivate int num;public int getNum() {return num;}
  • Bpublic int num;protected public int getNum() {return num;}
  • Cprivate int num;public int getNum() {return num;}private
  • Dprotected int num;public int getNum() {return num;}public
  • Eprotected int num;private int getNum() {return num;}public

How the community answered

(32 responses)
  • A
    81% (26)
  • B
    9% (3)
  • C
    6% (2)
  • E
    3% (1)

Explanation

Not B: illegal combination of modifiers: protected and public not C: setNum method cannot be private. not E: getNum method cannot be private.

Topics

#encapsulation#access modifiers#inheritance#getters

Community Discussion

No community discussion yet for this question.

Full 1Z0-803 Practice