Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
inner class in an interface
07-01-2008, 05:29 PM
Post: #1
inner class in an interface
Is it possible to have a class within an interface?

If it is possible , then is it possible to create an object of that class?
Find all posts by this user
Quote this message in a reply
07-01-2008, 05:40 PM
Post: #2
RE: inner class in an interface
In the Map interface , what is Map.Entry?

I have a code as below: Kindly explain :

Map hm=new HashMap();

//we put some keys and values in the map here using hm.put()

Set set=hm.entrySet(); //here what do we actually get?

Iterator i= set.iterator(); //why do we need this statement?

while(i.hasNext())
{
Map.Entry me=(Map.Entry)i.next(); //what happens here?

System.out.println(me.getKey() +" :");
System.out.println(me.getValue());
}
Find all posts by this user
Quote this message in a reply
07-01-2008, 05:42 PM
Post: #3
RE: inner class in an interface
We can have a class inside an interface. It will be just like any other inner class only. For eg:

[java]
public interface TestInterface {

// Inner class inside an interface
public class TestInnerClass{
public TestInnerClass(){
System.out.println("Inside TestInnerClass constructor.");
}

public String sayHello(String name){
return "Hello "+name;
}
}
}
[/java]

To create an object of this inner class use

[java]
TestInterface.TestInnerClass testInner = new TestInterface.TestInnerClass();
System.out.println(testInner.sayHello("Albin"));
[/java]
Visit this user's website Find all posts by this user
Quote this message in a reply
07-01-2008, 06:53 PM
Post: #4
RE: inner class in an interface
priyaguptan Wrote:In the Map interface , what is Map.Entry?

Map.Entry is an entry in the Map. ie, a key and value pair.

priyaguptan Wrote:Set set=hm.entrySet(); //here what do we actually get?
Here we get an object of Map.Entry which will consists of the key value pairs in this Map.

priyaguptan Wrote:Iterator i= set.iterator(); //why do we need this statement?

Iterator is used for iterating over a collection. The Set has a method which returns an Iterator. So when we call set.iterator() we are calling a method of Set interface.

priyaguptan Wrote:Map.Entry me=(Map.Entry)i.next(); //what happens here?

The next method of Iterator returns the next element in the collection. So when we call the next method on an iterator we will get the next element in that iterator
Visit this user's website Find all posts by this user
Quote this message in a reply
07-01-2008, 08:56 PM
Post: #5
RE: inner class in an interface
albinjoseph Wrote:
priyaguptan Wrote:In the Map interface , what is Map.Entry?

Map.Entry is an entry in the Map. ie, a key and value pair.

priyaguptan Wrote:Set set=hm.entrySet(); //here what do we actually get?
Here we get an object of Map.Entry which will consists of the key value pairs in this Map.

priyaguptan Wrote:Iterator i= set.iterator(); //why do we need this statement?

Iterator is used for iterating over a collection. The Set has a method which returns an Iterator. So when we call set.iterator() we are calling a method of Set interface.

priyaguptan Wrote:Map.Entry me=(Map.Entry)i.next(); //what happens here?

The next method of Iterator returns the next element in the collection. So when we call the next method on an iterator we will get the next element in that iterator

Thanks Albin
Find all posts by this user
Quote this message in a reply
08-13-2008, 05:06 PM
Post: #6
RE: inner class in an interface
Hi Everyone..
I also have one query on the same...
An inner class in an Interface...
here is a sample..
interface Test
{
class Inner_test
{}
}

class test_class implements Test
{
public static void main(String []arg)
{
Inner_test in_t = new Inner_test();
}
}

This works fine... but my query is how can it work??
we are making an object of non-static class in static context.
which should return compilation errror....

May be i am overconcerned because of which i have overlooked an imp concepts....
But what i think it can work only when Inner_test is implicitly static in the interface. but IS IT???

thanks in anticipation.
Find all posts by this user
Quote this message in a reply
08-13-2008, 05:58 PM (This post was last modified: 08-18-2008 03:39 AM by albinjoseph.)
Post: #7
RE: inner class in an interface
Your code returns a compile time error (at least for me in JDK 1.6 and Eclipse). The correct way to instantiate the inner class is

[java]
Test.Inner_test in_t = new Test.Inner_test();
[/java]

Assume that I have a class
[java]
class Abc{
int a =10;

public static void main(String[] args) {
Abc obj = new Abc();
}
}
[/java]

How can I create an object of a non static class from a static context?

Static means we are associating with a class. So any members that we are referring from within the static method also has to be static. But non static can be accessed by creating an object of the type.

Why can't we call a non static member from static method? its because non static exist only when we create an object. So anytime if we need a non static member to be accessed from static member we explicitly need to create an object.
Visit this user's website Find all posts by this user
Quote this message in a reply
08-13-2008, 06:25 PM
Post: #8
RE: inner class in an interface
The code that i have presented ... it compiles fine with jdk1.5

But my query is how that class is accessible from a static context??
plz let me know if anything required.
Find all posts by this user
Quote this message in a reply
08-16-2008, 03:31 PM
Post: #9
RE: inner class in an interface
Hi Every body outthere
waiting for the answer specially from u albinjoseph....-
Find all posts by this user
Quote this message in a reply
08-18-2008, 03:37 AM
Post: #10
RE: inner class in an interface
Abhinav, why do you think the Inner_test class is not accessible from main?

Here we are accessing a non static object (not a member) from static context which is perfectly fine.
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: