Java - Interview Questions and Answers for 'Inner class' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Inner class' - 20 question(s) found - Order By Newest

 Q1. Why do we need Inner classes ? Cant we just work with outer classes wherever we implement Inner classes ?Core Java
Admin
info@buggybread.com
Ans. Yes, we can substitute outer classes wherever we need to have inner classes but Inner classes have advantage in certain cases and hence preferred -

Ease - Why to implement a class outside if its objects are only intended to be part of an outer object. Its easy to define the class within another class if the use is only local.

Protection - Making a call an outer exposes a threat of it being used by any of the class. Why should it be made an outer class if its object should only occur as part of other objects.

For example - You may like to have an class address whose object should have a reference to city and by design thats the only use of city you have in your application. Making Address and City as outer class exposes City to any of the Class. Making it an inner class of Address will make sure that its accessed using object of Address.

  Sample Code for inner class

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   inner classes   classes   objects   technical lead      intermediate

Try 1 Question(s) Test


 Q2. What is the difference between the following two code lines ?

1. new OuterClass().new InnerClass();

2. new OuterClass.InnerClass();
Core Java2016-06-30 14:45:37

Ans. In first case we are trying to initialize Inner class object using the instance of Outer Class whereas in second case we are trying to initialize the Inner class object directly using the Outer class name.

In second case , Inner class is "static inner class" as we cannot access "non static inner class" using Classname alone.

In first case, the inner class could be either "static inner class" or "non static inner class".

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inner classes  inner class   static inner class   new operator


  Q3. What are the different types of inner classes ?Core Java
Anonymous
Ans. Simple Inner Class,
Local Inner Class,
Anonymous Inner Class,
Static Nested Inner Class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   inner classes   classes     Infosys  Tata Consultancy (TCS)  HCL Technologies  IBM  IBM India  Gateway Media      basic        frequent

Try 1 Question(s) Test


 Q4. Which access specifier can be used with Class ?
Anonymous
Ans. For top level class we can only use "public" and "default". We can use private with inner class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   class   access specifier   inner class      basic        frequent


 Q5. Difference between nested and inner classes ?
Anonymous
Ans. Inner classes are non static nested classes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   static   nested classes   inner classes   basic interview question      basic        frequent

Try 1 Question(s) Test


 Q6. Explain use of nested or inner classes ?
Anonymous
Ans. Sometime we just need classes or class objects just to be used as part of a particular class or objects. Making them non nested won't make any difference as far as functionality is concerner but making them Nested provide a level of convenience and protection fro, being used anywhere else. Moreover it helps reducing the Code.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   inner classes   nested class   inner class     IBM

Try 1 Question(s) Test


 Q7. What is the benefit of inner / nested classes ?Core Java
Admin
info@buggybread.com
Ans. You can put related classes together as a single logical group.

Nested classes can access all class members of the enclosing class, which might be useful in certain cases.

Nested classes are sometimes useful for specific purposes. For example, anonymous inner classes are useful for writing simpler event-handling code with AWT/Swing.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   nested classes   inner classes   oops   classes

Try 1 Question(s) Test


 Q8. Explain Inner Classes ?Core Java
Admin
info@buggybread.com
Ans. The accessibility (public, protected, etc.) of the inner class is defined by the outer class.

Just like top-level classes, an inner class can extend a class or can implement interfaces.

Similarly, an inner class can be extended by other classes, and an inner interface can be implemented or extended by other classes or interfaces.

An inner class can be declared final or abstract.Inner classes can have inner classes, but you will have a hard time reading or understanding such complex nesting of classes.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   classes   inner classes     Computer Sciences Corporation (CSC) India      Basic        frequent

Try 2 Question(s) Test


 Q9. Explain Method Local Inner Classes ?Core Java
Admin
info@buggybread.com
Ans. You can create a non-static local class inside a body of code. Interfaces cannot have local classes, and you cannot create local interfaces.

Local classes are accessible only from the body of the code in which the class is defined. The local classes are completely inaccessible outside the body of the code in which the class is defined.

You can extend a class or implement interfaces while defining a local class.

A local class can access all the variables available in the body of the code in which it is defined. You can pass only final variables to a local inner class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   inner classes   local classes   classes   method local inner classes     Cognizant (CTS)



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. Explain about anonymous inner classes ?
Admin
info@buggybread.com
Ans. Anonymous classes are defined in the new expression itself, so you cannot create multiple objects of an anonymous class.

You cannot explicitly extend a class or explicitly implement interfaces when defining an anonymous class.

An anonymous inner class is always created as part of a statement; don't forget to close the statement after the class definition with a curly brace. This is a rare case in Java, a curly brace followed by a semicolon.

Anonymous inner classes have no name, and their type must be either a subclass of the named type or an implementer of the named interface

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   oops   inner classes   anonymous inner classes   classes


 Q11. Which of the following is not type of inner classes ?

a. Simple Inner Class
b. Static Nested Inner Class
c. Method Nested Static Inner Class
d. Anonymous Inner Class
Anonymous
Ans. Method Nested Static Inner Class

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   classes   inner classes

Try 1 Question(s) Test


 Q12. Can we have an inner class within Interface ?Core Java2016-10-24 11:18:24

Ans. Yes, we can define an inner class within interface.Inside the interface, the inner class is implicitly public static.

So the following is legit in Java

public interface BuggyBreadInterface {
   void doSomething();

   public class BuggyBreadClass implements BuggyBreadInterface{
      void doSomething(){
         System.out.println("Do something");
      }
   }
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inner classes  inner class in interface  nested classes


 Q13. how to access the methods of an inner class?Core Java2017-01-14 11:11:38

Ans. It depends on the type of inner class

To access non static inner class method

new OuterClass().new InnerClass().getMethod();

To access static method of static inner class

new OuterClass.InnerClass().getMethod();

  Sample Code for inner class

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inner classes  nested aclasses     SuccessFactors      Intermediate        frequent


 Q14. How can we hide a class in Java ?Core Java2017-06-07 16:42:57

Ans. By encapsulating it within another class and declaring it private. In such a case, it will only be accessible through parent class or parent class object.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     private class   private inner class  class hiding  encapsulation


 Q15. What is the difference between inner class and sub class ?Core Java2017-06-18 16:30:30

Ans. Inner Class is a class that is nested within another class whereas sub class is a class that extends or inherit another class.

Inner class can only be accessed using reference of outer class ( Class name , if inner class is static or object reference, if inner class is non static ) whereas Sub class is accessed directly.

In terms of memory, inner class is stored just like another class having it's own body whereas sub class carries the body of parent class as well as it's own fields in memory.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inner class  nested class  inner class vs sub class  nested class vs sub class      Basic


 Q16. Can an inner class be subclass of it's parent class ?Core Java2017-06-18 16:36:17

Ans. Yes that can be done

public class OuterClass {
public class InnerClass extends OuterClass {
}
}

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inner class  nested class  sub class      Basic


 Q17. Is this code legal in java i.e nested class within interface implementing the parent interface ?

public interface MyInterface {
public class MyClass implements MyInterface {
}
}
Core Java2017-06-18 16:37:53

Ans. Yes that's legal in java

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inner class  nested class  sub class      Basic


 Q18. Can an interface be defined inside a class just like we can have inner classes inside a class ?Core Java2017-06-23 08:13:43

Ans. Yes , we can have an interface that is part of a class.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     inner class   inner interface  nested classes


 Q19. Why can we have a static inner class but not a static Top level class in Java ?Core Java2017-08-23 20:14:59

 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     static class  inner classes      Expert        rare


 Q20. Which of following can be nested into another ?Core Java
a. class within another class
b. class within interface
c. interface within class
d. All of above

Ans.d. All of above



Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: