aspose file tools*
The moose likes Beginning Java and the fly likes how to name and save the Television interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to name and save the Television interface " Watch "how to name and save the Television interface " New topic
Author

how to name and save the Television interface

Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167




Please tell me how to name and save the Television interface in the same folder as the other two Television class and SonyTelevision.My problem is how to save class television and interface television in the same folder or is there a better way.I am totally new to interface handling.

Thanks and Regards


Varuna Seneviratna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
What folders have you put the other classes in? Are you giving anything package declarations? You can't save two files of the same name in the same folder; neither Java nor the operating system will be happy about that.

I suggest you read the Java Tutorials; there are sections about interfaces and packages there. You can usually find examples by searching the Ranch; but I would never trust one person who posted in this thread
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Actually this is an exercise from the java.sun.com tutorials Object-oriented concepts Trail. I have put all the classes in same folder named Exercises.

Are you giving anything package declarations?


No Ritchie no package declartions or anything other than interfaces and classes


Varuna
[ December 12, 2008: Message edited by: Varuna Seneviratna ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Varuna Seneviratna:
Actually this is an exercise from the java.sun.com tutorials Object-oriented concepts Trail.


Well no; no, it's not. There's no class named SonyTelevision on Sun's web site. If you're trying to follow along with a tutorial but changing the classes as you go, that's certainly a fine thing to do, but maybe you should get the actual tutorial working first, and then make changes.


[Jess in Action][AskingGoodQuestions]
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Originally posted by Ernest Friedman-Hill:


Well no; no, it's not. There's no class named SonyTelevision on Sun's web site. If you're trying to follow along with a tutorial but changing the classes as you go, that's certainly a fine thing to do, but maybe you should get the actual tutorial working first, and then make changes.


Hello Ernest!
in the page http://java.sun.com/docs/books/tutorial/java/concepts/QandE/questions.html

there are two exercises I am trying to do the second one.It is true that there is no class named SonyTelevision, I created that name to do the exercise
I am stuck in saving and compiling the interface and the class, included in my first post

Regards Varuna
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

They don't mean for you to create the Television class, just the Television interface and the SonyTelevision class. Put "interface Television" in Television.java, and SonyTelevision in SonyTelevision.java and compile them both together with

javac -cp . SonyTelevision.java

If you do it this way, the compiler will find Television.java automatically!
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167


I compiled the above interface as Television.java and the Television.class was produced


I saved the above SonyTelevision class as SonyTelevision.java in the same directory as the interface Television.When I compiled SonyTelevision.java
using the command javac -cp SonyTelevision.java the output is
javac: no source files
Usage: javac <options> <source files>
use -help for a list of possible options


When compiling I was in the same directory as Television.java and SonyTelevision.java.Television.java contains the Television interface.

When I compiled SonyTelevision.java from TextPad menu Tools-->External Tools--->Compile Java then the out put is

C:\Java\SunJavaTutorialExcecises\Trails\LearningTheJavaLanguage\ObjectOrientedConcepts\Exercises\SonyTelevision.java:8: switchOff(int) in SonyTelevision cannot implement switchOff(int) in Television; attempting to assign weaker access privileges; was public
void switchOff(int newState){
^
C:\Java\SunJavaTutorialExcecises\Trails\LearningTheJavaLanguage\ObjectOrientedConcepts\Exercises\SonyTelevision.java:5: switchOn(int) in SonyTelevision cannot implement switchOn(int) in Television; attempting to assign weaker access privileges; was public
void switchOn(int newState){
^
2 errors

Tool completed with exit code 1




Thanks and regards Varuna
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Right it works when public is added to the methods of the SonyTelevision class.What is the theory behind it.The error message said
attempting to assign weaker access privileges


I just worked on a hunch why does a method of an interface when implemented has to be public.The class is accessing the method signature of the interface(I do not know whether I am right) in that case the method signature in the interface has to be public in order to accessed by the class

Varuna
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Varuna Seneviratna:
When I compiled SonyTelevision.java
using the command javac -cp SonyTelevision.java


Look closer at my post above; the command is actually

javac -cp . SonyTelevision.java
(javac space dash cp space dot space SonyTelevision.java)

You missed that all-important dot (period). That dot stands for the current directory, the place where your files are located.

As for your other question: all the methods of all interfaces are public, by definition. The methods that implement them must be public as well, always. That's just the rule.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
I included the dot the first time I compiled SonyTelevision.java the result was
javac -cp. SonyTelevision.java
javac: invalid flag: -cp.
Usage: javac <options> <source files>
use -help for a list of possible options




Varuna
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
Sory Sorry I had missed the spaces between the -cp dot and SonyTelevision and it should be




And It works!!!

Thanks

Varuna
[ December 12, 2008: Message edited by: Varuna Seneviratna ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Originally posted by Varuna Seneviratna:
Right it works when public is added to the methods of the SonyTelevision class.


The idea of an interface is that it defines which methods are available to any class using your SonyTelevision class. Any class anywhere will "expect" to say "myTelevision.turnOff(1);". If they are available to any class, they are public. You don't write "public" in the interface, but these methods are public. If you override or implement a method which is already public, it has to remain public.

By the way, the usual Java practice is for something with two states to be implemented as a boolean field, and for the methods to have names which may look slightly odd.I'll suggest an enhancement for your exercise: Change the names of the classes, then add a "make" field (String) and pass the make eg "Sony", "Hitachi" to a constructor.
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
If I change the class name to TelevisionSet.java how do I save it together with the interface TelevisionSet.java.My intent is to to create a TelevisionSet class and add a make filed of String type to it.


Varuna
Varuna Seneviratna
Ranch Hand

Joined: Jan 15, 2007
Posts: 167
I changed the class to Television and interface TelevisionSet then included a String type make field to the Television class
your views please......

Varuna
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
It is for you to decide what name to give your classes. If you want interface Television, class TelevisionSet implements Television, that looks all right to me
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to name and save the Television interface