• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with access modifiers  RSS feed

 
Sachin Tripathi
Ranch Hand
Posts: 368
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
 
Marcus Biel
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.
 
Tapas Chand
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
 
Darryl Burke
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.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • 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?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!