Difference between private and final in Java?

Hello guys, one of the common questions among Java beginners is what is the difference between a private and final keyword in Java? The confusion arises because they are very similar and used in a similar context. For example, you can make a class, method, and variable final as well as private. Both put some kind of restriction like you cannot override a final method in Java and the private method is not even accessible outside the class, so obviously you cannot override it. The main difference between private and final keyword in Java is that private is primarily an access modifier, which controls the visibility of variable, method, and classes in Java application, while final is just a modifier that enforces additional constraints on the field, method, and class in Java.

In this article, I will explain to you the difference between a final and private keyword in detail by examining them how different a private class is from a final class and a private member variable from a final variable in Java. I will also explain to you when to make a method private in Java vs final in Java.

I expect you to know some Java basics to understand what I am talking about, if you are a complete beginner in Java then this information will go over your head and it would be better if you first go through a comprehensive Java course like The Complete Java Masterclass by Tim Buchalaka and his team on Udemy.

This is one of the better courses to learn Java and I highly recommend all Java beginners. A good thing about this course is that it is also most up-to-date and covers important changes made in recent Java releases like Java module system, lambda expression, Stream API, var for local variables and much more.




Difference between Private vs final keyword in Java

Without wasting any more of your time, let's deep dive into private vs final discussion in Java. As I have told the difference is divided into three sections, the first section focuses on private vs final method, the second section discusses the difference between the final class and private class, and the third and final section covers the effect of private variable vs final variable in detail.

1. Difference between the private and final method in Java

A private method is not visible outside the class they are declared, they are virtually final becuase you cannot override them. While final methods are visible outside the class, you can call them outside the class they are declared, depending upon their access modifier, but you can't override them in a subclass.

If you try to create a method with the same signature as the final method in the parent class, the compiler will give an error but you can create the same private method as in parent class, without any compile-time warning.

This is known as method hiding. In this case, a private method from the superclass will be invoked if a call happens from the superclass, and the private method from the subclass will be invoked if a call is made in the subclass. This difference will be more clear by the following example.

- private is an access modifier, while final is a modifier which  puts additional constraints
- you cannot use private methods outside the class, the final method can be used.
- you cannot override both private and final methods.
- you can hide private method but the final method cannot be hidden, depending upon whether it is static or not.

If you want to learn more about the difference between a private vs final method in Java and want to learn when to use the final vs private method then I suggest you go through a Java fundamental course like Java Fundamentals: The Java Language on Pluralsight. It's a great course to learn Java fundamentals like this.

Difference between private and final in Java?


2. Difference between private and final class in Java

As I have said before the fundamental difference between private and final is that former control visibility while later puts additional constraints. You can use both private and final with class but there are certain restrictions.

For example, you cannot make a top-level class private in Java but you can make it final. private classes are always nested inside, top-level classes. The main difference between private and final class is that you can inherit from a private class in the same class but extending the final class is prohibited by the Java compiler.

Though you can not create sub-class of a private class outside of the class they are declared, similar to the final class. Another difference between private and final class is that you cannot use private class outside their enclosed class but you can use the final class depending upon their access modifier like if a final class is public then you can use it everywhere.

- you cannot make top-level class private but can make the final.
- you can inherit from a private class in the enclosed class, but you cannot inherit from final class.
- final can be used with both nested and top-level classes.
- a private class is not visible outside the enclosed class, but the final modifier doesn't put access restriction of class.

If you want to know more about different classes like top-level and inner class, private and final classes then you can also check out this Java In-Depth: Become a Complete Java Engineer! course on Udemy. It's also a good resource for anyone wants to learn Java in depth.



3. Difference between private constructor and final class

The most common way to prevent a class from being sub-classes is making it final, but there is also another way to achieve the same effect, by making the constructor private. The main difference between these two approaches is that private constructor only prevents from inheritance outside the class boundary, but final prevents it both outside and inside of the class boundary.

For example, you can create a subclass of a class with the private constructor in the same class, because the private constructor is visible there, but you cannot extend final classes anywhere.

I first saw the example of a private constructor while implementing a Singleton pattern in Java, which just creates one instance of the class and that is shared globally via getInstance() method.

Making constructor private ensures that no one can create another instance of Singleton in Java. By making a class final doesn't put such constraint, you can create an instance of a final class outside as well.
  1. The private constructor doesn't prevent from extending the class in the same boundary using nested class, the final class cannot be inherited anywhere.
  2. You cannot create an instance of a class with a private constructor outside the class boundary, but you can create an instance of the final class anywhere.
  3. Both private constructor and final class prevent other class to extend by another top-level class.
  4. A private constructor has limited use like in Singleton pattern

And, if you want to learn more about constructor and final variable in Java then there is no better course than The Complete Java Masterclass course on Udemy. I also bought in just $10 and always refer it whenever I need to refresh any core Java concept like this.



That's all about the difference between private and final keyword in Java. As I have said, a private keyword can be applied to a class, method, and variables similarly to a class, method, and variable can also be made final but local variables can only be made final and not private. It's a fundamental concept to know for any Java developer and knowing the difference will help you to decide when to use final vs private keyword in Java.

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!


Other Programming Articles You May Like to Explore

Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or doubt then please drop a note.

P. S. - If you are new to Java Programming world and want to learn Java but looking for a free course then you can also check out this list of 10 Free Java Programming courses for beginners and anyone who wants to learn Java.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.