• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

protected constructor

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

Can anybody give me the rules for protected constructor? If possible with an example pls.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sudha

Please explain what you want to know about protected constructor so I can explain you.


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

If constructor of your class is protected, no classes outside its package can instantiate object of your class except the subclasses regardless of the packages can access it.

One more thing to add "Provided that your class has public accessibility".

Is that information enough of now? Or you want examples!



Regards,
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
Hi Sudha,

If constructor of your class is protected, no classes outside its package can instantiate object of your class except the subclasses regardless of the packages.


Is that information enough of now? Or you want examples!



Regards,
cmbhatt



I do not agree with you cmbhatt.
Protected access specifier has nothing to do with package (I think you are talking about default access specifier), but only the classes extending this class can access this.

visible to only sub classes
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I do not agree with you cmbhatt.
Protected access specifier has nothing to do with package (I think you are talking about default access specifier), but only the classes extending this class can access this.

visible to only sub classes



Hi Rahul,

I was only talking about protected access specifier.
If class has default access (No access modifier used) and constructor is protected; only the classes in the same package can instantiate that class.
Outside that package the constructor (set to protected is not visible).

You can check it out well.

Is still something there that I miss?
Please suggest!


Regards,
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really very sorry Chandra Bhatt and sudha javvadi .I am wrong about the concept of protected.

I was under the impression that protected only visible to classes sub classing it , but the fact is it is also visible to all the other classes in the same package (with or without sub classing).

Thanks for clarifying.
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chandra,

My understanding is as follows:

if a class in another package inherits the class with protected constructor can instantiate the protected constructor class.I think iam confusing you. ok following is the code.

package food;
public class Construct
{
public int i;
protected Construct()
{
i=10;
}
}

import food.*; // This is in another package
class ConstTest extends Construct
{
public static void main(String args[])
{
Construct c = new Construct();
System.out.println("Value of i:" + c.i);
}
}

I am getting the error as : Cosntruct() has the protected access in food.Construct. If we cannot apply protected there then what is the use of protected constructor. Can you give an example of protected constructor or else just correct the code above.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate your attempt!



One liner: If a class has protected constructor, only and only the classes which come under the same package can instantiate it

Another fact:
A class in another package can extend the class(the class which has protected constructor) provided that it has public access of-course but can't instantiate it.

Outside the package the protected constructor is not visible to anybody;


If we cannot apply protected there then what is the use of protected constructor.



Yeah, benefit, you don't want any other class except classes that are in your package to instantiate your class and at the same time you want that class that is outside the package could extend it.

See the code below:



Now you got the point?

Hey Sudha, don't you think "This protected is too much!"

Regards,
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]
 
sudha javvadi
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now i got it! thank you.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,

It was doubtful to me also.

Thanks for clarifying,

Raghav
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic