aspose file tools*
The moose likes Beginning Java and the fly likes Declare and use interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Declare and use interface" Watch "Declare and use interface" New topic
Author

Declare and use interface

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Why does this not compile?


C:\Java\EigeneJavaProgramme>javac interface1.java
interface1.java:9: <identifier> expected
public String do();
^
interface1.java:9: = expected
public String do();
^
interface1.java:13: <identifier> expected
public String do() { return "A"; };
^
interface1.java:17: <identifier> expected
public String do() { return "B"; };
^
interface1.java:23: <identifier> expected
System.out.println( adoer.do() );
^
interface1.java:23: ')' expected
System.out.println( adoer.do() );
^
interface1.java:25: <identifier> expected
System.out.println( bdoer.do() );
^
interface1.java:25: ')' expected
System.out.println( bdoer.do() );
^
8 errors

Thanks for your answers.
Thomas
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
"do" is reserved word of the Java Language!
It is used in the do-while loop. So you can not use it as a method name!

W.
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
...and please name your package. You will have problems one day or the other if you continue working with anonymous packages.
W.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello Wilfried,
please show me by code what you mean by "please
name a package".
Thanks.
Thomas
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi Thomas
Here's a sample:
I have a "root" directory in C:\anthony\java\applications. I create a C:\anthony\java\applications\math folder and define a Vector.java in this folder with the following code:

Note the package declaration at the very top.
In C:\anthony\java\applications I define Test.java:

Note the import command at the very top. Since it is very possible that classes may share the same name, packages are used to differentiate them, in the same sense that different file paths distinguish different files with the same name. For example, there is a java.util.Date as well as a java.sql.Date class. In the example above we now have a math.Vector class in addition to the usual java.util.Vector.
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Thanks for the info. I changed method do() to dowrite() and it compiled.
I know that only Adoer or Bdoer have the method xy, so i think it is impossible
To say adoer.xy (=> Compiler Error).
But please explain the sense of „Doer adoer = new ADoer“, why don’t we say
„Adoer adoer = new Adoer“?


C:\Java\EigeneJavaProgramme>javac Interface1.java
Interface1.java:22: cannot resolve symbol
symbol : method xy ()
location: interface Doer
adoer.xy();
^
1 error
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
Hi,
You are doing the same mistake as in your famous thread Why can only "real" dog bark?"!
You are declaring "adoer" as a Doer, in the code line Doer adoer = new ADoer();.
And Doer does not how to xy().
You can write ADoer adoer = new ADoer(). There is no problem about that. Choosing between
ADoer adoer= new ADoer() and
Doer adoer = new ADoer() depends on your goals.

For exemple, let's say you are working with states.
You may have an interface State, which has a method nextState(). Then you have subclasses like IdleState and RunningState.
You may have a declaration like:
State myState; or
State []myStateCollection;
then myState can either be a IdleState or a RunningState in the course of your program.
If you'd declared:
IdleState myState;
you are done with it. myState is an IdleState, and will remain an IdleState all its life.

myStateCollection can contain either IdleState or RunningState, it does not have to care about what specific state it is, it is a State, that's all.
And a client using myStateCollection will only be aware of that: myStateCollection contains State's. Thus, it is only allowed to send to those instances, messages which are declared in the State interface.
If RunningState has a specialisation (like a stop method), you can not write
myStateCollection[0].stop() even if you know you have add only RunningState in it, because you do not meet the contract of the State interface.
W.
[ August 07, 2002: Message edited by: Wilfried LAURENT ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declare and use interface