1

While studying OCJP I came across this question of packages which surprises me. I have two simple classes classes Message and New Message each within their own packages A and B respectively as follows:

package A;
public class Message
{
    String getText()
    {
        return "text";
    }
}

And class NewMessage which is in package B and subclass of A:

package B;
public class NewMessage extends A.Message
{
    String getText() {
        return "New Message";
    }

    public static void main(String[] args)
    {
        System.out.println(new NewMessage().getText());
    }
}

And I compile as :

javac -d . Message.java
javac -d . NewMessage.java

and run as:

java B.NewMessage

and it prints

 New Message

By following this stackpost Can a private method in super class be overriden in the sub-class? I tried tagging the method of NewMessage with @Override and compiling it which gave me the following error:

NewMessage.java:4: error: method does not override or implement a method from a supertype
        @Override
        ^

which means that I am not overriding the getText() method. Could someone explain to me what is happening here? Is this case similar to the case of private mehthod override in the post I linked?

Community
  • 1
  • 1
  • did you understand it? – ItamarG3 Dec 20 '16 at 16:30
  • @ItamarGreen what I understand is this behaviour is the same as the behaviour for private method overriding. I know that default visibility means it is only visible and accessible to the default package classes only. If I wanted to override it then I would need to add "protected" modifier to the getText() method in class A. – Mohammed Salman Shaikh Dec 20 '16 at 17:22

3 Answers3

0

I think this question's answer has what you're looking for:

In Java, difference between default, public, protected, and private

The accepted answer references this documentation from oracle:

http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Since the default scope is for package only, your subclass is not allowed to override it or even access it. If you were to remove the getText() method from the NewMessage class and try to compile you can see that the getText method in the Message class is not visible to NewMessage.

I think there is a similarity to the post you linked, but the difference is that access is being given only to the package since no access modifier was specified.

Community
  • 1
  • 1
0

Your method in class Message has default visibility, which means it is only visible within the same package. In consequens it is not visible for Subclasses in other packages.

Since the method is not visible in package B it cannot be overridden my NewMessage.

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
-1

This is happening because of a different reason:

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

You are creating a NewMessage object, which has that method. If you created that object, and it didn't have the method, yet still extended Message, then the output will be 'text'

ItamarG3
  • 4,092
  • 6
  • 31
  • 44