Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

rules for extending classes

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
 
Saloon Keeper
Posts: 12275
259
  • Mark post as helpful
  • send pies
  • 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
  • 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"
 
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic