Pregunta

In the following code, the constructor of Child has reduced visibility from public to private, which is allowed. The inherited methods, such as test(), cannot have reduced visibility. Why does Java operate this way?

class Parent { 
        public Parent(){}

       public void test()  
        {  
            System.out.print("parent test executed!");  
        }
}

class Child extends Parent{  

    private Child(){}

    private void test(){
        System.out.print("child test executed!"); 
    }

    }  
¿Fue útil?

Solución

Constructors are not inherited, so Child() doesn't override Parent().

As for the methods, if you have (if Child() were public)

Parent p = new Child();
p.test();

Had it been allowed, this would be invoking a private method. So narrowing the access while overriding is not permitted.

Otros consejos

When extending a class you are stating that your class is an extension of the parent class ("IS-A" relationship). What this means is that your class will have all methods of your parent class. This is the same as implementing an interface in java except you gain the method definitions (and fields) from your parent and not just methods declared in the interface. In interfaces constructors are not present because they are not methods. Constructors are special as they belong entirely to the class they are declared on. They declare how to construct only themselves.

In order to construct an object you must know that objects class.

    class A {
        private message;
        private A() {
            message = "You created an A";
        }

        public A(String message) {
            this.message = message; 
        }

        public void printMessage() {
            System.out.println(message);
        }

        public static A createAnA() {
            return new A();
        }
    }

    class B extends A {
        public B() {
            super("You created a B");
        }
    }

A anA = new B();  // This will work
A anotherA = new A(); // This is not allowed as the constructor is private
A yetAnotherA = A.createAnA(); // This works too

So when we constructed B we can say that it is an A. Even though the constructor A is private this is due the constructor not being a part of the interface. The only thing we are saying about B when we assign it to a field of type A is that it has the methods of declared in A. In this case printMessage and createAnA.

That is why you can make the constructor private without changing the definition of the class. Now, why are you not allowed to make the method private when overriding a parents signature. This comes to having varying definitions of the [class].method(). Let's say that you could make your method private. Let's say that you declared a printMssage in the B class. Your reasoning is that you want that method for your use only inside the method and you want your parents printMessage to be used when called externally. Now, you wrote a method like this in the B class.

public void adjustMessage(String additional) {
    message = getMessage() + additional();
}

Which version of get message would be executed? Your private one or the public one of your parents? The Java dispatcher would of course choose the public one as it is the one declared in the interface. So we we look at this example we can see if you did make your method have different a lower privilege your method could never be dispatched too which would just make things confusing for the reader.

This is a very good question.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top