Can We Declare a Class Private in Java

Outer or concrete class can’t be private in java.

For example:

private class PrivateExample{
	public static void main(String args[]){
		System.out.println("this is a private class example");

Output: Compile Time Error
It is because, java follows oops concepts. If you make any class as private, it will not be accessible from another class. So there can’t be inheritance, runtime polymorphism, abstraction etc.

But, nested class can be private. For instance:

class A{
	private class B{
		void msg(){
	public static void main(String args[])
		A.B b=new A().new B();

Output: hello

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation