nested subclass java code example
Example 1: Nested classes in java
interface HelloWorld
{
String print();
}
public class AnonymousInnerDemo
{
public void display(HelloWorld m)
{
System.out.println(m.print() + "example of anonymous inner class as method argument.");
}
public static void main(String[] args)
{
AnonymousInnerDemo obj = new AnonymousInnerDemo();
obj.display(new HelloWorld() {
public String print() {
return "Hi! ";
}
});
}
}
Example 2: Nested classes in java
class OuterClass
{
private int number = 56;
public class InnerClass
{
public int showNumber()
{
System.out.println("showNumber() method of inner class.");
return number;
}
}
}
public class AccessPrivateMembers
{
public static void main(String[] args)
{
OuterClass objOuter = new OuterClass();
OuterClass.InnerClass objInner = objOuter.new InnerClass();
System.out.println(objInner.showNumber());
}
}