Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Override Superclass Constructor

inheritance constructor

  • Please log in to reply
9 replies to this topic

#1 Smokeman

Smokeman

    CC Lurker

  • New Member
  • Pip
  • 9 posts
  • Programming Language:C, Java, C++, C#, PL/SQL
  • Learning:Java, C#, (Visual) Basic, PL/SQL

Posted 14 July 2012 - 11:00 AM

I have two classes Year and LeapYear setting the days of the year to 365 and 366 respectively. How can i override the Year's constructor in the LeapYear's constructor. I did this and seems to be working fine.


public class Year {

private int days;

public Year(){
this.days = 365;
}
public void setDays(int days) {
this.days = days;
}

public int getDays() {
return this.days;
}
}


public class LeapYear extends Year {

public LeapYear() {
super.setDays(366);
}
}
  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 14 July 2012 - 12:50 PM

You can't override constructors. Your solution works, but that's because in reality, the Year constructor DID execute, but after it did, you do setDays(366), resetting what the Year constructor did.
What will actually get compiled is this, for your LeapYear class:
public class LeapYear extends Year {
public LeapYear() {
super(); //Call Year constructor
super.setDays(366);
}
}
That super(), even though you didn't write it. The compiler will add it for you.

So you can either leave it as it is, or else do:
public class Year {
private int days;

public Year(){
this(365);
}

protected Year(int days){
this.days = days;
}

public void setDays(int days) {
this.days = days;
}


public int getDays() {
return this.days;
}
}

public class LeapYear extends Year {

public LeapYear() {
super(366);
}
}

  • 1

#3 Smokeman

Smokeman

    CC Lurker

  • New Member
  • Pip
  • 9 posts
  • Programming Language:C, Java, C++, C#, PL/SQL
  • Learning:Java, C#, (Visual) Basic, PL/SQL

Posted 14 July 2012 - 12:53 PM

Thank you wim DC i will implement the protected constructor :)
  • 0

#4 UttamPanchal

UttamPanchal

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 03 January 2014 - 05:51 AM

Wim DC is right ..

But code written by Wim DC is not a constructor overriding.

 

@jagruttam


Edited by Roger, 03 January 2014 - 10:44 AM.
Removed formatting

  • 0

#5 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts
  • Programming Language:C, Java, C++, C#, (Visual) Basic, Perl, Transact-SQL, Bash, Prolog, Others
  • Learning:Others

Posted 03 January 2014 - 07:24 AM

You need a third class - AbstractYear.

 

e.g

abstract class AbstractYear {
    public AbstractYear(int daysInYear) {
        ...
    }
}
 
class Year extends AbstractYear
{
   public Year() {
       super(365);
   }
}
 
class LeapYear extends AbstractYear
{
    public LeapYear() {
        super(366);
    }
}

  • 0

Creating SEGFAULTs since 1995.


#6 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 03 January 2014 - 11:50 AM

Regardless of how you handle inheritance between the two, the amount of days for both might as well just be public static final fields, as a year is always going to be approximately 365.25 and a leap year 366.  There's no reason to set their days via constructor. 


  • 0

Averageloser.com - I used to be a programmer like you, then I took a -> in the knee. 


#7 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts
  • Programming Language:C, Java, C++, C#, (Visual) Basic, Perl, Transact-SQL, Bash, Prolog, Others
  • Learning:Others

Posted 04 January 2014 - 02:53 AM

on earth...


  • 0

Creating SEGFAULTs since 1995.


#8 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 04 January 2014 - 05:34 AM

I agree with Farrell on the "keep it simple" concept, Evan is right about the year's amount of days (and the day's duration) vary in other planets, but... I think Smokeman is limiting his software to earth

 

Anyway, good lesson about abstract classes application, no doubt SM went home with more than what he asked for :D


  • 0

#9 farrell2k

farrell2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 169 posts

Posted 04 January 2014 - 06:52 AM

on earth...

 

MatrianYear, VenusianYear, etc.


Edited by farrell2k, 04 January 2014 - 06:53 AM.

  • 0

Averageloser.com - I used to be a programmer like you, then I took a -> in the knee. 


#10 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts
  • Programming Language:C, Java, C++, C#, (Visual) Basic, Perl, Transact-SQL, Bash, Prolog, Others
  • Learning:Others

Posted 04 January 2014 - 08:43 AM

Indeed; but I suppose we've hit a bit to much of a tangent for the OP. The upshot of the topic is that there are better data models for dates than a simple year class (such as an Generic Calendar class.) If you think you want to override the default constructor then think about the model and see if that's really want you need (i.e if a class X does something in its constructor, you want to subclass it as Y, but don't actually want it to behave as X - maybe Y isn't really a subclass of it.)


  • 0

Creating SEGFAULTs since 1995.






Also tagged with one or more of these keywords: inheritance, constructor