aspose file tools*
The moose likes Beginning Java and the fly likes Can we create a Object of Interface type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Can we create a Object of Interface type" Watch "Can we create a Object of Interface type" New topic
Author

Can we create a Object of Interface type

prathamesh bandivadekar
Ranch Hand

Joined: May 22, 2008
Posts: 43
Hi there,

i want to ask a simple question?

I know many of you will laugh on me...

but cant we really create the object of interface?

i m not asking to implement the interface and then create the object of that class.....

i m asking direct object creation of interface type.....


Prathamesh M.B.<br />Java....only.....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42291
    
  64
You can't create an object without method implementations. Anonymous inner classes allow you to do the following, though:



Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hai ULF,
we can not instatiate the interface(do not have constructor)...

so how it is possible to create an object?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42291
    
  64
we can not instatiate the interface

Correct. What's happening here is that an object gets instantiated that implements the interface.

You may want to read the Java Language Specification (or your favorite Java book) on the subject of "anonymous inner classes".
[ June 02, 2008: Message edited by: Ulf Dittmer ]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks ULF
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hai Ulf,

i read some url regarding this...but still i could not get the point..

please can you explain object of interface
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42291
    
  64
Why don't know you try to explain what you do understand about it, and we'll chime in if you get it wrong. Often one understands things better if one needs to explain them to others, or write them down.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

sorry ulf,

what i know about annonymous inner class
----------------------------------------------

1.do not have name

2.do not have constructor

---------------------------------------------

but my doubt is how below one is working?(because you can not instantiate interface)

------------------------------------------------------


MyInterface myIntfObj = new MyInterface() {//it is a interface .how this?
public void myMethod() {
....
}
};

-------------------------------------------------------


sorry..if i ask again worng question

Thanks & Regards,
seetharaman.v
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Ulf has told you: you are converting the interface to an inner class, which you haven't given a name to (hence anonymous). You have to implement all the interface's methods, then you have a class, then you create an object from it all in one statement.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

Originally posted by seetharaman venkatasamy:
1.do not have name

Technically each class has a name, although for anonymous classes that name is not known at compile time. You can see those classes in the same folder as your other class files - they end with $1, $2 etc.

2.do not have constructor

Every class has a constructor, even anonymous classes. With anonymous classes you can't define your own constructors though; instead, it "inherits" all constructors of its parent class, and then redirects to that parent constructor. It's similar to the following:

Now you may thing, "but interfaces do not have constructors!"
Well, you're 100% correct. However, the parent class in that case is Object, and that certainly has a constructor. Another similarity example:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks cambpell and Rob
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

Can anyone provide me the syntax to make an annonymous inner class extend a class or implement an interface.


Thanks & Regards
Sidharth Pallai
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by Sidharth Pallai:
Can anyone provide me the syntax to make an annonymous inner class extend a class or implement an interface.
They already have. Read seetharaman venkatasamy's post dated 2nd June 13:15 and he has provided an example of a named object reated as an instance of an anonymous class which implements the MyInterface interface, with a single method.
[ June 24, 2008: Message edited by: Campbell Ritchie ]
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

Originally posted by Campbell Ritchie:
They already have. Read seetharaman venkatasamy's post dated 2nd June 13:15 and he has provided an example of a named object reated as an instance of an anonymous class which implements the MyInterface interface, with a single method.

[ June 24, 2008: Message edited by: Campbell Ritchie ]


Thank you Campbell.
But does an annonymous inner class can use implement keyword to implement an interface or extend to inherit a super
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

Originally posted by Sidharth Pallai:
But does an annonymous inner class can use implement keyword to implement an interface or extend to inherit a super

No, you cannot use the 'implements' or 'extends' keywords with an anonymous inner class.

The anonymous inner class is an implementation of the interface that you specify - it can't implement or extend any other interface or class.
[ June 24, 2008: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

Thank you Jesper.
Is there any specific reason or circumstance in which one can go for plain old annonymous way of implementing interfaces.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

1) Reuse. If you need the same anonymous class in two places it's better to make it a named inner class, or perhaps even a method local class.
2) If you need to extend a class AND implement an interface, or implement multiple interfaces, you will need to create a class for it.
3) Just because you don't like anonymous classes. There is nothing wrong with using inner classes or method local classes instead of anonymous classes. You'll only have to think of names for those.
[ June 24, 2008: Message edited by: Rob Prime ]
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Rob Spoor wrote:
Originally posted by seetharaman venkatasamy:
1.do not have name

Technically each class has a name, although for anonymous classes that name is not known at compile time. You can see those classes in the same folder as your other class files - they end with $1, $2 etc.

2.do not have constructor

Every class has a constructor, even anonymous classes. With anonymous classes you can't define your own constructors though; instead, it "inherits" all constructors of its parent class, and then redirects to that parent constructor. It's similar to the following:

Now you may thing, "but interfaces do not have constructors!"
Well, you're 100% correct. However, the parent class in that case is Object, and that certainly has a constructor. Another similarity example:


Constructor inheritance logic is bizarre here for me ,but as you said that anonymous classes will always have some name so i believe there should be a default constructor of anonymous class that's used to instantiate its object.


Tell the difficulties that i am difficult.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
An anomymous class has the same constructors as its superclass. If you create it from an interface which has no constructor, the compiler will provide it with a default constructor.
 
 
subject: Can we create a Object of Interface type