Starkers
3
Q:

java inner class

class OuterClass {
  int x = 10;

  class InnerClass {
    int y = 5;
  }
}

public class MyMainClass {
  public static void main(String[] args) {
    OuterClass myOuter = new OuterClass();
    OuterClass.InnerClass myInner = myOuter.new InnerClass();
    System.out.println(myInner.y + myOuter.x);
  }
}

// Outputs 15 (5 + 10)
 
2
In Java, it is possible to define a class within another class, such 
classes are known as nested classes. They enable you to logically group 
classes that are only used in one place, thus this increases the use of 
encapsulation, and creates more readable and maintainable code.
0
//Only fields and methods are inherited. 
//Inner class can extend it's outer class. 
//... Because, even the private members of outer 
//class are available inside the inner class. 
//Even though, When an inner class extends its outer class, 
//only fields and methods are inherited but not inner class 
//itself.
0

New to Communities?

Join the community