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

Package

 
Bindu San
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am writing a class "Date" in package "A".
And writing a class "Employee" in package "A"
(i am creating an object of DATE in class EMPLOYEE)
And then, i am inheriting the class "Employee" to a subclass "Boss" . Now, what classes are supposed to be placed in package "A" and does Boss needs an import statement ?
And, when we create a package, do we have to create a directory before hand ???
I appreciate some help .
Thankyou.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bindu San:
Now, what classes are supposed to be placed in package "A" and does Boss needs an import statement ?

Well, you can place whichever classes you'd like inside package A. Packages are really designed to help you organize your classes. They can, however, be used to help set up accessibility to members.
The rule of thumb about imports is that, unless the class you're trying to get at is in the same package as the one the class that's trying to get at it is in, you'll need to designate the full name of the class. Take a look at this example:

The code above is illegal because the compiler can't find a class called Parent within package B (which is where class Child is). You have two options to resolve this - you can use an import statement or you can provide the fully qualified name of class Parent. If you replace the last line in the above example with either of these options, you'll be fine.

I hope this helps you understand how to deal with packages. If you have more questions, please ask.
Corey
 
Bindu San
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Corey ,
Thats a great help. But what i did is , i placed all my classes in the same package. If this is the case, will my import statement give any problem ???
Coz, i declared classed A and B in the same package. And B extends A. In the class B, i used an import statement,. Is this necessary ???
Still my program is in trouble...
Plz help me out
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
If I'm understanding you correctly...
No, you don't need to import the package that your class is already in. So:
package a;
import a.*; // unnecessary
class A {
}
Let me recommend Controlling Access To Members Of A Class as a good source of information straight from the horse's mouth.
Good Luck,
-Dirk Schreckmann
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic