Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is anonymous inner class in java and how to implement interface in annonymous class ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 286
    Comment on it

    Anonymous means no name, a class with no name called as Anonymous class. But is has advantage like to create an instance of class in one step.

    We can'e create an object of this class since it has no name.

    An anonymous class can implement interface or can extend another class in it like this :

    public interface MyInterface extends SuperInterface{
        public void value();
    }
    
    

    another super interface to show how can anonymous inner class can implement another interface

    public interface SuperInterface {
        public void myName();
    }

    main class to use anonymous class :

    MyInterface myInterface = new MyInterface() {
    
        @Override
        public void value() {
            System.out.println("annonymous inner class using interface");
        }
    
        @Override
        public void myName() {
            System.out.println("annonymous inner class using super interface");
    
        }
    };
    myInterface.value();
    myInterface.myName();
    
    }

     

    output :

    annonymous inner class using interface
    annonymous inner class using super interface

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: