Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

creating new class in same package

 
nirjari patel
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?

Thanks
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic