Search Java Interview Questions
| 2159 questions in repository.|
There are more than 200 unanswered questions.
Click here and help us by providing the answer.
|Java - Interview Questions and Answers|
|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
| What is the difference between the following two code lines ?|
1. new OuterClass().new InnerClass();
2. new OuterClass.InnerClass();
|What are the different types of inner classes ?|
|Which access specifier can be used with Class ?|
|Difference between nested and inner classes ?|
|Which of the following cannot be marked static ?|
|Explain use of nested or inner classes ?|
|What is the benefit of inner / nested classes ?|
|Explain Inner Classes ?|
|Explain Method Local Inner Classes ?|