• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Best practices for defining constructors in a child class

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On pages 238 - 242 in the book 'Certified Associate Java SE 8 Programmer I' by Jeanne Boyarsky and Scott Selikoff, it explains the legal rules about defining constructors in parent and child classes:

Parent Class:

Child Class:

Also, on page 242, it says, "Child classes can define no-argument constructors even if their parent classes do not, as long as the constructor of the child maps to a parent constructor via an explicit call of the super() command."

So why do I get the error, "The constructor Lion() is undefined" on line 29?

Finally, which of the 3 constructor defining methods do you recommend using the most? I like method 2, because it's less code, but it would probably only be useful for testing purposes because of the hard coded values right?
 
Bartender
Posts: 368
44
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are getting the error because the constructors are commented out. If something is commented out, it is like it does not exist.

Finally, which of the 3 constructor defining methods do you recommend using the most? I like method 2, because it's less code, but it would probably only be useful for testing purposes because of the hard coded values right?

In this example, the two methods presented are not the same. In one case, you expose two different constructors to other classes; there are two ways for them to create objects. In the other, there is only one way for the object to be instantiated.

Your constructors should match how you are going to create your objects. If you have more than one constructor, you should chain them.
 
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and remember that cert exam books are about the rules of the language and may not always describe best practice.
 
Sam Peterson
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jj Roberts wrote:You are getting the error because the constructors are commented out. If something is commented out, it is like it does not exist.


Whatever happened to int age being initialized to a default value of 0? Why doesn't the compiler just assign it to 0?

Campbell Ritchie wrote:...and remember that cert exam books are about the rules of the language and may not always describe best practice.


Oh I know. I'm very painfully aware of that.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Peterson wrote:Whatever happened to int age being initialized to a default value of 0? Why doesn't the compiler just assign it to 0?



Instance variables don't need to be declared with an initial value, if that's what you're talking about. In the example you posted, the age and region variables of the Animal class are examples of that rule -- notice that they don't have an initial value in their declarations.

And it's not that the compiler assigns zero and null to those two variables -- that assignment happens automatically at run-time when an instance of Animal is created.

Now, in the example the constructors always assign values to the two variables. But it isn't a requirement for constructors to do that, and if they don't then the variables will have default values when the creation of an instance is completed.
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . Now, in the example the constructors always assign values to the two variables. But it isn't a requirement . . .

That means the constructors are here written correctly. The compiler checks for compliance with the syntax rules, not for quality of the code.
 
Campbell Ritchie
Marshal
Posts: 74381
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A few minutes ago, I wrote: . . . the constructors are here written correctly. . . .

But in line 17 you are assigning something to null. That is the same as the default value, but I don't like nulls. Consider assigning it to "don\u2019t know" or similar.
\u2019 is the apostrophe: ’
 
Sheriff
Posts: 16675
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A few things to note that I didn't notice were mentioned yet:

1. Constructors are NOT inherited. If a class does not have at least one explicit constructor the compiler will provide a default no-argument constructor.
2. If there is no explicit call to super(...) or super() in a constructor, the compiler will provide a call to super() by default as the first thing executed in the constructor.
3. When any class is instantiated, the primordial Object.Object() no-argument constructor must eventually be executed via a chain of calls to super constructors in the class hierarchy.

That is, if you had a class hierarchy Object <-- SubClass1 <-- SubClass2 <-- ... <-- SubClassN, then every constructor of every class in this hierarchy must have a call to some kind of super (whether it's explicit or implicit, with or without arguments) such that Object.Object() eventually gets executed.

For example, if you write:

or this:

The class essentially looks like this to the compiler:

However, if you write this:

you'd get a compiler error complaining about a missing Foo() constructor (no arguments). Why? Because that code essentially looks like this to the compiler:

Because the Foo class has an explicitly defined constructor that takes an int argument, the compiler doesn't provide a no-argument constructor for it like it did in the previous examples. Therefore, the default call to super() that the compiler provides in the Bar() constructor will fail. You'd need to provide an explicit constructor of Bar that explicitly calls super(someIntValue).

Remember, the default no-argument constructor is only provided by the compiler if there are no other constructors explicitly defined. This is because the primordial Object() no-argument constructor must eventually be called whenever any class is instantiated. This means that all subclasses of Foo now must have an explicit call to super(someIntValue) in all of its constructors otherwise it will break the chain of super() calls that works all the way back to the primordial Object() constructor.

I hope this makes sense.
   
 
Bartender
Posts: 732
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the terms "Parent class" and "Child class" is a poor choice of terminology. One should use "Superclass" and "Subclass" instead.

"Parent" and "Child" are usually used  to describe GUI relationships.
For example, when you add a JLabel to a JPanel, the panel is the parent of the label, and the label is a child of the panel (see java.awt.Component.getParent()).
So the label is a child of the panel, but it is not a subclass of JPanel.

Similarly, if you create a new class MyLabel that is a subclass of JLabel, and add a myLabel to the JPanel, the myLabel's parent is the JPanel. JLabel is the superclass of MyLabel, not its parent.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic