File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor" Watch "constructor" New topic
Author

constructor

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

if the class is declared public and it default constructor is public....
then tell whether all of its constructors are also public....

then if i make the default constructor as protected then whther it will have any impact on in any class.

Note:- making the default constructor as protected will not give any compilaion error


SCJP6.0,My blog Ranchers from Delhi
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

I cannot understand your question as its not clear what you mean by default constructor. If a class is declared public, and if you don't provide a constructor, the compiler adds a no-arg constructor to the class which is also public. If you explicitly provide a constructor to the class, the constructor will have the accessibility you provide it. If you don't declare it public, it will be package visible i.e. you won't be able to instantiate the class from outside of its package.

If you declare the no-arg constructor as protected, then you'll be able to subclass the class but you won't be able to instantiate the class outside of its package...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

then if i make the default constructor as protected then whther it will have any impact on in any class.


This statement requires elaboration. Perhaps you can give us an example ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

there is difference between default constructor and the no-argument constructor that we provide
default constructor is the constructor that is provided by the compiler when we do not wrote any constructor for the class
and the access modifier of the default constructor is same as that of class (according to K & B)

then tell whether all of its constructors are also public....


not necessary
it depends on us what access modifier we give to them
if we do not give the access modifier then it is default and we can instantiate the class in the same package

then if i make the default constructor as protected then whther it will have any impact on in any class.

as per the explanation above there cannot be default constructor with protected access modifier (because we cannot write outer class as protected(we are talking about outer classes only))

I hope this helps
hth


SCJP 6 [86%] June 30th, 2010
OCPWCD [84%] March 26th, 2013
If you find any post useful, click the "plus one" sign on the right
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Prasad Kharkar wrote: the access modifier of the default constructor is same as that of class (according to K & B)



if the default constructor is made protected by me irrespective of whether the class is public then is it appropriate.............
i want to know appropriate not legal...........i know it is legal.............


for example





note:- above code will not give any compilation error...........
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

phil sohar wrote:
Prasad Kharkar wrote: the access modifier of the default constructor is same as that of class (according to K & B)



if the default constructor is made protected by me irrespective of whether the class is public then is it appropriate.............
i want to know appropriate not legal...........i know it is legal.............

for example

note:- above code will not give any compilation error...........

It's up to you! But it's legal!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Abimaran Kugathasan wrote:
It's up to you! But it's legal!



i know its legal..............but whether it is appropriate or not.......

legal and appropriate is having a big differnce..........

like
String s[];// is legal but not appropriate...bad practice to do this....
String[] s;//it is legal and appropriate to........
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

This depends upon the use case. If I want my class to be accessible everywhere, but I don't want anyone to instantiate it or sub-class it (like a singleton class), I'll make my constructor private. If I want my class to be subclassed outside of its package but not instantiated, I'll mark the constructor protected. If I want my class to be instantiated or subclassed anywhere, I'll mark the constructor public...
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

@Ankit
can you please elaborate what a singleTon class means?
I have never heard of that word before
what is it?
is it the word used in the industry?
because I haven't completed my BE yet so know nothing about the words that are frequently used in companies
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

can you please elaborate what a singleTon class means?

Did you do a google search on this, writing "singleton" on google and pressing "I'm Feeling Lucky" will get you to the right page...
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

I will not ask dumb questions before googling it
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 542
    
    4

As far as I understand it (please correct me if I'm wrong) is that a default constructor is the constructor inserted by the compiler if you do not specify one. You can add a no-arg constructor to your class, but that is never a default constructor.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Sometimes I just get confused with the amount of misnomers we have in Java.

Implicit or Default Constructor --> No arg Constructor with access level same as the class definition given by compiler

User-Defined-No-Arg-Constructor --> Defined By user writing the class & makes sure that there is no compiler version & here the user can mark it absolutely anything
public,protected, private (no definition makes it default access or package access)

I will be searching for other misnomers while I go through older posts.

Thanks for the questions, creativity thrives on provocation.


Regards,
Avishkar Nikale
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor