*
The moose likes Beginning Java and the fly likes where will be the method implementation present for  an interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "where will be the method implementation present for  an interface?" Watch "where will be the method implementation present for  an interface?" New topic
Author

where will be the method implementation present for an interface?

Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
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

Joined: Sep 16, 2008
Posts: 241
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:



SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
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

Joined: May 19, 2001
Posts: 53
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

Joined: Jun 25, 2010
Posts: 76
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

Anand --

I was just explaining this to someone else yesterday -- check out my post in this thread. Hope this helps.


[Jess in Action][AskingGoodQuestions]
Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
not clear from my point of view

expecting simple and brief answer.

thanks for your kindly reply.
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
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.

SCJP, SCWCD
Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
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

Joined: Mar 24, 2008
Posts: 3816

Anand, Please UseCodeTags when posting code in the forums.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
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

Joined: Jun 25, 2010
Posts: 76
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

Joined: Jun 22, 2009
Posts: 31
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

Joined: Jun 25, 2010
Posts: 76
ok then what you say about

new Enumeration<E>()

you have not explained
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Garik already said that's an anonymous inner class. Look it up,


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
ok thanks.i will analyse and reply
Anand Sivathanu
Ranch Hand

Joined: Jun 25, 2010
Posts: 76
i have understood.thanks for your co operation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37913
    
  22
And please find the code button; your code is very difficult to read without code tags.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: where will be the method implementation present for an interface?
 
Similar Threads
Enumeration...
Did you see this ?StringTokenizer
Enumeration Interface Implementation
Implementatoion of hasMoreElemnts()???
interface