Nested Classes
Nested Classes
By
Arvind Kumar
Asst. Professor, LPU
Nested Class
• The Java programming language allows us to define a
class within another class. Such a class is called a nested
class.
Example:
class OuterClass
{
...
class NestedClass
{
...
}
}
Types of Nested Classes
• A nested class is a member of its enclosing class.
• Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
• For example, to create an object for the static nested class, use this
syntax:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
Inner Classes
• An inner class is associated with an instance of its enclosing class
and has direct access to that object's methods and fields.
• They are like local classes except that they do not have a
name.
Anonymous classes also have the same restrictions as local classes with
respect to their members:
• We cannot declare static initializers in an anonymous class.
• class OuterTest {
• public static void main(String args[]) {
• // instantiation 1
• Airplane airplane = new Airplane();
• Airplane.BlackBox box1 = airplane.new BlackBox();
• // instantiation 2
• Airplane.BlackBox box2 = new Airplane().new BlackBox();
• // instantiation 3
• Airplane airplane3 = new Airplane();
• BlackBox box3 = airplane3.new BlackBox();
• } }
• class Airplane {
• class BlackBox {
• } }
• choose all the correct answers
• A) Instantiation 1
• B) Instantiation 2
• C) Instantiation 3
• Ans- AB
Brain Storming Question
• class AirJet {
• public static void main(String[] args) {
• // instantiation 1
• Engine engine1 = new AirJet().new Engine();
• // instantiation 2
• AirJet.Engine engine2 = new AirJet().new Engine();
• // instantiation 3
• AirJet airjet = new AirJet();
• Engine engine3 = airjet.new Engine();
• // instantiation 4
• Engine engine4 = new Engine();
• }
• class Engine{ }
• }
• Choose all the correct answers
• A) Instantiation 1
• B) Instantiation 2
• C) Instantiation 3
• D) Instantiation 4
• Ans- ABC
Brain Storming Question
• class AirPlane {
• public void fly(int speed) {
• final int e = 1;
• class FlyingEquation {
• {
• System.out.println(e);// line 1
• System.out.println(speed);// line 2
• } } } }
• A) Both statements at lines 1 and 2 are correct
• B) Both statements at lines 1 and 2 are incorrect and cause compilation
errors
• C) Compilation error at line 1 , inner class can't access outer class local
variables
• D) Compilation error at line 2, inner class can't access method's arguments
• Ans-A
Brain Storming Question
• class Body {
• String name = "Body";
• public static void main(String[] args) {
• System.out.println(new Body().new Heart().getPName());
• }
• class Heart {
• String name = "Heart";
• String getPName() {
• // insert statement here
• }
• }
• }
• Choose all the correct answers
• A) return Body.name;
• B) return Body.this.name;
• C) return super.name;
• D) return this.super.name;
• Ans-B
Brain Storming Question
• class Outer
• {
• public static void main(String[] a)
• {
• static class Inner{
• }
• }
• }
• A) compilation error
• B) Runtime error
• C) program will execute successfully
• D) none of these
• Ans-C
Brain Storming Question
• class Outer {
• private int i = 5; // line 1
• Outer(int i ){ // line 2
• this.i = i; // line 3
• System.out.print(++i); // line 4
• }
• class Inner{
• Inner(){
• System.out.print(","+ i++); // line 5
• }
• }
• public static void main(String[] args) {
• int i = 6;
• Outer.Inner in = new Outer(i).new Inner(); // line 6
• }
• }
• choose only one answer:
• A) 6,6
• B) 7,7
• C) 7,6
• D) Compilation error
• Ans-C
Brain Storming Question
• class Test {
• public static void main(String[] args) {
• new OuterClass(); // line 3
• }}
• class OuterClass {
• private int x = 9; // line 5
• public OuterClass() {
• InnerClass inner = new InnerClass(); // line 7
• inner.innerMethod();
• }
• class InnerClass {
• public void innerMethod() {
• System.out.println(x);
• } } }
• choose only one answer:
• (1) Prints 9
• (2) Error during compilation at line 3
• (3) Error during compilation at line 5
• (4) Error during compilation at line 7
• Ans-(1)
Questions