Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

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

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
Filename: sub-class-result.bmp
Description: Sub-class result
File size: 846 Kbytes
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
indicate which line you are facing problem. your code looks fine to me. more over, I dont find any information from your attachment.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?
 
Saloon Keeper
Posts: 13391
295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Stephan van Hulst
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic