• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Can we create a Object of Interface type

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.....
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't create an object without method implementations. Anonymous inner classes allow you to do the following, though:

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ULF
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks cambpell and Rob
 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic