Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please clear my doubt

 
Anupam Mittal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can someone please explian me why it is not working??

***************************************************************
public interface FirstInterface {

String getName();

}


public class StaticTest implements FirstInterface {

public static void helloWorld()
{

System.out.println("hello world");
}

public String getName() {

return "something";
}

public void helloIndia()
{

System.out.println("helloIndia");
}
public static void main(String[] args) {
FirstInterface a=new StaticTest();
a.helloIndia();

}
}

***********************************************************************

This works for class and subclass but not for interface??
Please clear my doubt.

Thanks in advance

Anupam
 
Vijay Kokatnur
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anupam Mittal:


public interface FirstInterface {
String getName();
}

}



Got it? Your interface doesn't have helloIndia method. It gives you compilation error!!!
[ September 11, 2006: Message edited by: Vijay Kokatnur ]
 
Anupam Mittal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes we will get compilation error but i was just trying to figure out the reasons why it is not allowed...but i am not able to ascertain the reasons... can somebody help??

Thanks

Anupam
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FirstInterface a=new StaticTest();

You have an FirstInterface reference. When you do this you only have access to the defined interface methods. There are benefits to creating a reference like this, but like in everything else there are negative tradeoffs.

Try this and see if you can access any method in StaticTest:

Object o = new StaticTest();
[ September 11, 2006: Message edited by: Robert Hill ]
 
Anupam Mittal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

I dont have any problem with getName()it is executing fine and since both interface and class in same pacakage public or default modifer does not matter.

Problem is helloIndia() method i was in impression that it shd get executed. See the follwoing lines

FirstInterface a=new StaticTest();
a.helloIndia();

So a is the reference of type FirstInterface type and since StaticTest class implements FirstInterface variable a should be able to hold the reference of StaticTest class but when i try to make a call to method helloIndia() which is written in StaticTest my program is not compiling... trying to figure out why???

please help

Thanks

Anupam
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already answered it, perhaps it is too vague, so I will try again.

The only methods a reference can see are those of its own type and parents. FirstInterface, in this case can access its own method and the methods of Object. That is it. FirstInterface knows nothing about any classes that could implement it, and why should it? How many classes could potentially implement it?

What you are doing is called polymorphism, I suggest you read more on it, and its implementation in Java.
[ September 11, 2006: Message edited by: Robert Hill ]
 
Anupam Mittal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it guys... :-) I appreciate Robert for his help.
This clears one of my fundamental doubt.
Thanks

Anupam
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, cowboys!

Anupam assumed:
This works for class and subclass but not for interface??


No doesn't. Won't work for class, won't work for interface.

Consider this for classes:

Now, if you have somewhere

A polyVar = new B();


Then object type of polyVar is B, but the methods you can invoke depends only on the reference type, A in this example. Only Methods of class A (and superclasses) can be called on polyVar.

polyVar.aMethod(); is fine but
polyVar.bMethod(); does not compile.



Yours,
Bu.


ps: don't mix up polyVar with Bolivar since the south american nation is called Bolivia and not Polyvia
 
Anupam Mittal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Burkhard for your elobrate reply :-)

Anupam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic