Can a Private Method in Super Class Be Overridden in the Sub-Class

Override private method in java

You cannot override a private method. It isn't visible if you cast A to B. You can override a protected method, but that isn't what you're doing here (and yes, here if you move your main to A then you would get the other method. I would recommend the @Override annotation when you intend to override,

class A extends B {
@Override
public void don() { // <-- will not compile if don is private in B.
System.out.println("hoho public");
}
}

In this case why didn't compiler provide an error for using t.don() which is private?

The Java Tutorials: Predefined Annotation Types says (in part)

While it is not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with @Override fails to correctly override a method in one of its superclasses, the compiler generates an error.

Private method defined in `Sub` and `Super`, both methods can be invoked on Object of Sub , then why private method said not being inherited?

I think the key difference is in how the Java Language Specification uses the term "inherit". (Note that the JLS is the authoritative documentation, not the Java tutorials.)

JLS 8.2, Class Members says:

Members of a class that are declared private are not inherited by subclasses of that class.

But, when describing the behavior of the new operator, JLS 15.9.4, Run-Time Evaluation of Class Instance Creation Expressions says (emphasis mine):

The new object contains new instances of all the fields declared in the specified class type and all its superclasses.

This means that the superclass fields are not inherited by the subclass, but an instance object of the subclass still contains those fields. The same concept applies to private methods as well.

Although a private method from a superclass can be called on an instance of a subclass, the method is not formally a part of that subclass. The private method still belongs to the superclass.

This works because of the subtyping ("is-a") relationship. An instance of the subtype is an instance of the supertype. The class does not inherit those members into itself, but an instance of the class still contains them.

private final in superclass can be overridden?

Answer F isn't an example of overriding, it's an example of method scope. A method that is declared private in a class isn't visible to subclasses of that class, and so there's no overlap; the subclass doesn't see a method in the superclass that it can override. The subclass, with its own method void foo(int), is completely unaware of the superclass's private void foo(int).

Is it possible to override a private member when subclassing in C# or Java?

This is not silly question but it gives another concept of hiding variable.

Field in Java are only hidden and not actually overridden (that doesn't mean that we'll get a compilet time error while trying this, instead they are not overridden in its true sense). Overriding means the member should be invoked based on the run time type of the object and not based on the declared type. But binding for fields in Java is always static and hence it's based on the declared type of the object reference only. Read more about Static Binding in article - Dynamic Binding vs Static Binding >>

In case of methods, only those methods are overriden which are inherited and hence static methods are also not overridden but hidden only and they follow Static Binding only. private members (methods or fields both) are neither hidden nor overridden. They also follow Static Binding and they can not be accessed directly from any other class (including sub classes) except the class which have them. Remember, Hidden doesn't mean here we can't access the members from the subclass. So, don't confuse with being not accessible (in case of private members - fields or methods) and being hidden.

In ES6, how to override a private method in a super class?

Calling Symbol('_privateMethod') again creates a new, distinct symbol (even if it has the same description). You are creating a different method with a different key, not overwriting the original one.

You will need to use the exact same symbol to define the method in the subclass. You can either get it from Object.getOwnPropertySymbols(A.prototype) or by exporting the _privatMethod symbol as a constant from A.js and importing it in B.js (together with the class A). However, if you want to make the class extensible, I would recommend to simply not use symbols at all.



Related Topics



Leave a reply



Submit