• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

constructor

 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 9708
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
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...
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...........
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 9708
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
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 9708
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

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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will not ask dumb questions before googling it
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic