Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages!!

 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,
I have a series of questions on packages....I am trying out certain things practically with my compiler and couldn't proceed much..help me out...Although, I have done some decent java projects, i haven't encountered much with PACKAGES or else IDE which I am will be using will take careof all abt. packages....okay, enuf of background stories;;;; lemme COME TO THE POINT...!!
i want to create 2 java files each under separate PACKAGES and test the access of protected/default & public members from one package to the other!! (both as subclass and as a non-subclass)..
I'm testing the progs. on a UNIX based system. I did create files with the following source code::
file 1::
package parent;
class protect1
{
void test1()
{
System.out.println("AM IN PARENT - protect1.java");
}
}
file2::
package child;
import parent.*;
public class protect2 extends protect1
{
public static void main(String[] args)
{
protect2 p2 = new protect2();
p2.test2();
}
void test2()
{
test1();
}
}
Should I necessarily need to create separate directories for each of the package or how should I go about executing this.....HELP OUT !!....
Any help is APPRECIATED !!
~ Shalini
 
dennis zined
Ranch Hand
Posts: 330
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. create "parent" directory
2. class protect1 filename should be "protect1.java" (you probably know this already) and then place this inside the "parent" directory you created in 1.
3. create "child" directory (outside parent directory)
4. class protect2 filename should be "protect2.java" and then place this inside the "child" directory you created in 3.
5. from outside the two directories, compile both classes:
a. javac parent/protect1.java
b. javac child/protect2.java
6. run protect2...: java child.protect2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic