Java interview questions with answers
Home Java Interview Questions Revision Notes Resources Interview Tips Interview Samples

Valid HTML 4.01 Transitional

Java Interview Questions & Answers
Part 6 - Access Modifiers

What is the purpose of declaring a variable as final?
A final variable's value can't be changed. final variables should be initialized before using them.

Can a final variable be declared inside a method?
No. Local variables cannot be declared as final.

What is the impact of declaring a method as final?
A method declared as final can't be overridden. A sub-class doesn't have the independence to provide different implementation to the method.

I don't want my class to be inherited by any other class. What should i do?
You should declared your class as final. A class declared as final can't be inherited by any other class.

When will you declare a class as final?
When a class is independent and completely concrete in nature, then the class has to be marked as final.

Can you give few examples of final classes defined in Java API?
java.lang.String,java.lang.Math are final classes.

How to define a constant variable in Java?
The variable should be declared as static and final. So only one copy of the variable exists for all instances of the class and the value can't be changed also.
static final int PI = 3.14; is an example for constant.

Can a class be declared as static?
No a class cannot be defined as static. Only a method,a variable or a block of code can be declared as static.

When will you define a method as static?
When a method needs to be accessed even before the creation of the object of the class then we should declare the method as static.

I want to print "Hello" even before main is executed. How will you acheive that?
Print the statement inside a static block of code. Static blocks get executed when the class gets loaded into the memory and even before the creation of an object. Hence it will be executed before the main method.

What is the use of a static code block?
static code blocks could be used for one time initialisation activities.

Cant you use the constructor for initialisation rather than static block?
Constructors are used for object level initialisation whereas the static block are used for class level initialisation ie to initialise constants.

Will the static block be executed for each object?
No. It will be executed only once for each class ie at the time of loading a class.

What are the restriction imposed on a static method or a static block of code?
A static method should not refer to instance variables without creating an instance.
It cannot use "this" or "super". A static method can acces only static variables or static methods.

When overriding a static method, can it be converted to a non-static method?
No. It should be static only.

What is the importance of static variable?
static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.

Can we declare a static variable inside a method?
Static varaibles are class level variables and they can't be declared inside a method. If declared, the class will not compile.

What is an Abstract Class and what is it's purpose?
A Class which doesn't provide complete implementation is defined as an abstract class. Abstract classes enforce abstraction.

What is the use of a abstract variable?
Variables can't be declared as abstract. only classes and methods can be declared as abstract.

Can a abstract class be declared final?
Not possible. An abstract class without being inherited is of no use and a final class cannot be inherited. So if a abstract class is declared as final, it will result in compile time error.

What is an abstract method?
An abstract method is a method whose implementation is deferred to a subclass.

Can a abstract class be defined without any abstract methods?
Yes it's possible. This is basically to avoid instance creation of the class.

What happens if a subclass has inherited a abstract class but has not provided implementation for all the abstract methods of the super class?
Then the subclass also becomes abstract. This will be enforced by the compiler.

What happens if a class has implemented an interface but has not provided implementation for a method in a interface?
Its the same as the earlier answer. The class has to be marked as abstract. This will be enforced by the compiler.

Can you create an object of an abstract class?
Not possible. Abstract classes are not concrete and hence can't be instantiated. If you try instantiating, you will get compilation error.

Can i create a reference for a an abstract class?
Yes. You can create a reference for an abstract class only when the object being has provided the implementation for the abstract class - it means that the object should be of a concrete subclass of the abstract class. This applies to interfaces also. Below is an example for the same:
java.sql.Connection con = java.sql.DriverManager.getConnection("");

Can a class be marked as native?
No. Only methods can be marked as native.

What is the use of native methods?
When a java method accesses native library written in someother programming language then the method has to be marked as native.

What is the disadvantage of native methods?
By using native methods, the java program loses platform independence - the accessed platform might be tightly coupled with a operating system hence java program also loses OS independence.

What is the purpose of transient modifier?
Only variables can be marked as transient. Variables marked as transient will not be persisted during object persistence.

What is the purpose of volatile modifier?
Only variables can be marked as volatile. Volatile variables might be modified asynchronously.



Pls send your questions, suggestions and feedback to j2eeguide@yahoo.com

© Copyright JGuide 2006, All Rights Reserved.