YouTube Icon

Interview Questions.

Top 100+ Java Inner Classes Interview Questions And Answers - May 31, 2020

fluid

Top 100+ Java Inner Classes Interview Questions And Answers

Question 1. What Is An Inner Class?

Answer :

Inner magnificence is a category described interior some other magnificence and act like a member of the enclosing elegance.

Question 2. What Are The Advantages Of Inner Classes?

Answer :

The embedding of inner elegance into the outer class in the case when the inner magnificence is for use only by using one elegance i.E. The outer class makes the bundle extra streamlined. Nesting the internal magnificence code wherein it's far used (within the outer magnificence) makes the code greater readable and maintainable. The inner class shares a unique dating with the outer elegance i.E.

The internal elegance has get right of entry to to all contributors of the outer elegance and still have its own type is the main advantages of Inner magnificence. Advantage of internal class is they may be hidden from the alternative training in the same package deal and still have the get right of entry to to all of the participants (personal also) of the enclosing class. So the outer class contributors which are going to be used by the inner magnificence may be made personal and the internal class individuals may be hidden from the training inside the same package deal.

This will increase the extent of encapsulation. If a category A is written requires any other magnificence B for its personal use, there are two ways to do that. One manner is to put in writing a separate magnificence B or to jot down an inner magnificence B interior elegance A. Advantage of writing the internal elegance B inside the magnificence A is you could avoid having a separate class. Inner instructions are nice used in the occasion handling mechanism and to put into effect the helper classes. The benefit of the usage of inner class for event coping with mechanism is that using if/else to select the element to be dealt with may be averted. If internal lessons are used each factor gets its own occasion handler and each occasion handler implicitly knows the issue it's far working for.

Adv Java Interview Questions
Question 3. What Is Static Member Class?

Answer :

A static member elegance behaves just like an everyday top-degree class, besides that it is able to get admission to the static participants of the class that carries it. The static nested elegance may be accessed as the opposite static contributors of the enclosing magnificence while not having an example of the outer class. The static elegance can incorporate non-static and static individuals and techniques. 

Public elegance InnerClass

static magnificence StaticInner

static int i = 9;

int no = 6;

non-public void method() 

public void method1() 

static void method2() 

final void method3() 

The static inner class can be accessed from Outer Class within the following way: 

InnerClass.StaticInner staticObj= new InnerClass. StaticInner (); 
No outer class instance is needed to instantiate the nested static magnificence due to the fact the static magnificence is a static member of the enclosing magnificence.
Question 4. What Are The Different Types Of Inner Classes?

Answer :

The under stated are the styles of internal lessons:– 

Static member elegance
Inner elegance
Member class
Anonymous elegance
Local magnificence
Adv Java Tutorial
Question 5. What Are Non Static Inner Classes?

Answer :

The one-of-a-kind form of static inner classes is: Non - static inner training – training associated with the object of the enclosing class. 

Member elegance - Classes declared outdoor a feature (as a result a "member") and now not declared "static". The member class may be declared as public, private, covered, final and summary.

Example:

public elegance InnerClass

elegance MemberClass

public void method1()  

Method local elegance – The inner magnificence declared inside the approach is known as approach neighborhood inner class. Method neighborhood internal class can best be declared as very last or abstract. Method neighborhood magnificence can simplest access international variables or approach local variables if declared as very last 

public class InnerClass

int i = nine;

public void method1()

very last int okay = 6;

class MethodLocal

MethodLocal()

System.Out.Println(ok + i);

Anonymous internal elegance - These are nearby lessons which might be robotically declared and instantiated in the center of an expression. Also, like local instructions, nameless instructions cannot be public, personal, covered, or static. They can specify arguments to the constructor of the superclass, however can't in any other case have a constructor. They can enforce most effective one interface or increase a class. 

Public class MyFrame extends JFrame

JButton btn = new JButton();

MyFrame()

btn.AddActionListener(new ActionListener()

public void actionPerformed(ActionEvent e) 

);

//Anonymous elegance used with comparator

List l = new ArrayList();

l.Add(new Parent(2));

l.Add(new Parent(three));

Collections.Sort(l, new Comparator()

public int compare(Object o1, Object o2)

Parent prt1 = (Parent) o1;

Parent prt2 = (Parent) o2;

if (prt1.GetAge() > prt2.GetAge())

go back -1;

else if(prt1.GetAge() 

return 1;

else

return 0;

);

J2EE Interview Questions
Question 6. Can A Static Nested Class Have Access To The Enclosing Class's Non-static Methods Or Instance Variables?

Answer :

No.

Question 7. What Are Disadvantages Of Using Inner Classes?

Answer :

Using inner elegance increases the total number of instructions being used by the application. For all of the training created by way of JVM and loaded in the memory, jvm has to carry out a few obligations like developing the object of type elegance. Jvm might also should perform a few recurring obligations for those extra instructions created which may also result slower performance if the application is the usage of more quantity of inner training. 
Inner instructions get limited help of ide/gear in comparison to the pinnacle level lessons, so running with the internal instructions is sometimes disturbing for the developer.
J2EE Tutorial Core Java Interview Questions
Question 8. What Are Different Types Of Anonymous Classes?

Answer :

Plain antique anonymous magnificence type one:–

class superclass

void doSomething() 

System.Out.Println(“Doing something inside the Super class”);

elegance hasAnonymous

superClass anon = new superClass()

void doSomething() 

System.Out.Println(“Doing something in the Anonymous elegance”);

;

Here anon is the reference that's of kind superclass which is the magnificence extended by using the anonymous elegance i.E. Superclass of the nameless magnificence. The approach doSomething () is the splendid elegance approach overridden by using the anonymous class.

Plain vintage anonymous magnificence type :–

interface Eatable 

public void prepare Sweets();

magnificence serveMeal 

Eatable meals = new Eatable()

public void

prepareSweets() //come implementation code goes right here

;

food is reference variable of type Eatable interface which refers back to the anonymous class that is the implementer of the interface Eatable. The anonymous implementer class of the interface Eatable implements its approach put together Sweets () inside it. 

Argument described nameless class:–

interface Vehicle 

void getNoOfWheels();

class Car 

void getType(Vehical v)  

magnificence BeautifulCars

void getTheBeautifilCar()

Car c = new Car ();

c.GetType (new Vehicle ()

public void getNoOfWheels ()

System.Out.Println("It has 4 wheels");

);

Anonymous elegance is described because the argument of the method getTheBeautifilCar (), this nameless elegance is the implementer of the interface Vehicle. The approach of sophistication Car getTheBeautifilCar () expects the argument as an item of kind Vehicle. So first we create an item of Car referenced through the variable ‘c’. On this item of Car we name the method getTheBeautifilCar () and inside the argument we create an anonymous class in vicinity that's the implementer of interface Vehicle for this reason of type Vehicle.

Question nine. If You Compile A File Containing Inner Class How Many .Elegance Files Are Created And Are All Of Them Accessible In Usual Way?

Answer :

If a internal class enclosed with an outer magnificence is compiled then one .Class document for each internal elegance and a .Magnificence report for the outer class is created.

Example:

magnificence EnclosingOuter

elegance Inner 

If you collect the above code with command:

% javac EnclosingOuter.Java Two files might be created. Though a separate internal class report is generated, the inner elegance report isn't always reachable inside the common manner. 

EnclosingOuter.Class

EnclosingOuter$Inner.Class

JSP Interview Questions
Question 10. How To Access The Inner Class From Code Within The Outer Class?

Answer :

The internal class is instantiated best via the outer elegance instance. 

Elegance EnclosingOuter

non-public int noInnerClass = 1;

public void getNoOfInnerClasses()

Inner in = new Inner();

System.Out.Println("No Of Inner lessons is :"+ in.GetNoOfClassesFromOuter());

elegance Inner

public int getNoOfClassesFromOuter()

 return noInnerClass;

Here the approach getNoOfInnerClasses () is known as at the outer magnificence’s example and thru this outer magnificence example the inner magnificence instance is created.

Core Java Tutorial
Question 11. How To Create An Inner Class Instance From Outside The Outer Class Instance Code?

Answer :

To create an instance of the inner elegance you should have the example of its enclosing class. 

Magnificence Enclosing Outer

class Inner 

To create the instance of inner magnificence from class aside from the enclosing magnificence.

1) elegance OtherThanOuter

Enclosing Outer out = new Enclosing Outer ();

EnclosingOuter.Inner in = out.New Inner ();

2) elegance OtherThanOuter

EnclosingOuter.Inner out = new EnclosingOuter.Inner ();

Java-Springs Interview Questions
Question 12. Which Modifiers Can Be Applied To The Inner Class?

Answer :

Following modifiers may be applied to the internal class: 

Public
Private
Abstract
Final
included
Strict
Static – turns the inner elegance into
Static nested class
Adv Java Interview Questions
Question 13. Can The Method Local Inner Class Object Access Method Local Variables?

Answer :

No, a way neighborhood internal magnificence item cannot get admission to the approach nearby variable. 

Reason: The nearby variables aren't guaranteed to live so long as the neighborhood internal class gadgets. The approach nearby variable live on stack and exist simplest until the technique lives, their scope is restricted upto the code within the method they are declared in. But the local internal class object created in the technique lives on heap and it can exist even after the method ends if in case the reference of this neighborhood internal elegance is surpassed into some different code and is saved in an example variable. So we can't make sure that the nearby variables will stay until the technique nearby internal magnificence object lives, consequently the technique local internal magnificence object can not get right of entry to the approach neighborhood variable. To get right of entry to the technique nearby variables, the variable has to be declared as very last.

JSP Tutorial
Question 14. Can A Method Local Inner Class Access The Local Final Variables? Why?

Answer :

Yes. Because the very last variables are saved on heap and live as long as the technique nearby inner elegance items can also live.

Question 15. Which Modifiers Can Be Applied To The Method Local Inner Class?

Answer :

Only summary or final key-word is authorized.

JMS(Java Message Service) Interview Questions
Question 16. Can A Local Class Declared Inside A Static Method Have Access To The Instance Members Of The Outer Class?

Answer :

No. There isn't any reference available in the static technique approximately the instance participants of the outer magnificence. The static technique elegance cannot have access to any members of the outer magnificence aside from static individuals.

Java-Springs Tutorial
Question 17. Can A Method Which Is Not In The Definition Of The Superclass Of An Anonymous Class Be Invoked On That Anonymous Class Reference?

Answer :

No. As the reference variable sort of the anonymous magnificence might be of superclass so that you can now not recognise approximately any method described in the nameless elegance and therefore the compilation will fail. 

Magnificence Superclass

void doSomething()

System.Out.Println ("In the Super elegance");

magnificence has Anonymous

Superclass anon = new Superclass ()

void doSomething()

System.Out.Println("In the Anonymous class");

void doStuff()

System.Out.Println ("An Anonymous class technique not found in

superclass");

;

public void doIt()

anon. DoSomething(); // criminal superclass has this approach

anon. DoStuff(); // Not prison

The above code will no longer compile because the superclass does now not realize about the anonymous elegance approach doStuff ().

Java applet Interview Questions
Question 18. Can An Anonymous Class Define Method Of Its Own?

Answer :

Yes. But there may be no manner through which the strategies defined within the anonymous elegance which aren't found in its superclass be invoked. As only the ones strategies which can be defined inside the superclass which the nameless magnificence extends be invoked. Hence defining the methods inside the nameless elegance could be of no need.

J2EE Interview Questions
Question 19. Can An Anonymous Class Implement An Interface And Also Extend A Class At The Same Time?

Answer :

No. An anonymous class can both make bigger a category or put in force a unmarried interface. If the nameless elegance is extending a category then it automatically turns into the implementer of all of the interfaces implemented by way of its superclass.

Java Tutorial




CFG