wood burning stoves 2.0
The moose likes Beginning Java and the fly likes creating new class in same package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "creating new class in same package" Watch "creating new class in same package" New topic

creating new class in same package

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 386
I have Class1 in package1 which is public. Now I want to create Class2 in package1. Do I need to import package1 in Class2 ?

Class2 in package1 is protected. I am creating Class3 in package1. I can not instantiate Class2 in Class3. Can I extend Class2 in package2 (public class Class3 extends Class2) ?
I have Class4 with default access specifier (in package1class Class4). Can this class be extended by Class3, which is in package2 ?

Class1 has an integer field as protected, "protected int i". Can I use this field in Class3 ?

There is a private String str in class1. Can I use this field in the main() of Class1 with the referance of Class1 ? (Class1 c1 = new Class1(); c1.str;) ?
OR do I need to access str through a public method of Class1 even in the main() of Class1 ? I understand that if I am accessing str in a different class, then I have to use method of Class1 to access private str, but does it apply to the main() of Class1 also ?

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

There is a real easy way to figure that out: try it. Seriously if you are unsure of something, try it. You'll learn much more from it then from getting the answer from us.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 44494
nirjari patel wrote: . . . Class2 in package1 is protected. . . .
You can't have protected top-level classes.

I think if you get a pencil and paper and write down all the names of your packages and which classes are in those packages, you will understand the problem a lot better.
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
nirjari patel , your question is too confusing. Don't ask a list of questions at a time. And it will help you a lot if you use paper and pencil to solve such questions.

I agree. Here's the link: http://aspose.com/file-tools
subject: creating new class in same package
It's not a secret anymore!