Private Inward Java: Why Should Y'all Ever Continue Fields In Addition To Methods Private?
Making members private inward Java is ane of best coding practice. Private members (both fields in addition to methods) are only accessible within the class they are declared or within inner classes. private keyword is ane of iv access modifier provided past times Java in addition to its a most restrictive with all iv e.g. public, default(package), protected in addition to private. Though in that place is no access modifier called package, rather its a default access grade provided past times Java. In this Java tutorial nosotros volition come across why should nosotros ever brand members of shape past times default every bit private in addition to answer to one of popular Java interview question can override private methods inward Java.
This article is inward continuation of my before postal service on Java e.g. 10 Object Oriented Design principles Java programmer should know in addition to 10 best practise to follow spell writing comments. If y'all haven’t read them already y'all may detect them interesting in addition to worth reading.
Why should y'all brand your members private past times default:
concept of Encapsulation inward Java. Here are or then benefits of making variables or methods private in Java:
1) private methods are good encapsulated inward shape in addition to developer knows that they are non used anywhere else inward code which gives them confident to change, modify or heighten private method without whatever side-effect.
2) By looking to the private method y'all know past times certain that no ane is using it which is bully assistance during debugging coffee program.
3) IDE similar Netbeans in addition to Eclipse utilisation this information in addition to automatically cheque whether private methods are used within Class or non in addition to tin sack display relevant warning to improve code quality or delete unused statements, variables or methods.
4) private methods utilisation static binding inward Java in addition to they are bonded during compile fourth dimension which is fast compared to dynamic binding which occurs during runtime in addition to likewise gives risk to JVM to either inline the method or optimize it.
5) Making variables soul inward coffee in addition to providing getter in addition to setter for them makes your shape compatible Java edible bean naming convention in addition to other reflection based frameworks similar displaytag .
Can nosotros override Private method inward Java
No y'all cannot override private methods inward Java, private methods are non virtual in addition to accessed differently than non-private methods. Since method overriding can alone live done on Sub Class in addition to since private method is non accessible inward sub shape or derived class, You only tin sack non override them. Another possibility of overriding private methods inward inner shape by extending outer class, since private methods are accessible within inner class. This volition likewise non piece of employment because private methods are bonded during compile fourth dimension in addition to alone Type (or Class) which is used to locate a private method. For event inward below code where it looks similar Inner SubClass is overriding private method , if y'all telephone phone privateMethod() with a type of Super Class but object of Sub Class, it volition alone execute privateMethod() declared inward super Class.
Should I ever brand private method every bit lastly inward Java
Using lastly in addition to private inward Java with method is a expert pick but I don't think it offering whatever do goodness inward damage of performace or not. Rather this conclusion should live taken on the the world of design, functionality in addition to ensuring readability of code. Since making a method lastly only bound in that place mightiness to live overridden, it doesn't brand much feel to score a private method every bit lastly inward java, because private method tin sack non overridden inward Java past times whatever means.
Important Points close private keyword inward Java
Few important points to scream back close private keyword inward coffee before seeing event of private keyword in addition to overriding soul methods inward Java which is non possible.
1.private keyword tin sack live applied to fields, methods in addition to inner shape inward Java.
3.Though private methods or variables are non accessible exterior of Class, they tin sack live accessed via reflection past times using setAccessible(true) in addition to changing in that place private visibility.
4.Private method tin sack non live overridden inward Java, non fifty-fifty within inner classes.
5.private members allows Compiler in addition to JVM to optimize it for amend performance.
Example of soul plain in addition to method inward Java
Here is a code event or private keyword inward Java. This examples shows that private methods uses static binding at compile fourth dimension in addition to tin sack non live overridden. Don’t confuse with the output though regarding private variable because its accessible within Inner classes. It volition live compiler fault if private variables used exterior the class.
public class PrivateMemberExample {
private String i_m_private = "I am soul member, non accessible exterior this Class";
private void privateMethod() {
System.out.println("Private method of Outer Class");
}
public static void main(String args[]) {
PrivateMemberExample outerClass = new PrivateMemberExample();
NestedClass nc = outerClass.new NestedClass();
nc.showPrivate(); //shows that soul method are accessible inward inner class.
outerClass = nc;
nc.privateMethod(); //does non telephone phone soul method from inner shape because
// y'all tin sack non override soul method within inner class.
}
class NestedClass extends PrivateMemberExample {
public void showPrivate() {
System.out.println("Accessing Private members of Outer class: " + i_m_private);
privateMethod();
}
private void privateMethod() {
System.out.println("Private method of Nested Class");
}
}
}
Output:
Accessing Private members of Outer class: I am private member, non accessible exterior this Class
Private method of Outer Class
Private method of Outer Class
private String i_m_private = "I am soul member, non accessible exterior this Class";
private void privateMethod() {
System.out.println("Private method of Outer Class");
}
public static void main(String args[]) {
PrivateMemberExample outerClass = new PrivateMemberExample();
NestedClass nc = outerClass.new NestedClass();
nc.showPrivate(); //shows that soul method are accessible inward inner class.
outerClass = nc;
nc.privateMethod(); //does non telephone phone soul method from inner shape because
// y'all tin sack non override soul method within inner class.
}
class NestedClass extends PrivateMemberExample {
public void showPrivate() {
System.out.println("Accessing Private members of Outer class: " + i_m_private);
privateMethod();
}
private void privateMethod() {
System.out.println("Private method of Nested Class");
}
}
}
Output:
Accessing Private members of Outer class: I am private member, non accessible exterior this Class
Private method of Outer Class
Private method of Outer Class
In Summary private keyword inward java allows most restrictive access to variables in addition to methods in addition to offering strongest shape of Encapsulation. private members are non accessible exterior the shape in addition to private method tin sack non live overridden.
Further Learning
Complete Java Masterclass
10 JVM options Java programmer should know
0 Response to "Private Inward Java: Why Should Y'all Ever Continue Fields In Addition To Methods Private?"
Post a Comment