• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Please Help Me To Clear My Doubt?

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

Under what circumstances, we can declare constructor of a class as a protected?



According to my knowledge, constructor never participate in inheritance, then under what circumstances we have to declare constructor as protected?

Thanks in advance,

Regards,
Gopal
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure , but If you try to access the class from different package , the class will be accessible but it will not create the object as constructor declared is protected.

check for this condition.
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to my knowledge, constructor never participate in inheritance, then under what circumstances we have to declare constructor as protected?



What do you mean by "never participate in inheritance"? While it is true that a constructor can't be overridden, but it can be called from the subclass. Hence, you need to define access to it -- be it from the subclass or any class that uses it.

Henry
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by this

under what circumstances we have to declare constructor as protected?



You are not forced to do so...

however, if you declare all the constructors of a class as protected, then you can sub-class it in different packages, but you cannot create instances of the class outside the package....
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When this can be used?
I think in the case you *prefer* it to be subclassed, so abstract is too much. Think about a colleague who hates almost empty subclasses and does not want to extend your abstract Animal-class; you change it this way, explaining public was too much with good reasons, and go on with your work.

Probably you'll only use private and default and coderanch. Java-engineers did not make the language on usage-statistics, but only left parts out which could lead to problems (such as multiple extension). So the question is the reverse: why wouldn't it be possible to make a class-instantiation protected?
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if the constructor of a class is declared as protected, objects of that class cannot be created outside its package.
And constructors will play a role in inheritance also. Only thing is that constructors are not inherited. A call to the super class constructor will be added in the constructor of the subclass by the compiler if the default constructor is there otherwise you will have to add the same.(ofcourse if there is a call to this(), call to super class constructor is not required.)
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing added to SRILATHA


A call to the super class constructor will be added in the constructor of the subclass by the compiler if the default constructor is there otherwise you will have to add the same.(ofcourse if there is a call to this(), call to super class constructor is not required.)



The compiler will add a call to the default constructor of the superclass, when it finds a constructor. ie, it will add a super(), as the first line of the constructor.

when a call to this() is given, the compiler will go for the default constructor of the same class. since it can't contain a call to this() again(Recursive Contructor invocation error), the first line added will be super(). so it will perform the functionality implicitly.
 
V. Potluri
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks a lot for each of you for spending your precious time for clearing my doubt.

Have a wonderful day ahead!

Regards,
Gopal
 
reply
    Bookmark Topic Watch Topic
  • New Topic