• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

rules for extending classes

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are there any rules for extending classes?

I found out the hard way the superclass must have a default no arg constructor.

I suspect there are other rules, but I can't seem to find any.

Mark
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, a superclass doesn't need a default constructor. It just needs an accessible constructor. The subclass then needs to call this constructor explicitly in the first line of its own constructors.

You can subclass any class, as long as 1) the class is accessible, 2) the class is non-final and 3) the class has an accessible constructor.

Here's an example of subclassing when there is no default constructor in the superclass:
 
Ranch Hand
Posts: 196
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As "Stephan van Hulst" said Default Constructor existence is not a Compulsion for any Class.
But
If you don't have a Don't have a default Constructor in the Super class and you forget to insert a super(<with appropriate parameters>) inside every subclass you create then the compiler will Complain .

The following code will result in a Compiler Error :

"Implicit super Constructor A() undefined will be the error"
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic