Java Interview QuestionsNo Comments

Access Modifiers

1. What is default class modifier?

• A class is called a Default Class is when there is no access modifier specified on a class.

• Default classes are visible inside the same package only.

• Default access is also called Package access.

Example

/* No public before class. So this class has default access*/
class DefaultAccessClass {
//Default access is also called package access					
}

2. What is private access modifier?

a. Private variables and methods can be accessed only in the class they are declared.

b. Private variables and methods from SuperClass are NOT available in SubClass.


3. What is default or package access modifier?

a. Default variables and methods can be accessed in the same package Classes.

b. Default variables and methods from SuperClass are available only to SubClasses in same package.


4. What is protected access modifier?

a. Protected variables and methods can be accessed in the same package Classes.

b. Protected variables and methods from SuperClass are available to SubClass in any package


5. What is public access modifier?

a. Public variables and methods can be accessed from every other Java classes.

b. Public variables and methods from SuperClass are all available directly in the SubClass


6. What access types of variables can be accessed from a Class in Same Package?

package com.membermodifiers.access;

public class TestClassInSamePackage {
    public static void main(String[] args) {
        ExampleClass example = new ExampleClass();
        example.publicVariable = 5;
        example.publicMethod();     
        //privateVariable is not visible								
        //Below Line, uncommented, would give error								              
        //example.privateVariable=5;	//COMPILE ERROR									
        //example.privateMethod();																		
        example.protectedVariable = 5;
        example.protectedMethod();
        example.defaultVariable = 5;
        example.defaultMethod();
    }
}

7. What is the use of a final modifier on a class?

Final class cannot be extended. Example of Final class in Java is the String class. Final is used very rarely as it prevents re-use of the class.Consider the class below which is declared as final.

Final Class examples : String, Integer, Double and other wrapper classes.

8. What is the use of a final modifier on a method?

Final methods cannot be overridden. Consider the class FinalMemberModifiersExample with method finalMethod which is declared as final.

public class FinalMemberModifiersExample {
    final void finalMethod() {}
}

Any SubClass extending above class cannot override the finalMethod().

class SubClass extends FinalMemberModifiersExample {
    //final	method cannot be over-riddent					
    //Below	method, uncommented, causes	compilation	Error					
    /*final	void finalMethod(){														

    }
*/	
}

9. What is a Final variable?

Once initialized, the value of a final variable cannot be changed.

final int finalValue = 5;
//finalValue = 10;	//COMPILER ERROR

Final Variable example : java.lang.Math.PI

10. What is a final argument?

Final arguments value cannot be modified. Consider the example below:

void testMethod(final int finalArgument) {
    //final argument cannot be modified					
    //Below line, uncommented, causes compilation Error					
    //finalArgument = 5; //COMPILER ERROR	
}

11. What happens when a variable is marked as volatile?

• Volatile can only be applied to instance variables.

• A volatile variable is one whose value is always written to and read from “main memory”. Each thread has its own cache in Java. The volatile variable will not be stored on a Thread cache


12. What is a Static Variable?

Static variables and methods are class level variables and methods. There is only one copy of the static variable for the entire Class. Each instance of the Class (object) will NOT have a unique copy of a static variable. Let’s start with a real world example of a Class with static variable and methods.

Static Variable/Method – Example
count variable in Cricketer class is static. The method to get the count value getCount() is also a static method.

public class Cricketer {
    private static int count;

    public Cricketer() {
        count++;
    }

    static int getCount() {
        return count;
    }

    public static void main(String[] args) {

        Cricketer cricketer1 = new Cricketer();
        Cricketer cricketer2 = new Cricketer();
        Cricketer cricketer3 = new Cricketer();
        Cricketer cricketer4 = new Cricketer();

        System.out.println(Cricketer.getCount()); //4					
    }
}

4 instances of the Cricketer class are created. Variable count is incremented with every instance created in the constructor.