This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
Joined: Mar 08, 2001
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
Joined: Jul 13, 2001
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.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 ]