Java Interview Questions

By in
No comments


What is data encapsulation and what’s its significance?
Encapsulation is a concept in Object Oriented Programming for binding together variables and methods in a single unit.
Encapsulation helps programmers to follow a modular approach for software development as each object has its own set of methods and variables and serves its functions independent of other objects. Encapsulation also serves data hiding purpose.

What is the difference between an Inner Class and a Sub-Class?
An Inner class is a class which is nested within another class. An Inner class has access rights for the class which is nesting it and it can access all variables and methods defined in the outer class.Kh
A sub-class is a class which inherits from another class called super class. Sub-class can access all public and protected methods and fields of its super class.

Can you access non static variable in static context?
No you cannot access static variable in non-static context in Java. Read why you cannot access non-static variable from static method.

What’s the purpose of Static methods and static variables?
When there is a requirement to share a method or a variable between multiple objects of a class instead of creating separate copies for each object, we use static keyword to make a method or variable shared for all objects.

What are the various access specifiers for Java classes?
In Java, access specifiers are the keywords used before a class name which defines the access scope. The types of access specifiers for classes are:
• Default: Method, Field, class can be accessed only from the same package and not from outside of its native package.
• Public : Class, Method, Field is accessible from anywhere.
• Protected: Method, Field can be accessed from the same class to which they belong or from the sub-classes, and from the class of same package, but not from outside.
• Private: Method, Field can be accessed from the same class to which they belong.

What’s the base class in Java from which all classes are derived?

What is a singleton class? Give a practical example of its usage.
Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. The singleton class must provide a global access point to get the instance of the class. A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.
Singleton pattern is used for logging, database driver objects.

Java Singleton Pattern
To implement Singleton pattern, we have different approaches but all of them have following common concepts.
• Private constructor to restrict instantiation of the class from other classes.
• Private static variable of the same class that is the only instance of the class.
• Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.

What are Loops in Java?
Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:
• For Loops
For loops are used in java to execute statements repeatedly for a given number of times.
for (initialization; termination;
increment) {
• While Loops
While loop is used when certain statements need to be executed repeatedly until a condition is fulfilled. In while loops, condition is checked first before execution of statements.
while (expression) {
• Do While Loops
Do While Loop is same as While loop with only difference that condition is checked after execution of block of statements. Hence in case of do while loop, statements are executed at least once.
do {
} while (expression);

What is Final Keyword in Java? Give an example.
In java, a constant is declared using the keyword Final. Value can be assigned only once and after assignment, value of a constant can’t be changed.
In below example, a constant with the name const_val is declared and assigned avalue:
Private Final int const_val=100
When a method is declared as final,it can NOT be overridden by the subclasses.This method are faster than any other method,because they are resolved at complied time.
When a class is declares as final,it cannot be subclassed. Example String,Integer and other wrapper classes.

What’s the difference between an Abstract Class and Interface in Java?
The primary difference between an abstract class and interface is that an interface can only possess declaration of public static methods with no concrete implementation while an abstract class can have members with any access specifiers (public, private etc) with or without concrete implementation.
Another key difference in the use of abstract classes and interfaces is that a class which implements an interface must implement all the methods of the interface while a class which inherits from an abstract class doesn’t require implementation of all the methods of its super class.
A class can implement multiple interfaces but it can extend only one abstract class.


Leave a Reply

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