Nested_PuvlicClass1.htm
Objectives :
  • Nested Public Class : by default is subclass of class "java.lang.Object"
 

Complete Code :


import java.io.*;
import java.lang.reflect.*;
import java.util.*;

public class ClassTemplate1 {

public String str1; private String str2;
public ClassTemplate1() {
// TODO Auto-generated constructor stub
str2="encapsulated strictly Private scope";
System.out.println(str2);
}

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
try{
System.out.println(" Class B objects---");
B b1 = new B(20);
B b2 = new B();
System.out.println(" Inner Class C objects---");
B.C c1 = b2.new C(40); // access nested constructor
B.C c2 = b1.new C(90); //access nested constructor
//System.out.println(" Class D objects ---");
//D d1 = new D (60);
System.out.println("objects from subclasses");
System.out.println
(b1.x1 + " " + c1.x1 + " "+ c2.x1+ " ");
//(b1.x1 + " " + c1.x1 + " "+ c2.x1+ " "+d1.x1);

}catch(NumberFormatException e){
//
}
}
}


class B{
int x1;
B(){
System.out.println("super class Constructor implicit B() invoked");
} // explicit constructor
B(int n1){
x1 = n1;
System.out.println("Constructor (int n1) B invoked : super "
+ this.getClass().getSuperclass());
C cnc1 = new C( );
System.out.println("cnc1---"+cnc1.x1);
}
// inner nested class
public class C {
int x1;
C(){
System.out.println("inner class C () default constructor" +
this.getClass().getSuperclass());
}
C(int n1){
x1 = n1;
System.out.println("Constructor (int n1) C invoked : super "
+ this.getClass().getSuperclass());
System.out.println("secret number " +x1 );
}
}
// nested inner class
}
//

Runtime views :

Special Notes : A class and it's nested class by default are the subclasses of  "java.lang.object"