Nested Class |
public class test extends base_class.nested_class { test(base_class bc){ bc.super(); System.out.println("test constructor");} public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Hi"); base_class bc = new base_class(); test t = new test(bc); System.out.println ("first read public variable : n1 = " + bc.n1); t.nested_method(); } } class base_class { public int n1 = 12; base_class() { System.out.println("base class constructor : " ); } class nested_class { int n2 = 24; public void nested_method(){ n1 = n2; System.out.println("override with private n2 : n1 = " + n1); System.out.println("Hello from nested class"); } }//end of nested class } |
In the above example we are viewing a nested class being used from another class 'test" that inherit the "base_class" |
The image below shows role of test constructor in the above
example, and how compiler sees to this object.It would allow us to use "new"
key word to create an object from the class that will allow to latch on the
methods in the super class (base_class).
|
The use of the object "bc" and "super" key word in the above
scenario, here "bc- represnts test.test(base_class)", where as super represents "base_class.nested_class". Now a dot operator glue these two
objects together and present it to the java compiler.
|
If you need to run from command line
|