• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Protected

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



a. Prints 8
b. Compile error
c. Runtime error
d. Prints nothing
e. None of the above.



Source : WHizlabs

Whats the answer with explanation?
1) What if the static wasn't there and we tried to access using instance of class pack in the test class?
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it will print 8, however note that if the "test" class dosen't extend the "pack" class you would get a compilation error telling you that the variable x2 is not visible.

if you remove the static modifier then you would get a compilation error. however the variable x2 of your super class is still visible since it has a protected attribute and you are extending the pack class so if you write a new method like this one



there should be no problems. but if you try to access to the x2 variable in the main method you get a compilation error telling you that its not possible to access to a non static variable from a static context.


(peace)

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if with NO OTHER CHANGES , i say p.x2 where p is an instance of class PACK in the subclass (in other package of course)

omar al kababji wrote:it will print 8, however note that if the "test" class dosen't extend the "pack" class you would get a compilation error telling you that the variable x2 is not visible.

if you remove the static modifier then you would get a compilation error. however the variable x2 of your super class is still visible since it has a protected attribute and you are extending the pack class so if you write a new method like this one



there should be no problems. but if you try to access to the x2 variable in the main method you get a compilation error telling you that its not possible to access to a non static variable from a static context.


(peace)


 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you don't make any other changes then you can get the value of x2 using an instance of that class such as p.x2, however the compiler will give you a warning telling you that you should access the x2 variable in a static way.

and also note that the compiler will replace it to pack.x2 ;)


 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1) What if the static wasn't there and we tried to access using instance of class pack in the test class?



It will give you compiler error. As p.x2 will try to access protected x2 non static member of class Pack, but outside the package, protected member behaves like private members, so you cannot call x2 on instance of Pack.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is confusing. As far as i know Protected means Available to subclasses even outside Package. If outside pacakage then we can access the protected members via inheritance. Thats what i am doing. Isn't it? Then why compiler error?

Punit Singh wrote:

1) What if the static wasn't there and we tried to access using instance of class pack in the test class?



It will give you compiler error. As p.x2 will try to access protected x2 non static member of class Pack, but outside the package, protected member behaves like private members, so you cannot call x2 on instance of Pack.

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is confusing. As far as i know Protected means Available to subclasses even outside Package. If outside pacakage then we can access the protected members via inheritance. Thats what i am doing. Isn't it? Then why compiler error?



This means you can use protected member using instance of subclass only, not the instance of superclass.
As protected member is inheritable, you can use thinking it as a member of subclass, but not a member of superclass if there is a package difference.

subclassinstance.protectedmember;//ok as subclass has this member via inheritence.
superclassinstance.protectedmember;//compiler error as accessing super class protected member outside the package.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great.
This clears my doubt.

Thanks !!!

Punit Singh wrote:

This is confusing. As far as i know Protected means Available to subclasses even outside Package. If outside pacakage then we can access the protected members via inheritance. Thats what i am doing. Isn't it? Then why compiler error?



This means you can use protected member using instance of subclass only, not the instance of superclass.
As protected member is inheritable, you can use thinking it as a member of subclass, but not a member of superclass if there is a package difference.

subclassinstance.protectedmember;//ok as subclass has this member via inheritence.
superclassinstance.protectedmember;//compiler error as accessing super class protected member outside the package.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Punit Singh wrote:

This is confusing. As far as i know Protected means Available to subclasses even outside Package. If outside pacakage then we can access the protected members via inheritance. Thats what i am doing. Isn't it? Then why compiler error?



This means you can use protected member using instance of subclass only, not the instance of superclass.
As protected member is inheritable, you can use thinking it as a member of subclass, but not a member of superclass if there is a package difference.

subclassinstance.protectedmember;//ok as subclass has this member via inheritence.
superclassinstance.protectedmember;//compiler error as accessing super class protected member outside the package.



@Punit, in the given program above, pack is an instance of SuperClass, then how compiler is not complaining when we are trying to access x2 from subclass using reference of SuperClass?


 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pack is an instance of SuperClass


Hope it was typo error. If not, Pack is a class name.And p is the instance.

then how compiler is not complaining when we are trying to access x2 from subclass using reference of SuperClass?



X2 is static. So even without the instance of the class, we can acccess x2 ( provided ,it is visible).
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic