Matthew Brown wrote:Hi Cyril. Welcome to the Ranch!
You can't override a private method. So, have you tried changing the default version to public or protected? I think you'll see different behaviour then.
Matthew Brown wrote:The problem is that when you add a method to a constant specific body, you're really creating an anonymous inner class. And because you have no way of referring to that class from outside (because it's anonymous), you have no way of accessing the method unless the method is declared in the original class.
So if you declare the method in a way that is overrideable, all is fine. If you make that method private, you've hidden the constant-specific method in a way you can never recover.
Pritish Chakraborty wrote:why at runtime is GREEN not redirected to its own method
Helen Ma wrote:In case1, the GREEN refers to an annoymous, Color's subtype class. GREEN overrides the public method.
Helen Ma wrote:In case2, the GREEN class does not override the private method in Color because private method cannot be overriden. GREEN has it own public method.
However, in the main method, GREEN.method() actually refers to the private method because GREEN is a Color and GREEN can "see" the private method.
Roel De Nijs wrote:...and private methods are not inherited
Helen Ma wrote:..and GREEN can "see" the private method.
Thus private method of inner class Color is visible throughout class Main.
Cyril Sadasivan wrote:An inner class extending an outer class inherits private members of the outer class. And that's the very basis of the code in this thread. However they cannot be overriden,
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |