• Post Reply Bookmark Topic Watch Topic
  • New Topic

Protected Class In java  RSS feed

 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello friends And seniors, I am Novice in Java. I am Not very familiar with Protected Class, Where and how to use it. what is the core logic behind. Anyone One can show me the way to get clear on the logic.
I have one more queries including this, How to use Subclass of One Package into the Other Package Class and Subclass. Hope i gave Clear Clue what is the Question. although its a silly question for advanced learner, i got stuck on it.

Also, I tried one Program ,





[Added code tags - see UseCodeTags for details]

i am not able to rectify the problem what causes the error. please help ASAP.
Thanks In Advance and Have a great Programming.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhinesh. Welcome to the Ranch!

You didn't say what the error was (it's always helpful to give the exact error message), but I don't think the problem you're seeing is because it's a protected class. It's because B is an inner class. Which means it can only be instantiated in the context of an instance of the enclosing class.

If you were in a normal method of AccessB, you'd have that context (the AccessB object IS-AN AccessA object, and so can contain a B object). But you're calling it from a static method, which means you need to use a different way. Do you remember the way of creating an instance of an inner class?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what a protected class means: it is not accessible in a different package, unless the accessing class is a subclass of the protected class. See http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html for more details

Edited: Seeing the code now that Matthew has added code tags, I didn't even notice that is was an inner class. Just goes to show how useful those code tags are :-) So, UseCodeTags!
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B is an inner class to AccessA.


This does not work because " B() has protected access in Modifier1.AccessA.B"



You have access to the protected stuff (outside of the packege) from a part which is responsible for the implementation.
It works through "super" but not directly.

 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mathew and all of you(Please don't mind I haven't mentioned name though, i am yet learning to you use the forum too ...)

Thanks For resolving my query. From next time i will be careful to post an error too with a problem. How can a protected subclass of one package be accessed by Class or with a subclass. Will you help me with example as i understand easily with examples. also i want to know in detail about protected class(used on inner classes) with examples. where can i find.

Thanks and Happy programming
 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got one Query too.. on the above program ..

I am using following steps ... and the both working fine.. as it was mentioned by Seniors Like Ivan and Mathew here too...







I am not able to get.. What actually is happening here...
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things get mingled here: access modifiers and inner classes. Which part is not clear?

B is a (non.static) inner class of AccessA, so every B instance needs an enclosing instance of AccessA.
If we are inside a (non-static) method of AccessA, this instance is implicitly given, however in the static main method
we have to tell which AccessA instance should enclose the B instance to be created.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhinesh Kumar wrote:I got one Query too.. on the above program ..

I am using following steps ... and the both working fine.. as it was mentioned by Seniors Like Ivan and Mathew here too...





I am not able to get.. What actually is happening here...



That's how inner classes work. Inner classes needs to have an instance of an outer class. In the first case, you created an outer class assigned to a reference variable, then used it to create the inner class. In the second case, you created an outer class that you immediately used to create an instance of the inner class.

Henry
 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How About this??
Why it is running and there is no error in Object declaration Of B class of Package Modifier1 in Modifier2 package??








I am confused ... Help Me
 
Abhinesh Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Josef.. But What about the other one code i.e



This is also running fine there.. what's the difference between two.. as i think this one is used for static methods .




Ivan Jozsef Balazs wrote:Two things get mingled here: access modifiers and inner classes. Which part is not clear?

B is a (non.static) inner class of AccessA, so every B instance needs an enclosing instance of AccessA.
If we are inside a (non-static) method of AccessA, this instance is implicitly given, however in the static main method
we have to tell which AccessA instance should enclose the B instance to be created.

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhinesh Kumar wrote:



I am confused ... Help Me



In this case, the instance of the outer class is the "this" instance.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!