This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Five Lines of Code and have Christian Clausen on-line!
See this thread for details.
Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

creating new class in same package

 
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
 
Bartender
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.
 
Marshal
Posts: 69771
277
  • 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.
 
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.
 
What? What, what, what? What what tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic