This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with access modifiers  RSS feed

 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have learnt that by declaring protected members it can be accessed as package -private +by subclass from outer package


when i compile both .java files both got compiled
when i run Child.class
it gives error as:
Could not load or find main class p1.child
 
Ranch Hand
Posts: 51
IntelliJ IDE Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your classes are both in the same package. importing p1.*; does not compile.
Remove the import and it will work.

p.s.: protected is a complex beast. Try to avoid it. Try to avoid inheritance alltogether.
 
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from the points by Marcus you are good to go with this code. There is no syntax errors.
I think you are executing wrong command.
If I am not wrong you are executing
java p1.child
It should be
java p1.Child
Note the change in case
 
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Marcus Biel wrote:Your classes are both in the same package. importing p1.*; does not compile.


Did you take the time to test that before posting?

Sachin Tripathi wrote:when i compile both .java files both got compiled.



Marcus Biel wrote:p.s.: protected is a complex beast. Try to avoid it. Try to avoid inheritance alltogether.


Nope. Don't. Don't.
 
Marshal
Posts: 58388
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please indent your code correctly; unindented code is hard to read.

Why are you importing things in the same package?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!