aspose file tools*
The moose likes Beginning Java and the fly likes Please clear my doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Please clear my doubt" Watch "Please clear my doubt" New topic
Author

Please clear my doubt

Anupam Mittal
Greenhorn

Joined: Aug 25, 2006
Posts: 8
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

Joined: Aug 10, 2006
Posts: 2
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

Joined: Aug 25, 2006
Posts: 8
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

Joined: Feb 24, 2006
Posts: 94
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

Joined: Aug 25, 2006
Posts: 8
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

Joined: Feb 24, 2006
Posts: 94
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

Joined: Aug 25, 2006
Posts: 8
Got it guys... :-) I appreciate Robert for his help.
This clears one of my fundamental doubt.
Thanks

Anupam
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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


all events occur in real time
Anupam Mittal
Greenhorn

Joined: Aug 25, 2006
Posts: 8
Thanks Burkhard for your elobrate reply :-)

Anupam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please clear my doubt
 
Similar Threads
static modifier doubt
tthread doubt
Looking for explanation
Just a doubt on static methods
doubt in polymorphism