• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Declare and use interface

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Wilfried,
please show me by code what you mean by "please
name a package".
Thanks.
Thomas
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic