Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we create a Object of Interface type

 
prathamesh bandivadekar
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.....
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't create an object without method implementations. Anonymous inner classes allow you to do the following, though:

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hai ULF,
we can not instatiate the interface(do not have constructor)...

so how it is possible to create an object?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks ULF
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks cambpell and Rob
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone provide me the syntax to make an annonymous inner class extend a class or implement an interface.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic