• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of 'super' in constructors

 
Ranch Hand
Posts: 110
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Evening,

I've been playing around constructors and inheritance.

I know that constructors are not inherited but those of a base class can be referred to by 'super'.

So what happens when, the derived class has no user-defined constructor but the base class does ?


In the following code:
  *   class A extends B.
  *   A is given a user-defined constructor; B is not.  So the compile ought to provide it with the construtor:
        equivalent to:
       

  *  The code fails unless line 21 is uncommented

All insights greatly appreciated,

Mohammed.



 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammed Azeem wrote:Good Evening, . . .

Good evening . . .

I know that constructors are not inherited but those of a base class can be referred to by 'super'. .  . .
  *   class A extends B.
  *   A is given a user-defined constructor; B is not.  So the compile ought to provide it with the construtor:
        equivalent to:
        . . .

Yes, you are correct. If you call javap −c B you can inspect the bytecode and see that you do indeed have such a constructor, only it will be named <init>. Remember you only get bytecode after the souirce code has been compiled. The subclass constructor calls super(); first before doing anything else. That means your superclass has to have a non‑private constructor taking no arguments. The code will only compile if you have a no‑arguments constructor in the superclass. The example you showed only has a constructor with a String parameter in class A, so the javac tools cannot find a no‑arguments constructor, and it will not compile class B. If you uncomment the no‑arguments constructor in line 21, the code will compile, but will not run correctly because the field in the A object will always point to null, which you probably don't want.
 
Mohammed Azeem
Ranch Hand
Posts: 110
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thanks Campbell.

I extend classes willy-nilly.

So is it good practice to provide a no-parameter constructor in my own classes in case I or others wish to extend it, or is it up to users to look at the documentation to see which constructors are available ?
 
Marshal
Posts: 28226
95
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

Mohammed Azeem wrote:So is it good practice to provide a no-parameter constructor in my own classes in case I or others wish to extend it, or is it up to users to look at the documentation to see which constructors are available ?



If it isn't necessary for your class to have a no-parameter class, then don't give it one. Of course people who are extending a class should look at the documentation for that class.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mohammed Azeem wrote:OK, thanks Campbell.

That's a pleasure

I extend classes willy-nilly.

Twenty years ago it was taught that subclasses/inheritance were an essential part of object‑orientation, but now people realise that too much inheritance can cause problems. Make sure all inheritance follows the Liskov Substitution Principle and that all subclasses have a bona fide IS‑A relationship with their superclasses.

So is it good practice to provide a no-parameter constructor in my own classes in case I or others wish to extend it, . . .

Paul has already told you to avoid unnecessary constructors. I agree with him.
 
reply
    Bookmark Topic Watch Topic
  • New Topic