2

I was working on SCJP6 dumps when I found this confusing exercise:

Given classes defined in two different files:

package packageA;

public class Message {

String getText() { return “text”; }

}

And:

package packageB;

public class XMLMessage extends packageA.Message {

String getText() { return “<msg>text</msg>”;}

public static void main(String[] args) {

System.out.println(new XMLMessage().getText());

}

}

What is the result of executing XMLMessage.main?

A. text

B. Compilation fails.

C. <msg>text</msg>

D. An exception is thrown at runtime.

The answer was: B, but I don't understand why; I think the answer should be C.

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
Tarik
  • 4,961
  • 3
  • 36
  • 67

3 Answers3

2

If the code you posted it's the one that is in the book, the correct answer as you mentioned is C, let me explain why.

Again, assuming you copied the code as it's shown in the book when you do, the following line:

String getText() { return “<msg>text</msg>”;}

Its not overriding the getText() method in packageA.Message class but declaring a new one, that will can be accessed for XMLMessage instances within packageB.

This would be different if the the main method is something like:

 public static void main(String[] args) {

    Message message = new XmlMessage();
    System.out.println(message.getText());
}

In this case there is a compilation error since the Message.getText() methods is not exposed outside the package.

jbarrueta
  • 4,907
  • 2
  • 20
  • 21
  • why its just a warning ? even more, i can run it and it produce text – Tarik Nov 04 '14 at 18:05
  • As long as you declare an XMLMessage instance and access the getText() methods within packageB, (which is the case) the code is valid and the output be "text", something that is very important for you to notice, is what I mentioned before, this is not overriding the packageA.Message.getText(). – jbarrueta Nov 04 '14 at 19:19
  • thanks jbarrueta for all the explanation and i do agree with you. However, i am really wondering why in the dumps book they said B is the correct answer, you think in the real exam (SCJP 6) the answer will be "C" or they may make such errors ? – Tarik Nov 05 '14 at 21:01
  • i just saw that in the chineese version of the dumps, the answer was c :) – Tarik Nov 05 '14 at 21:35
1

A package default method cannot be overridden because it is not visible in another package. In your example, method getText() in class Message is only visible to members of packageA. Method does not override package visible method in Eclipse

Community
  • 1
  • 1
Krish Srinivasan
  • 568
  • 1
  • 6
  • 14
  • thanks for your reply, but i thought that as XMLMessage can't see the method in packageA.Message it can define it like a new method. why its just a warning ? – Tarik Nov 04 '14 at 17:56
  • why its just a warning ? even more, i can run it and it produce text – Tarik Nov 04 '14 at 18:05
  • when i compile and run the code i see no warnings (not in my IDE anyways). jbaruetta is correct - if the code sample had tried to reference the method getText() from Message instead it would have been a compilation error. I failed to see that the code sample referenced getText from XMLMessage() assuming that your prediction of the answer B was correct to begin with :) – Krish Srinivasan Nov 04 '14 at 20:06
1

The method String getText() { return “text”; } is with package (default) scope . And hence it is not visible outside the package packageA .

so it is not possible to override the method in the class XMLMessage which is outside the packageA .

You can learn the basics of method overloading and overriding here

Santhosh
  • 8,181
  • 4
  • 29
  • 56
  • thanks for your reply, but i thought that as XMLMessage can't see the method in packageA.Message it can define it like a new method. why its just a warning ? even more, i can run it and it produce text – Tarik Nov 04 '14 at 18:02