Why creators of java allowed this situation? I am sure there must be some reason for it. My below code allows Lion to mischievously run as fast as Cheetah.
public class animal { class carnivores { private final void runAsFastAsCheetah() { System.out.println("Ran as fast as Cheetah"); } } public class Lion extends carnivores { public void runAsFastAsLion() { System.out.println("Ran as fast as Lion."); super.runAsFastAsCheetah(); } } public static void main(String[] args) { animal animal = new animal(); Lion lion = animal.new Lion(); //lion.runAsFastAsCheetah(); //Not allowed but// lion.runAsFastAsLion(); } } EDIT: For those taking Lion and cheetah seriously, I have modified code.
public class foo { class A { private final void myMethod() { System.out.println("in private final myMethod()"); } } public class B extends A { public void myMethod() { System.out.println("in B's myMethod()"); super.myMethod(); } } public static void main(String[] args) { foo foo = new foo(); B b = foo.new B(); b.myMethod(); } }
privateword mean? :)