Showing posts with label outer class. Show all posts
Showing posts with label outer class. Show all posts

Monday, 22 January 2018

Kotlin: Access super class of outer class from inner class

By using the super keyword qualified with outer class name, we can access the super class of the outer class. Let’s me explain with an example.

Ex

Find below working application.

HelloWorld.kt
open class GrandParentClass {
 fun grandParentFunction() {
  println("I am in grand parent class")
 }
}

open class ParentClass : GrandParentClass() {
 fun parentClass() {
  println("I am in parent class")
 }

 inner class ChildInnerClass {
  fun printInfo() {
   parentClass()
   super@ParentClass.grandParentFunction()
  }
 }
}

fun main(args: Array<String>) {
 var innerObj = ParentClass().ChildInnerClass()

 innerObj.printInfo()
}

Output
I am in parent class
I am in grand parent class



Previous                                                 Next                                                 Home

Wednesday, 10 January 2018

Kotlin: Inner classes

Inner classes are like nested class, only difference is you can access the members of outer class from inner class.

How to define inner class?
Inner class is defined using the keyword ‘inner’.

How to define object of inner class?
var innerObj = Outer().Inner()

InnerDemo.kt
class Outer {

 fun printMsg() {
  println("Inside Outer class")
 }

 inner class Inner {
  fun printMsg() {
   println("Inside Inner class")
  }
 }
}

fun main(args: Array<String>) {
 var outerObj = Outer()
 var innerObj = Outer().Inner()

 outerObj.printMsg()
 innerObj.printMsg()
}

Output
Inside Outer class
Inside Inner class

You can able to access the member variables of outer class from inner class.


InnerDemo.kt

class Outer {
 private var x = 10

 inner class Inner {
  fun getX() = x
 }
}

fun main(args: Array<String>) {
 var innerObj = Outer().Inner()

 var x = innerObj.getX()

 println("x = $x")
}

Output

x = 10


Previous                                                 Next                                                 Home

Monday, 10 February 2014

Inner Class

Non static nested classes are called inner classes

Syntax
    class OuterClass {
        ...
        class InnerClass {
            ...
        }
    }

How to create an object to the inner class
    OuterClass.InnerClass innerObject = outerObject.new InnerClass();

Example
class Outer{
 void print(){
  System.out.println("I am in outer class");
 }

 class Inner{
  void print(){
   System.out.println("I am in Inner class");
  }
 }

 public static void main(String args[]){
  Outer outerObj = new Outer();
  Outer.Inner obj = outerObj.new Inner();
  obj.print();
 }
}

Output
I am in Inner class


There are two additional types of inner classes.

Those are:
    1. Local Classes

Some points to Remember
1. Inner class is associated with an instance, it cannot define any static members itself. Defining static variable inside inner class causes the compiler error
class Outer{
 class Nested{
  static int a = 100;
 }
}
When you tries to compile the above program, compiler throws the below error
Outer.java:3: error: Illegal static declaration in inner class Outer.Nested
           static int a = 100;
           ^
modifier 'static' is only allowed in constant variable declarations
1 error
      

2. You can define static constants in the inner class, but not static variables.
class Outer{
 class Nested{
  static final int a = 100;
 }
}
    3. Defining static methods inside inner class causes compiler error
    class Outer{
     class Nested{
      static void print(){
      }
     }
    }
    
    When you tries to compile the above program, compiler throws the below error
    Outer.java:3: error: Illegal static declaration in inner class Outer.Nested
             static void print(){
             ^
    modifier 'static' is only allowed in constant variable declarations
    1 error
    
         
    4. An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance
    Example
    class Outer{
     String name;
    
     void setName(String name){
      this.name = name;
     }
    
     class Inner{
      public void print(){
       System.out.println(this.name);
       System.out.println(Outer.this.name);
      }
    
      String name;
           
      void setName(String name){
       this.name = name;
      }
    
      public String toString(){
       return name;
      }
     }
    
     public static void main(String args[]){
      Outer obj = new Outer();
      obj.setName("Outer Class Object");
      Outer.Inner in = obj.new Inner();
      in.setName("Inner Class Object");
      in.print();
     }
    }
    
            
    Output
    Inner Class Object
    Outer Class Object
    


    Static Nested Classes                                                 Local Classes                                                 Home

    Nested Classes

    Java provides a facility like, you can define a class inside other class. This feature is called Nested classes.

        Example
        class OuterClass {
            class NestedClass {

            }
        }

    Nested Classes can be static or non-static. A Nested class can be private, public, protected and default. Non-static nested classes are called inner classes.

        Example
        class OuterClass {   
            static class StaticNestedClass {

            }

            class InnerClass {

            }
        }

    Uses Of Nested classes
    1. Improve Encapsulation : We can make the nested classes as private, so outside world these nested classes are not visible.
    2. More readable code : Making small nested classes and enclosing thse with outer classes, makes the code readable


    final keyword                                                 static nested classes                                                 Home