Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Constructor Rules in Java

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In OCA 8 by Jean boyorsky, rule 4, it has been said that

If the parent doesn’t have a no-argument constructor and the child doesn’t define any
constructors, the compiler will throw an error and try to insert a default no-argument
constructor into the child class.

but when I compile, there was no error!,  can any one explain ?

 
Marshal
Posts: 65062
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look in the Java® Language Specification (=JLS), you will find that your superclass does indeed have a no‑arguments constructor.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:If you look in the Java® Language Specification (=JLS), you will find that your superclass does indeed have a no‑arguments constructor.


I know, there will be default no-argument constructor. But I am confused with the rules explained by Jean Boyorsky in her book. Can you explain the rule to me ?
 
Bartender
Posts: 1244
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will you please mention the page number?  this is from OCA SE 8 by Sybex book ?

by the way i moved your topic from OCPJP forum to appropriate OCAJP 8 forum.
 
author & internet detective
Posts: 39396
763
Eclipse IDE VI Editor Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's on page 243. That text is a bit confusing; we've re-written in the Java 11 version of the book. (which we are actively working on.)

We were trying to explain the error case where the parent doesn't have a no-argument constructor. (Yours does; just implicitly generated as the default constructor). They key point is that if the parent defines a constructor, the child needs to as well.

 
Rancher
Posts: 128
7
IntelliJ IDE Spring Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I assume you are referring to this rule (from the book):

Constructor definition rules:
4) If the parent doesn’t have a no-argument constructor and the child doesn’t define any constructors, the compiler will throw an error and try to insert a default no-argument constructor into the child class.

Let's say you have the following code:



The parent class (Animal) does not have a no-args constructor, but a constructor that takes an argument (i.e. age). The child class (Dog) does not have a constructor specified.

In this case, when you compile the code, the compiler will add a no-args constructor to the Dog class. This default constructor also contains a call to the no-args constructor of the parent class:



What method is super() calling? Exactly, that method does not exist. Therefore, you must explicitly add a call to the super constructor, e.g. super(10);

In your original example, both the superclass (Mostafa) and the subclass (Zunayeed) don't have a constructor specified. Therefore, the compiler will add a no-args default constructor for both classes, which is why you do not get an error.

 
Brecht Geeraerts
Rancher
Posts: 128
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:.... (which we are actively working on.) ...



I'm sure I am not the only one thinking "the OCA/OCP8 books are great, let's hope the new book will be here soon.  
 
Campbell Ritchie
Marshal
Posts: 65062
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:. . .  They key point is that if the parent defines a constructor, the child needs to as well.

I presume that means constructors with arguments only. The subclass's constructor must then start this(something); or super(something); and it is the super(something); part that causes the problem in question.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:It's on page 243. They key point is that if the parent defines a constructor, the child needs to as well.



Thank you Jeanne Boyarsky . Now, I understand the concept. I changed my code, and I understood how compiler works in case of constructor.
 
Jeanne Boyarsky
author & internet detective
Posts: 39396
763
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jeanne Boyarsky wrote:. . .  They key point is that if the parent defines a constructor, the child needs to as well.

I presume that means constructors with arguments only. The subclass's constructor must then start this(something); or super(something); and it is the super(something); part that causes the problem in question.


Yes, that
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:It's on page 243. They key point is that if the parent defines a constructor, the child needs to as well.



Thank you Jeanne Boyarsky . Now, I understand the concept. I changed my code, and I understood how compiler works in case of constructor.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 48
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:I assume you are referring to this rule (from the book):

Constructor definition rules:
4) If the parent doesn’t have a no-argument constructor and the child doesn’t define any constructors, the compiler will throw an error and try to insert a default no-argument constructor into the child class.


Thank you so much for your nice nice explanation:
I changed my code in order to get the concept, and then it failed in compilation that made me understand  the concept.

 
 
Brecht Geeraerts
Rancher
Posts: 128
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm glad I could help.  
 
Jeanne Boyarsky
author & internet detective
Posts: 39396
763
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jeanne Boyarsky wrote:. . .  They key point is that if the parent defines a constructor, the child needs to as well.

I presume that means constructors with arguments only. The subclass's constructor must then start this(something); or super(something); and it is the super(something); part that causes the problem in question.


Yes, that
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!