• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is it a constructor call without the new keyword?  RSS feed

 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We all know that a constructor is a code that runs when we say new on a class type. Now refer the following piece of code -



AND




If I remove extends StaticParent, the program compiles absolutely fine. I know that if a class has a private constructor, it cannot be subclassed.

But my question is, though I have not used new keyword, if I extend StaticParent, why does the compiler give the error "StaticParent() has private access in StaticParent" ? A call to constructor has not been made at all.


Thank You.
 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Apologies.....The Title should be "Is it a constructor call without the new keyword?"
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshan Kapoor wrote:A call to constructor has not been made at all.

Yes it has. Or rather, it potentially could. Note that this is a compiler error, not a runtime error. The compiler assumes that somewhere you (or someone else) might try to create an instance of your class and so it has to make sure that this is possible.
If you don't include a constructor in your code, the compiler automatically adds a no-arg constructor. All that this constructor will do is call the parent class no-arg constructor. But because the parent class constructor is private, it can't do this so you get a compiler error.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few things you need to know:

When a new instance of a class is created (because you used the "new" keyword), a constructor is always called to initialize that new instance.

An object that is an instance of some subclass is like an onion, with layers. For example, an instance of StaticChild consists of an instance of class Object, with a layer that consists of what's in class StaticParent, with a layer that consists of what's in class StaticChild.

To initialize such an object, all the layers must be initialized. This happens by calling a constructor of class Object, then a constructor of class StaticParent, and then a constructor of class StaticChild. Constructors have an implicit call to the constructor of their superclass. You can make this call explicit if you like:

If you don't provide a constructor in your class, then the Java compiler will automatically add a public, no-arguments constructor.
 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper de Jong and Stuart A. Burkett

I completely agree with both of you. Whatever both of you said is absolutely right but it doesn't answer my question. I'll try to ask it in a different way. Now, see the following code-



AND



Now, if we run this code, it generates the following output-

yo
hello in Child
hello in Parent

Now, keeping in mind that compiler does provide a no-argument constructor with super() call in it, if we remove the line with the "new" keyword, "yo" in output will not show up. It is because compiler will only provide a no-argument constructor with a super() call in child class if it gets the "new" keyword. Removing that line, you will get -

hello in Child
hello in Parent

Now, coming back to my original question, I didn't provide "new" keyword in that program. So how come compiler made a call to the constructor?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshan Kapoor wrote:Now, keeping in mind that compiler does provide a no-argument constructor with super() call in it, if we remove the line with the "new" keyword, "yo" in output will not show up. It is because compiler will only provide a no-argument constructor with a super() call in child class if it gets the "new" keyword.

No. It is because you are not creating an object in that case, so the constructor of class StaticParent is not called. It has nothing to do with whether the compiler provides a no-args constructor or not. It does provide a no-args constructor, but since you're not creating an object, it's not called.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshan Kapoor wrote:Now, coming back to my original question, I didn't provide "new" keyword in that program. So how come compiler made a call to the constructor?

You don't make a call to new in this particular bit of code, but there may be code elsewhere that does try to create an instance of your class.
Because of this the compiler has to be sure that that call would work. However because the parent class constructor is private the compiler knows it won't work and so doesn't allow the code to compile.
 
Roy White
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Child class always called parent class constructor , So need set public or protected!
 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Jesper de Jong & Stuart A. Burkett -
Thanks a lot. I appreciate your help . I got the point now
I will keep coming up with more questions.
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshan Kapoor wrote:
I know that if a class has a private constructor, it cannot be subclassed.


Well, that is not quite true. If a class has one or more private constructors, but also one or more non-private ones, it can be subclassed making use of a non-private contructor.
 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ivan Jozsef Balazs

I didn't even think about this one! Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!