Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

4 classes in one program..how to

 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Fred Abbot
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
protected cannot be used for classes only for methods
 
Adrian Ferreira
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic