Core Java Interview Questions and Answers – Part 8

In this post, Java Interview Questions and Answers are discussed, which are helpful for Java beginners and Experienced persons. When you go for Selenium Test Automation Interview, you can expect similar kind of questions.

141. Why to use nested classes in java?

  1. Grouping of related classes

Classes which are not reusable can be defined as inner class instead of creating inner class.

For example : We have a submit button upon click of submit button we need to execute some code. This code is related only to that class and cannot be reused for other class . Instead of creating a new class we can create inner class

  1. To increase encapsulation :

Inner class can access private members of outer class.so by creating getter and setter methods for private variables , outside world can access these variables. But by creating inner class private variables can be accesed only by inner class.

  1. Code readable and maintainable :

Rather than creating a new class we can create inner class so that it is easy to maintain.

  1. Hiding implementation :

Inner class helps us to hide implementation of class.

142. Explain about static nested classes in java?

When a static class is defined inside a enclosing class we define that as nested class. Static nested classes are not inner classes. Static nested classes can be instantiated without instance of outer class.

A static nested doesnot have access to instance variables and non static methods of outer class.

143. How to instantiate static nested classes in java?

We can access static members and static methods of outer class without creating any instance of outer class.

Syntax for instantiating Static nested class :

OuterclassName.StaticNestedClassName ref=new OuterclassName.StaticNestedClassName();

144. Explain about method local inner classes or local inner classes in java?

Nested classes defined inside a method are local inner classes. We can create objects of local inner class only inside method where class is defined. A local inner classes exist only when method is invoked and goes out of scope when method returns.

145. Explain about features of local inner class?

  1. Local inner class does not have any access specifier.
  2. We cannot use access modifiers static for local inner class. But we can use abstract and final for local inner class.
  3. We cannot declare static members inside local inner classes.
  4. We can create objects of local inner class only inside method where class is defined.
  5. Method local inner classes can only access final variables declared inside a method.
  6. Method local inner classes can be defined inside loops(for,while) and blocks such as if etc.

146. Explain about anonymous inner classes in java?

Inner class defined without any class name is called anonymous inner class. Inner class is declared and instantiated using new keyword.The main purpose of anonymous inner classes in java are to provide interface implementation. We use anonymous classes when we need only one instance for a class. We can use all members of enclosing class and final local variables.

When we compile anonymous inner classes compiler creates two files

  1. EnclosingName.class
  2. EnclsoingName$1.class

147. Explain restrictions for using anonymous inner classes?

  1. An anonymous inner class cannot have any constructor because there is no name for class.
  2. An anonymous inner class cannot define static methods, fields or classes.
  3. We cannot define an interface anonymously.
  4. Anonymous inner class can be instantiated only once.

148. Is this valid in java ? can we instantiate interface in java?

Runnable r = new Runnable() { 
@Override
public void run() {

}
};

Runnable is an interface.If we see the above code it looks like we are instantiating Runnable interface. But we are not instantiating interface we are instantiating anonymous inner class which is implementation of Runnable interface.

149. Explain about member inner classes?

Non static class defined with in enclosing class are called member inner class. A member inner class is defined at member level of class. A member inner class can access the members of outer class including private members.

Features of member inner classes :

  1. A member inner class can be declared abstract or final.
  2. A member inner class can extend class or implement interface.
  3. An inner class cannot declare static fields or methods.
  4. A member inner class can be declared with public, private, protected or default access.

150. How to instantiate member inner class?

OuterClassName.InnerclassName inner=new OuterClassReference.new InnerClassName();

We cannot instantiate inner class without outer class reference

151. How to do encapsulation in Java?

Make instance variables private.

Define getter and setter methods to access instance variables .

152. What are reference variables in java?

Variables which are used to access objects in java are called reference variables. Ex : Employee emp=new Employee();

In the above example emp is reference variable. Reference variable can be of only one type.

A reference variable can point to any number of objects. But if a reference variable is declared final it can’t point to other objects.

A reference variable can be declared either to a class type or interface type. If a reference variable is declared with interface type it points to the class that implements the interface.

153. Will the compiler creates a default constructor if I have a parameterized constructor in the class?

No compiler won’t create default constructor if there is parameterized constructor in the class. For example if I have a class with no constructors, then compiler will create default constructor.

For Example :

public classCar {
}

In the above Car class there are no constructors so compiler creates a default constructor.

public classCar {

classCar(String name) {
}

}

In this example compiler won’t create any default constructor because already there is one constructor in the Car class.

154. Can we have a method name same as class name in java?

Yes we can have method name same as class name it won’t throw any compilation error but it shows a warning message that method name is same as class name.

155. Can we override constructors in java?

Only methods can be overridden in java. Constructors can’t be inherited in java. So there is no point of overriding constructors in java.

156. Can Static methods access instance variables in java?

No.Instance variables can’t be accessed in static methods. When we try to access instance variable in static method we get compilation error. The error is as follows:

Cannot make a static reference to the non static field name

157. How do we access static members in java?

Instance variables and instance methods can be accessed using reference variable . But to access static variables or static methods we use Class name in java.

158. Can we override static methods in java?

Static methods can’t be overridden. If we have a static method in superclass and subclass with same signature then we don’t say that as overriding. We call that as

159. Difference between object and reference?

Reference and object are both different. Objects are instances of class that resides in heap memory. Objects does’nt have any name so to access objects we use references. There is no alternative way to access objects except through references.

Object cannot be assigned to other object and object cannot be passed as an argument to a method. Reference is a variable which is used to access contents of an object. A reference can be assigned to other reference ,passed to a method.

160. Objects or references which of them gets garbage collected?

Objects get garbage collected not its references.

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories