aspose file tools*
The moose likes Beginning Java and the fly likes why iam able to access defualt specifier field even within a sub-class ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why iam able to access defualt specifier field even within a sub-class ?" Watch "why iam able to access defualt specifier field even within a sub-class ?" New topic
Author

why iam able to access defualt specifier field even within a sub-class ?

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
There are three classes :-

Base.java


Derived.java



New.java



I know that the Protected access specifier fields can be accessed within same class, sub-class and same package and defualt access specifier fields can be accessed withing same class, and same package only.
But why i'm able to acess the default specifier fields with sub-class also ? (see the screen shot attached)
Am i doing something wrong here ? please tell me, it is very important to clear this basic.


[Download sub-class result.bmp] Download



Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

But why i'm able to acess the default specifier fields with sub-class also ?

If I were you, I'd wonder what would happen if the subclasses were in a different package than their parent.


[My Blog]
All roads lead to JavaRanch
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

indicate which line you are facing problem. your code looks fine to me. more over, I dont find any information from your attachment.
Ninad Kuchekar
Ranch Hand

Joined: Jan 05, 2010
Posts: 64
Vinod Vijay wrote: .....defualt access specifier fields can be accessed withing same class, and same package only.


I think you have already answered your question.


Don't walk as if you rule the world, walk as if you don't care who rules it...
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
indicate which line you are facing problem. your code looks fine to me. more over, I dont find any information from your attachment.




see this line, i'm able to read it very easily in the Derived class
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Also, your example doesn't illustrate what you are trying to check. In Derived#display, you shouldn't have Base base=new Base(); You should have :
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
hi Christophe Verré , i have got your point that
If I were you, I'd wonder what would happen if the subclasses were in a different package than their parent.


i tried with another class found that default fields cannot be accessed from a sub-class(residing in a different package that of its parent class) but can be accessed withtin a sub-class if both parent and child are in same package only.

Now my next question is then what is difference between Protected and Default. Don't you think so that both are behaving similarly ?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  16

Protected and default access have to very different purposes.

You need to understand how important the notion is that a class member can be accessed outside of a package. Default access makes a member visible to any class that is inside the same package *only*.
Protected access means that the member can be accessed outside of the package, even if it is only by subclasses. This difference is very great.

Think of a package as something that is yours. If there is something inside this package that the rest of the world can't see, then it will make it very easy for you to change this thing later on.
However, if there is something in your package that is either public or protected, then the rest of the world can see it, and it can write code that depends on it. From this point on you have a commitment to never change what this piece of your package does, or it will break the code of someone else who depends on it.

I believe that you should almost always favour composition over inheritance. I declare all my classes final, unless I design them explicitly to be extended.
In practice, for now I would suggest going by my rule of thumb:

Use private and default access as you please. You can never go wrong with them.
Never use protected. You need to have a very clear idea of what you are designing if you are going to need protected access.
Use public sparingly. Only if the outside world needs access to a piece of your package.

If you want a great example of good use of protected, take a look at FilterInputStream and all its subclasses. These classes were designed to be inherited, and are clearly documented.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
thanks Stephan van Hulst
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why iam able to access defualt specifier field even within a sub-class ?