Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Is it possible to instantiate an interafce ?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Is it possible to instantiate an interafce ?

    Greeting all ,

    please , can you explain when instantiating an interface is acceptable and reasonable ?

    i'm asking due to with my pre knowledge that an interface can't be instantiate .

    example :
    addMouseListener(new MouseAdapter(){
    public void mousePressed(MouseEvent e){
    moveSquare(e.getX(),e.getY());
    }
    });


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Is it possible to instantiate an interafce ?

    Always read the API documentation - MouseAdapter is not an interface:
    This class exists as convenience
    MouseAdapter (Java Platform SE 6)

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to instantiate an interafce ?

    yes i'm sorry and you are right i did provided a wrong example ...

    what i mean is something like this

    interface anInterface { void aMethod(); }
    class myClass { anInterface A1 = new anInterface();
    }

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Is it possible to instantiate an interafce ?

    I'm not exactly sure whether the answer is exactly yes or no (* see edit below), but you can instantiate an anonymous object (The Java tutorial docs call these anonymous inner classes) which implements an interface:

    package com.javaprogrammingforums.domyhomework;
     
    interface TheInterface
    {
    	public void doSomething();
    }
    public class InstantiateAnInterface
    {
    	public static void main(String[] args)
    	{
    		new TheInterface()
    		 {
    		  public void doSomething()
    			{
    				System.out.println("Wahey!");
    			}
    		 }.doSomething();
    	}
    }

    ... and of course you can write your own class that *implements* an interface


    edit: helloworld922 (see below) is absolutely technically correct - the answer is 'no'. The fact that we typically always comply with a request from someone to give them an instance of MouseListener (rather than saying "actually that is impossible end of story") is no excuse for my prevarication.
    Last edited by Sean4u; August 8th, 2011 at 03:03 AM.

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    Learner (August 7th, 2011)

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Is it possible to instantiate an interafce ?

    yes , i will read about this concept " anonymous inner classes " it is new for me , and thank you very much Sean4u for ur responding .

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Is it possible to instantiate an interafce ?

    You cannot directly instantiate abstract classes or interfaces, period. However, what you have there is an anonymous inner class, which happens to extend another class (in this case, it's the MouseAdapter class, which happens to be abstract).

  8. #7
    Member
    Join Date
    Aug 2011
    Posts
    48
    My Mood
    Fine
    Thanks
    1
    Thanked 4 Times in 4 Posts

    Default Re: Is it possible to instantiate an interafce ?

    Hi Learner,

    It is impossible to instantiate an interface, but you can instantiate new objects of classes which derived from that interface. For example:

    interface A { }

    class B implements A { }

    // WRONG: A a = new A();

    // CORRECT:
    A a = new B();// this is possible, since B implements A

    This is something called polymorphism, that means the declared type is static, but the runtime object can be changed dynamically.

    immutable objects
    Last edited by ha.minh.nam; December 4th, 2011 at 07:25 PM.

  9. The Following User Says Thank You to ha.minh.nam For This Useful Post:

    Learner (August 8th, 2011)

  10. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    18
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Is it possible to instantiate an interafce ?

    You can not instantiate Interface in any case. And the example is just anonymous class and it looks like you are initiating Interface.

Similar Threads

  1. [SOLVED] instantiate problems
    By jamal in forum Object Oriented Programming
    Replies: 9
    Last Post: September 13th, 2010, 06:08 PM