Create a program in Java, implementing multiple inner classes. Class A
is the outer class, class B is inner class which is inside method of class
A and class Local is the method local inner class inside method of
class B. Access these classes from the main method in class Test and
override the method of class Local and print 'inside anonymous'.
Input format
No console input.
Output format
Refer sample input and output for formatting specifications.
Answers
Answer:
In common parlance, the terms "nested" and "inner" are used interchangeably by most programmers, but I'll use the correct term "nested class" which covers both inner and static.
Classes can be nested ad infinitum, e.g. class A can contain class B which contains class C which contains class D, etc. However, more than one level of class nesting is rare, as it is generally bad design.
There are three reasons you might create a nested class:
organization: sometimes it seems most sensible to sort a class into the namespace of another class, especially when it won't be used in any other context
access: nested classes have special access to the variables/fields of their containing classes (precisely which variables/fields depends on the kind of nested class, whether inner or static).
convenience: having to create a new file for every new type is bothersome, again, especially when the type will only be used in one context
There are four kinds of nested class in Java. In brief, they are:
class, class B is inner class which is
Explanation:
Inner classes: classes declared within another class.
- A regular class definition but inside another class - this is quite common for event handlers which are either too long to inline them as anonymous inner class or which are instantiated more than once. (Non-static) inner classes do have full access to any properties declared in the outer class.
- Tight coupling (inner class accessing properties of the outer class) might be more difficult to maintain and might cause trouble if you later have to refactor your class and move to the inner class outside as well. Therefore only classes that do need access to the outer classes' properties should be declared as non-static inner classes.
public class Outer {
private String str;
//TODO: add your code here
class Inner {
public void foo() { System.out.println(Outer.this.str); } } }
If you would need to create an instance of Inner in another class the code would look like the following:
class B {
public void bar() {
new Outer().new Inner();
}
}
Anonymous classes: inner classes declared without a proper name.
- classes declared inside a method without a name - mainly used for event handlers that are not needed anywhere else - i.e.
new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
//TODO: add your code here
}
}
Static inner classes: classes declared within another class, in the static context, which means it doesn’t have any reference to any instances of the outer class and can exist and be instantiated independently of them.
- Static inner classes are quite similar to non-static inner classes with the following differences:
- static inner classes are like standalone classes and therefore do not have access to member variables of the outer classes instance
- static inner classes can be instantiated from other classes without having to create an instance of the outer class as well
- The code would like something like this:
public class Outer {
private String str;
//TODO: add your code here
class Inner {
public void foo() { System.out.println(Outer.this.str); } } }
class B {
public void bar() { new Outer.Inner(); } }