• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Interface instance?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can a interface have a instance? It doesn't compile when I tried in websphere. But I have seen mock questions that has instance of interface and it didn't report compilation error. Why?
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serena,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
An interface is 100% abstract class. We know that an abstract class cannot be instantiated, therefore the same applies to an interface.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally posted by Abdulla Mamuwala:
Hi
An interface is [b]100%
abstract class. We know that an abstract class cannot be instantiated, therefore the same applies to an interface.[/B]

An interface is not the same as an abstract class - they are quite different.
However, it is correct that you can not instantiate an interface.
If you look at some mock exam questions, though, you might be lead to believe that you can have instances of interfaces. Check out this example:

ActionListener is an interface and it appears that we're instantiating it. However, it's important to realize that we're not actually making a new instance of an interface, we're creating an anonymous object that implements ActionListener and that's certainly legal.
I hope that helps. If you have some specific examples that you're still confused about, post them here and I'll try to help.
Corey
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Serena:
Can a interface have a instance? It doesn't compile when I tried in websphere. But I have seen mock questions that has instance of interface and it didn't report compilation error. Why?


maybe u saw something like this


...in this case, u are actually creating an object of HashSet, which is an implementation of Set. (I am new to java but I think I am right)
 
Serena Zhou
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I didn't include code I am puzzled about. Here is one of the questions:
Given the following classes, which of the following will compile without error?

A. o1 = o2;
B. ob = b;
C. b = ob;
D. o1 =b;
Ans: A D
Another one I am having problem:
What will be the result of an attempt to compile and run the following code?

A. compile error at line 1
B. compile error at line 2
C. compile error at line 3
D. compile error at line 4
E. ClassCastException at runtime
F. Compiles and runs without errors
Ans: E
Thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Serena,
Hmmm. I'm not entirely sure what it is you're confused about. You can have a variable with an Interface type, such as Comparable, but you can't have an instance of an interface - that variable would have to reference some object that implements that interface. In your examples, that's exactly what you have.
That aspect of it is no different from having a variable with the type of an abstract class. We know that we can't instantiate the abstract class so the only use of the variable would be to reference a subclass of that abstract class.
Perhaps if you can point out the line(s) that are giving you trouble, I can be of more help.
Corey
 
Serena Zhou
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I understand it from your following explanation:
You can have a variable with an Interface type, such as Comparable, but you can't have an instance of an interface - that variable would have to reference some object that implements that interface.
Thank you very much!
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't 'C' also one of answers besides 'A' and 'D' for the first code snippet you have posted?
Correct me if I am wrong.
Regards,
Kiran
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also thinking that one of the answers for the first snippet is 'C'.
Can anyone explain whether 'C' is correct or not???

Thanks
kalpana
 
kalpana appala
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am also thinking that one of the answers for the first snippet is 'C'.
Can anyone explain whether 'C' is correct or not???

Thanks
kalpana
 
Serena Zhou
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, You are right. C is the right answer, too.
Sorry about that.
You all have sharp eyes.
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic