• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to access protected inner class

 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code below is a class in one package



now my question is , how to create an object of the inner class which has access type protected ( as seen in above example ) in some other package ? I tried code as seen below but didn't worked , please help.....



this also didn't worked



please solve my doubt
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, let's see if you can solve it. It usually helps if you read the error message. For example, here is the code I used to test:


When I tried to compile Maker, this is the error message:


The error message tells you that Inner() [That's the Constructor for the Inner class] has protected access, which means, since Maker does not extend Inner, it can not access Inner() [and therefore can't make a new instance of the Inner class].

There are two solutions to this problem, one involves changing Inner slightly, the other involves adding a new class (inside Maker). Can you come up with the solutions?
 
Rahul Shivsharan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i got it , please go though it


Parent class in a package which has the inner class with access type protected



the child class in other package



the above code compiles and runs , but what about the first solution you wrote about modifying the inner class which is in parent class , this i didn't understood
can you please elaborate on this hint
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add a public constructor to the inner class.

 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hardly a beginning question. Moving thread.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic