What is Java inner class? Explain the types of inner classes, i.e. Static member classes, Member classes, Local classes, Anonymous classes
Inner Class : A class defined inside another class is called an ‘Inner Class’.
Types of inner classes :
1.
Member inner class : An inner class which has the scope within another class
Exampleclass Outer
{
Inner inner = new Inner();
void show()
{
System.out.println("Outer is invoking inner show() method");
innr.show();
}
class Inner
{
// Member inner class
void show()
{
System.out.println("In the inner class");
}
}// end of member inner class
} // end of outer class(enclosing class)
2.
Local Inner Class : An inner class declared within a block
Example public class Outer
{
void show()
{
System.out.println("Outer is invoking inner show() method");
innr.show();
class LocalInner
{
void showLocal()
{
System.out.println("Local inner class - show()");
}
}
LocalInner localinner = new LocalInner();
localinner.showLocal();
} // end of method show()
} // end outer class
3.
Static Inner class : An inner class with access modifier as ‘static’
Examplepublic class Outer
{
Outer.StaticInner stcinnr = new Outer.StaticInner();
void show()
{
System.out.println("Outer is invoking inner show() method");
stcinnr.show();
}// end of show()
static class StaticInner
{
void show()
{
System.out.println("In the Static inner class");
} // end os static inner class
}
} // end of outer class
4.
Anonymous Inner Class:
An inner class without name. Anonymous inner classes are defined within a method can be used only once. The definition, object creation and the method invocation is done all at once in an anonymous inner class.
Example import java.awt.*;
import java.awt.event.*;
public class MyFrame extends Frame
{
MyFrame()
{
super("MyFrame");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {System.ext(0)};
}
}
}
In the above example, the WindowAdapter implements the methods that are required by the WindowListener interface.
Advantages of Inner classes 1. All class are logically grouped in one module.
2. It enhances the encapsulation
3. The inner classes lead to more reliable, readable and maintainable code
Write and explain to show the Java inner class in use.
The following is the code that illustrates all the four inner classespackage innerclasses;
public class Outer
{
Inner innr = new Inner();
Outer.StaticInner stcinnr = new Outer.StaticInner();
void show()
{
System.out.println("Outer is invoking inner show() method");
innr.show();
class LocalInner
{
void showLocal()
{
System.out.println("Local inner class - show()");
}
}
LocalInner localinner = new LocalInner();
localinner.showLocal();
stcinnr.show();
}
class Inner
{ // Member inner class
void show()
{
System.out.println("In the inner class");
}
}
static class StaticInner
{
void show()
{
System.out.println("In the Static inner class");
}
}
}
// main
public static void main(String[] args)
{
Outer outer = new Outer();
outer.show();
}// main()
}// end of Outer
The class Outer is the enclosing class of all inner clases.
The class Inner is the example of Member Inner class – defined in the scope of the outer class
The class StaticInner is the static inner class. It is defined within the scope of the Outer class and has a single copy for Outer class, being static
The LocalInner class is defined within the method .
Anonymous Inner class example:
Anonymous inner classes are used to add listener objects in event driven programming in Java, like AWT and Swing. For example, clicking a button is an event.
To add an action when a button is clicked, it is not needed to define a named class with much of functionality.
The required action can be authored by using an anonymous inner class
The following is the code snippet for anonymous inner class.
class MyPanel extens JPanel
{
JButton button = new JButton();
button.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent actEv)
{
button.setText(“The Button Clicked”);
} // end of actionPerformed
} //end of ActionListener
} // end of button.addActionListener
Need of inner class in context with adapter classes
Callbacks for GUI functionality are defined in local inner classes.
Components can share an object which implements an event handling abstract adapter class, later, which executes the functionality when a given event is triggered.
An adapter class can be used as anonymous inner class for drawing a rectangle.
The mouse events,like click, right click etc., are fired within this inner class.
The following
code example generates a rectangle by clicking the mouse from the top left corner to the bottom right corner.
The code demonstrates the functionality of ‘adapter class’ and anonymous inner class.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class AdapterInnerClassesDemo extends Applet
{
public void init()
{
addMouseListener(new MouseAdapter()
{
int topAtX, bottomAtY;
public void mousePressed(MouseEvent msEvt)
{
topAtX = msEvt.getX();
bottomAtY = msEvt.getY();
}
public void mouseReleased(MouseEvent msEvt)
{
Graphics grph = AdapterDemo.this.getGraphics();
grph.drawRect(topAtX, bottomAtY, msEvt.getX()-topAtX, msEvt.getY()-bottomAtY);
}
} // end of anonymous inner class
}
}
Functionality of wrapper classes. List the primitive types and the corresponding wrapper classes
1. At times the primitive types are needed to be referred as objects
2. Every primitive type has its corresponding wrapper class
3. The wrapper classes encapsulate the primitive data types within a class. Hence the name wrapper class
4. Wrapped classes are used to represent primitive types when an object is required.
5. The wrapper classes are used in Collection Framework by using java.util package
6. Object methods are provided for primitive types by using wrapper classes.
7. Following are the Primitive types and their corresponding wrapper clases:
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
All the wrapper classes are subclasses of the Number class.
Explain few Wrapper Classes Methods.
Following are few methods of Wrapper Classes:
1. static int digit(char ch, int radix)
The character ch’s numeric value will be returned in the specified radix
2. static boolean isDigit(char ch)
Returns true, if the given ch is a digit
3. static boolean isLetter(char ch)
Returns true, if the given ch is an alphabet
4. static int parseInt(String s)
Parses (Converts) the given string argument as a decimal integer.
5. static float parseFloat(String s)
Parses (Converts) the given string argument as float number
6. String toString()
Converts a given Long value into a String
7. static String toHexString(double d)
Hexadecimal string representation of the given double argument is returned