Nested_Extended_class1.htm
 
External Class:

Outer Class 

inner or nested classes

Complete Codes :

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 ---");
B.D d1 = b2.new D (120);
System.out.println("objects from subclasses");
System.out.println
(b1.x1 + " " + c1.x1 + " "+ c2.x1+ " "+" "+d1.x1);
}catch(NumberFormatException e){
//
}}}
//Reference super class
class A{
int x1 ;
A(){ // implicit constructor
System.out.println
("superclass Constructor undefined () A invoked");
} // explicit constructor
A(int n1){
x1 = n1;
System.out.println("Constructor (int n1) explicit A invoked"
+ this.getClass()); System.out.println("----");
}
}

//Outer class
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 extends A{
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
class D{
int x1 ;
D(){
System.out.println
("superclass Constructor undefined () D invoked");
} // explicit constructor
D(int n1){ // implicit constructor
x1 = n1;
System.out.println("Constructor (int n1) explicit D invoked"
+ this.getClass().getSuperclass());
System.out.println("----");
} }
}
//

Runtimes View