aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 4 classes in one program..how to 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "4 classes in one program..how to " Watch "4 classes in one program..how to " New topic
Author

4 classes in one program..how to

Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
I have this program..,

interface Inter
{
public void amethod();
}
class NoInstance
{
}
class Instance implements Inter
{
public void amethod()
{
System.out.println("Print");
}
}
public class SubInstance extends Instance
{
public static void main(String[] args)
{
Instance inst = new Instance();
SubInstance subinst = new SubInstance();
NoInstance noinst = new NoInstance();
System.out.println((inst instanceof Inter));
System.out.println((subinst instanceof Inter));
System.out.println((noinst instanceof Inter));
}
}


I am relatively new to java. How do u compile the above program(i know it is javac file name), but the above code has got three classes like NoInstance, Instance and subInstance and interface.what should be the name of the program NoInstance.javaor Instance.java or subInstance.java
i tried with subInstance.java it is working., can u please clarify because all the three classes are different(i mean not inner classes)
can u please explain this
------------------
Reddy


Bhasker Reddy
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
you can have as many classes as you like in one source file but only one public class per source file
and it is the public class that you must use to save the file as the public classes name and to compile it
Bhasker Reddy
Ranch Hand

Joined: Jun 13, 2000
Posts: 176
can the classes above the public classes be private? i.e
in the above program, classes like Instance, no instance can
they be private
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
they can be anything you want them to be they are totally seperate classes the compiler will break them up in to 3 diffrent class files
Vivek Shrivastava
Ranch Hand

Joined: Jun 03, 2000
Posts: 277
Hi,
i think you can't apply 'private' or 'protected' access modifier to the top level classes.
any comment from java expert would be appreciated.
vivek
Suma Narayan
Ranch Hand

Joined: Apr 03, 2000
Posts: 136
Yes, Vivek you are right. Top level classes cannot be private or protected. They can either be public or the default(i.e no access modifier).
Suma
Fred Abbot
Ranch Hand

Joined: Jun 01, 2000
Posts: 300
protected cannot be used for classes only for methods
Adrian Ferreira
Ranch Hand

Joined: May 29, 2000
Posts: 118
And...
When we use default (also called "friendly") for one class, every class in the current directory can access this. It happens because Java assumes that all classes in the same directory belongs to the same package.
So, if we write code in the same directory, as many people does; for these classes, it doesn�t matter if we define them as friendly or as public.
Adrian
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Fred- you're thinking only of top-level classes. It's entirely possible to have a protected inner class. Also fields (member variables) can be protected as well - not just methods.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: 4 classes in one program..how to