• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

creating new class in same package

 
Ranch Hand
Posts: 386
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 80138
418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Why is the word "abbreviation" so long? And this ad is so short?
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic