Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

default constructor in SuperClass?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID :957621799662
Consider the following subclass definition:
public class SubClass extends SuperClass
{
int i, j, k;
public SubClass( int m, int n ) { i = m ; j = m ; } //1
public SubClass( int m ) { super(m ); } //2
}
Which of the following constructors MUST exist in SuperClass for SubClass to compile correctly?
The answer of this question is:
public SuperClass(int a)
public SuperClass()
I know the first one is absolutely right, but how about the second one? Since SubClass will not call the default constructor in SuperClass, it does it must exist in order to compile fine?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you don't explicitely call the superclass constructor, the compiler will call it for you, that it
public SubClass( int m, int n ) { i = m ; j = m ; } //1
is rewritten to
public SubClass( int m, int n ) { super();i = m ; j = m ; } //1
by the compiler
HIH
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing to remember here is that you have to remember if you provide any other constructors in the super class, than the default constructor is NOT automatically provided. So since super(m) is declared, obviously like you mentioned you must provide that constructor that takes an int, but since you had to provide that constructor in the super class you now must actually declare the constructor with no arguments if you are going to have any code that will implicity call super() like Valentin mentions will happen in the SubClass in the second method.
Sorry if that explanation is not too clear. All the Java cert books cover this topic well.
 
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic