Help coderanch get a
new server
by contributing to the fundraiser
  • 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 constructor

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello fellows,
I have some misunderstanding related to the protected access modifier, maybe you can show some light here:



What I don't understand is why A's constructor is not visible in B class, although A() is protected and B is a subclass of A?

Protected gives access through inheritance, and since constructors are not inherited. maybe this has something to do with this...

Best regards,
Valentin
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,

You are right, members with Protected access are visible to class's subclasses in different package through inheritence.
Since constructor is not a member of a class it cannot be inherited into the subclasses. Which gives us the reason why we cannot instantiate a superclass constructor with protected access in the subclass which is in different package.

Thanks,
Rancy
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

There is one other case wherein we cannot asscess the instance variables in the super class using the parent class reference when that instance variable is marked protected. I giess this has got somehting to do with the protected constrcutor. Anyone on this??
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jothi Shankar Kumar Sankararaj:
Hi,

There is one other case wherein we cannot asscess the instance variables in the super class using the parent class reference when that instance variable is marked protected. I giess this has got somehting to do with the protected constrcutor. Anyone on this??



protected is visible by the class in the package or through inheritance. You cannot access a protected variable through parent class instance if the subclass is not in the same package. It has nothing to do with constructor.
 
This tiny ad is wafer thin:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic