Inner Classes Types in Java

Inner class is a non-static nested class and which can access directly to its enclosing members or properties.

Inner class can be differentiate in to three types such as

  • Member inner classes
  • Local inner classes
  • Anonymous inner classes

Member Inner Class

A class is declared inside a class but outside of methods is called “Member Inner Classes”.

Check below example.

/**
 * Created by Dasun  
 */
public class MemberParent {
    private int score = 111;
    class MemberInner{
        void print(){
            System.out.println("CURRENT SCORE IS " +  score);
        }
    }

    void display(){
        MemberInner memberInner = new MemberInner();
        memberInner.print();
    }

    public static void main(String[] args) {
        MemberParent memberParent = new MemberParent();
        memberParent.display();
    }
}

Local Inner Classes

A Class which is created inside a method of parent/outer class is called local class.
Method which contains inner class should create an object of local inner class to access that inner class from outer class or external world. Check below example which shows how to create local inner class and access it.

/**
 * Created by Dasun .
 */
public class LocalParent {
    private  int score = 111;
    void innerMethod(){
        class LocalInner{
            void display(){
                System.out.println("Outer Class -> Method -> Inner Class called. Score is = " + score);
            }
        }
        LocalInner localInner = new LocalInner();
        localInner.display();
    }

    public static void main(String[] args) {
        LocalParent localParent = new LocalParent();
        localParent.innerMethod();
    }
}

Anonymous Inner Class

This is also similar to local inner classes mentioned above but has no name. This is mostly used to override methods of a class or an interface.
Check below example.
Below two time zones are displaying in UTC. Default one is USA and other one is Sri Lanka.
For USA anonymousInterfaceForUSA variable initialize using local inner class and anonymousInterfaceForSriLanka is initialized using anonymous inner class.

/**
 * Created by Dasun .
 */
public class AnonymousMaster {
    interface AnonymousInterface{
        public void timeZone();
        public void displayTimeZone(String contactHours );
    }

    public void getTimeZone(){

        class AnonymousInner implements AnonymousInterface{
            String timezone = "UCT ";
            @Override
            public void timeZone() {
                displayTimeZone( "+0");
            }

            @Override
            public void displayTimeZone( String contactHours ) {
                System.out.println(timezone + contactHours);
            }
        }

        AnonymousInterface anonymousInterfaceForUSA = new AnonymousInner();

        AnonymousInterface anonymousInterfaceForSriLanka = new AnonymousInterface() {
            String timezone = "UCT ";
            @Override
            public void timeZone() {
                displayTimeZone("+5");
            }

            @Override
            public void displayTimeZone(String contactHours) {
                System.out.println("SL TIME >> " +  timezone + contactHours);
            }
        };
        //
        anonymousInterfaceForUSA.timeZone();
        anonymousInterfaceForSriLanka.timeZone();
    }

    public static void main(String[] args) {
        AnonymousMaster anonymousMaster = new AnonymousMaster();
        anonymousMaster.getTimeZone();

    }
}

Reference URL :- http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s