• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

where will be the method implementation present for an interface?

 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Let us see Enumeration,which is an interface.
We already know that interface have only declaration.no implementation.
So Enumeration interface have nextElement().
My question is
where will be the nextElement() implementation present?

thanks in advance
Anand Sivathanu
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implementation can be present in an abstract class implementing the interface, but it must be present in the first non abstract class that implements the interface or in any way inherits the abstract method declared in the interface. Hope this example will be more clarifying:

 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let us see this example

here vector's elements() will return a enumeration
then see e.nextElement() which give each object during iteration.
We already know that Enumeration is an interface ,
here how the nextElement() works
How it is possible?
import java.util.*;
class testEnumeration
{
public static void main(String[] args)
{
Vector v = new Vector();
v.add("a");
v.add("b");
v.add("c");
v.add("d");
v.add("e");
Enumeration e = v.elements();
while (e.hasMoreElements())
{
String str = (String)e.nextElement();
System.out.println("str=====>"+str);
}
}
}
 
Jimi Svedenholm
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand Sivathanu wrote:let us see this example

here vector's elements() will return a enumeration
then see e.nextElement() which give each object during iteration.
We already know that Enumeration is an interface ,
here how the nextElement() works
How it is possible?




When you use the method elements() what you get is not an instance of the Enumeration interface, since interfaces can't be instantiated. Instead you get some class that *implements* the Enumeration interface. You don't get to know what class that is (unless you look at the source code of the Vector class), but you don't really need to know that. All you need to know is that the object returned provides all the functionality declared by the Enumeration interface.
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
please try this

file name testanimal.java
interface testanimal
{
public void bark();
}

file name seeanimal.java
class seeanimal
{
public testanimal next()
{
return new testanimal()
{
public void bark()
{
System.out.println("wow wow");
}
};
}
public static void main(String[] args)
{
seeanimal see = new seeanimal();
testanimal t = see.next();
t.bark();
}
}

the above two files are in same package.

now you compile seeanimal.java
run seeanimal

output will be
wow wow

here you can see the next() of seeanimal class

return new testanimal()
{
public void bark()
{
System.out.println("wow wow");
}
};

new testanimal means new object.how can interface have object?

compile the above files and reply briefly
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand --

I was just explaining this to someone else yesterday -- check out my post in this thread. Hope this helps.
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not clear from my point of view

expecting simple and brief answer.

thanks for your kindly reply.
 
Garik Ustinov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your code you're using a so called anonymous inner class. This is a special case when you are creating an implementation of some interface without giving it a name.
i.e. you could have created a 'proper' implementation

instead you're creating the same implementation without creating a separate class named Dog. But this is not an instance of the interface! As the guys told you interface cannot be instantiated, it is just an instance of nameless (anonymous) class which implements your interface.
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you said iam creating anonymous inner class
i.e new testanimal() ,if iam not wrong

here testanimal is interface.as i declared earlier in the above conversation.
so you are telling that a new object is created(anonymous)

my doubt is anonymous class of which type?
testanimal?

here you can see the next() of seeanimal class

return new testanimal()
{
public void bark()
{
System.out.println("wow wow");
}
};

 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anand, Please UseCodeTags when posting code in the forums.
 
Garik Ustinov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my doubt is anonymous class of which type?
testanimal?

not really. if you call getClass() on this object you will see that JVM would assign it a type name consisting of a class which 'owns' this object and some number. In your case I guess it will be something like seeanimal$1.
This is a disadvantage of anonymous class, it's type is not intended for referencing elsewhere. This is just a quick and dirty way of instantiating interfaces, if you need a proper type, use normal implementation in a separate file.
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

Have a look at this

/**
* Returns an enumeration on the elements of this vector. The results of the
* enumeration may be affected if the contents of this vector is modified.
* @return an enumeration of the elements of this vector.
* @see #elementAt
* @see Enumeration
*/
public Enumeration<E> elements()
{
return new Enumeration<E>()
{
int pos = 0;
public boolean hasMoreElements()
{
return pos < elementCount;
}

@SuppressWarnings("unchecked")
public E nextElement()
{
synchronized (Vector.this)
{
if (pos < elementCount)
{
return (E) elementData[pos++];
}
}
throw new NoSuchElementException();
}
};
}

the above is the elements() method of vector class which return Enumeration

for reference you can see docjar.com (Class Vector)

here you can see return new Enumeration<E>().

Please define it clearly

you quoted that "This is just a quick and dirty way of instantiating interfaces".
then why the java creator coded like that?

Please define return new Enumeration<E>()
 
Garik Ustinov
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright, I didn't mean to say that using anonymous classes is evil. After all this is a language feature and sometimes it's quite useful. For instance, in Swing you have to deal with event handlers and often creating a separate named class consisting of two lines of code is a clear waste.
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok then what you say about

new Enumeration<E>()

you have not explained
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Garik already said that's an anonymous inner class. Look it up,
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks.i will analyse and reply
 
Anand Sivathanu
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have understood.thanks for your co operation
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please find the code button; your code is very difficult to read without code tags.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic