Java Link

See which of your colleagues or former colleagues are already on Java Link: Check out the Contact Finder
Java - General » J2SE » Interface

383 Views
  8 Replies

0
Vote Vote
Interface
Share  
 Type: Professional, Report if fun topic
Arvind
Arvind Picture
2 Posts
back to top
Posted 15-06-2009Reply

Can we create object of interface..?

Palaniappan
Palaniappan Picture
1 Posts
back to top
  Rated 0 | Posted 15-06-2009

Definitely no. We can create reference only. We can assign the implemented class's object to the interface reference.

Satya
Satya Picture
6 Posts
back to top
  Rated 0 | Posted 15-06-2009

No chance, bcoz interface contains only public static variables, and these r not belongs to object, belongs to class, then no use of creating object for interface.

Swapnilkumar
Swapnilkumar Picture
10 Posts
back to top
  Rated 0 | Posted 28-10-2009

yes sure.
her's an AWT example

Button closingButton = new Button("CLOSE");
closingButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
}
);

Its an example of anonymous inner class.
actually its a trick, its not an object of ActionListener interface but an object of the class (implicitly) implementing ActionListener. But simply it looks as "new ActionListener()" thats y i said yes!!!

By the way, interfaces are implicitly abstract, they cannot be directly instantiated. Interfaces are special to java, as they provide so many features to java,

1) Polymorphism.

2) multiple inheritance (as a class can implement zero or more interfaces & an interface can extend zero or more interfaces)

3) Function callback.

4) Marker interface.(java.lang.Cloneable, java.io.Serializable, java.rmi.Remote these interfaces are marker interfaces)

...... and much more..

Regards Swapnil.

Saudagar
Saudagar Picture
14 Posts
back to top
  Rated 0 | Posted 13-11-2009

Basically you can't create object of an interface.
The reason behind is that interface contains only method's declaration and not definition.
If you want to create object of interface, then you need to define its methods as shown by swapnilkumar.

But I have one question that we have several interface in Java those don't have any method, in that case can we create object of that interfaces in the way swapnilkumar created?

Jyoti
Jyoti Picture
19 Posts
back to top
  Rated 0 | Posted 15-11-2009

Yes we can create the objects of all those interfaces in java as shown by swapnilkumar.
Actually, its not creating an object of interface, but we can do it that way, its a nice trick.

Sagar
Sagar Picture
16 Posts
back to top
  Rated 0 | Posted 02-12-2009

We can not create object of interface. Basically Interface play role for contract between classes while designing wide area application.

Abhishek
Abhishek Picture
11 Posts
back to top
  Rated 0 | Posted 28-12-2009

Objects can only be created from new() function ....

try analysing the JVM HEAP , with JVM heap analyser tool , bet you won't find any object of an interface alteast


regards

ABee

Balamurali
Balamurali Picture
4 Posts
back to top
  Rated 0 | Posted 15-11-2010

No we cannot create directly objects to interface. But we can create reference variable to object

0
Vote Vote
Share  
 Type: Professional, please report if fun topic

Recent Activity in Forums

05-04-2012.
05-04-2012.
Kanchana posted a new forum topic in
27-02-2012.
11-02-2012.
Kanchana posted a new forum topic in
11-02-2012.