Access Specifiers Interview Questions

Which Access Specifier can be used with a class ?

For an outer class we can only use 'public' and 'default' access specifiers. We can use 'private' access specifier for an inner class.

Can we reduce the accessibility of an inherited or overridden method ?

No, we cannot reduce the accessibility of an inherited or overridden method.

What is the difference between public, private, default and protected access specifiers ?
  • Private - The code defined within a 'private' access specifier is not accessible outside the object scope.

  • Public - The code defined within a 'public' access specifier is accessible from anywhere.

  • Default - The code defined within a 'default' access specifier is accessible from anywhere within the same package.

  • Protected - The code defined within a 'default' access specifier is accessible by object and the sub class objects.

Can we reduce the accessibility of the overridden method ?

No, we cannot reduce the accessibility of the overridden method.

What will happen if we make the constructor 'private' ?

We won't be able to create the objects directly by invoking a new operator by making a constructor private.

Can we instantiate the object of derived class if parent constructor is 'protected' ?

No, we cannot instantiate the object of derived class if parent constructor is protected.

Can we declare an abstract method 'private' ?

No. An abstract method can only be declared 'protected' or 'public'.

What is an Access Specifier ?
  • It is used to explicitly mention the way how the data (variables and methods of a class) will be available outside the scope.

  • An access specifier is something which mentions the way how the member of a class will be made available to anything outside the class.

  • An access specifier cannot be used with the local variables(i.e. present inside method/scope).

  • Access specifiers are the keywords using which we can control the accessibility of the members of a class.

Learn more about Access Specifiers in our Video Tutorial