For Instance in beneath code the place it appears like that nested class is an overriding personal methodology, however if you happen to name privateMethod() with a kind of tremendous class however the object of the subclass, it is going to solely execute privateMethod() declared within the guardian class, which isn’t precisely methodology overriding.
Had the personal methodology overridden that it will have known as methodology from youngster class. By the best way, compiler is not going to complain, it is going to deal with methodology with very same signature in youngster class as separate methodology, and this is called methodology hiding in Java.
Ought to You Make Non-public methodology Closing in Java?
Since making a technique remaining, simply restrict it’s capability to be overridden, it would not make a lot sense to mark a non-public methodology as remaining in Java, as a result of personal methodology cannot overridden in Java by any means. So compiler will certainly carry out form of optimization it will possibly e.g. inlining or caching it.
By the best way, personal key phrase is without doubt one of the elementary entry modifier and could be utilized to variables, strategies and sophistication, let’s see a number of the price understanding truth about personal key phrase in Java.
Necessary Factors about personal key phrase in Java
1. You’ll be able to apply personal entry modifier fields, strategies and any internal class in Java. It’s most restricted entry modifier and solely accessible within the class they’re declared. They aren’t seen outdoors the category and accessing them outdoors will end in compile time error.
4.As we now have seen on this article, personal strategies cannot be overridden in Java, not even inside nested or internal courses.
5.Non-public members runs quicker than non-private one, due to static binding. They’re additionally higher candidate for optimization from compiler as a result of they cannot be overridden.
Non-public Methodology Overriding Instance
Although it does present that personal methodology is accessible in Inside class, you simply cannot override them. Let’s see what does output exhibits.
/** * Java Program to reveal, personal methodology cannot be overridden in Java, * not even on Inside courses. Major cause of that habits is as a result of they're bonded * utilizing static binding in Java. */ public class PrivateMemberExample { personal String i_m_private = "I'm personal member, not accessible outdoors this Class"; personal void privateMethod() { System.out.println("Non-public methodology of Outer Class"); } public static void primary(String args[]) { PrivateMemberExample outerClass = new PrivateMemberExample(); NestedClass nc = outerClass.new NestedClass(); nc.showPrivate(); //exhibits that personal methodology are accessible in internal class. outerClass = nc; outerClass.privateMethod(); // This is not going to name personal methodology from internal class, // which exhibits you can't override // personal methodology inside internal class. } class NestedClass extends PrivateMemberExample { public void showPrivate() { System.out.println("Accessing Non-public members of Outer class: " + i_m_private); privateMethod(); } /* * personal methodology making an attempt to be overridden, * as a substitute it’s simply hiding guardian class methodology. */ personal void privateMethod() { System.out.println("Non-public methodology of Nested Class"); } } } Output Accessing Non-public members of Outer class: I'm personal member, not accessible outdoors this Class Non-public methodology of Outer Class Non-public methodology of Outer Class
From the output, it’s clear that each the decision to non-public methodology, which is made through the use of reference variable with the kind of guardian class end in invoking personal methodology from the guardian class, which can also be an outer class in our case.
Had it was overridden, in case of second name, which is made utilizing object of Inside sub class, would end in execution of personal methodology of nested class. This proves that personal methodology cannot be overridden in Java, not in sub class and never even in Inside class.
Associated Java Interview questions from Java67 Weblog
Thanks for studying this text to this point. For those who like an object-oriented programming tutorial then please share it with your pals and colleagues. When you have any questions or suggestions then please drop a observe.