Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Java super() not working

Name: Anonymous 2011-08-21 22:29

Can someone tell me why this doesn't work?  I'm trying to call the super class' constructor that takes arguments, but the compiler keeps saying "required: no arguments...actual and formal arg lists differ in length."  But is this not how it's done?  I'm calling the super class constructor that takes two arguments. Everything is identical.  I'm obviously doing something wrong, though.  Can anyone find an error?

public class Person
{
private String name;
private int age;

// constructors
public Person() { name = " "; age = 0; }
public Person(String nm, int ag) { name = nm; age = ag; }
}


public class BaseballPlayer extends Person
{
private int homeruns;
private double bat_avg;

// constructors
BaseballPlayer()
{
super();
homeruns = 0;
bat_avg = 0;
}

BaseballPlayer(String name, int age, int hr, double bavg)
{
super(name, age); // THIS CAUSES AN ERROR. I DON'T KNOW WHY
homeruns = hr;
bat_avg = bavg;
}
}

Name: Anonymous 2011-08-21 22:33

Read SICP.

Name: Anonymous 2011-08-21 23:33

$ javac Test.java
$ java Test
15


Works for me bro.

class Test {
    public static void main(String[] args) {
        System.out.println(new BaseballPlayer("hello", 15, 5, 2.0f).getAge());
    }
}

class Person {
    private String name;
    private int age;
   
    public Person() {
        name = "";
        age = 0;
    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public int getAge() {
        return age;
    }
}


class BaseballPlayer extends Person {
    private int homeRuns;
    private double battingAverage;

    BaseballPlayer() {
        homeRuns = 0;
        battingAverage = 0.0f;
    }
    BaseballPlayer(String name, int age, int homeRuns, double battingAverage) {
        super(name, age);
        this.homeRuns = homeRuns;
        this.battingAverage = battingAverage;
    }
}

Name: Anonymous 2011-08-21 23:40

>>4
It happens when they're in separate files.  Each class is its own file and I have them both in the same folder.  I don't know if that affects anything.

Name: Anonymous 2011-08-22 0:28

>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourself.
>>4
Stop talking to yourseSegmentation fault.

Name: Anonymous 2011-08-22 0:55

if date < { 2011, 08, 23 }
    >>6
else
    >>5

fuck you

Name: Anonymous 2011-08-22 1:12

>>4
Works for me, too.  You're doing something wrong on your end that you're not showing us.

$ cat Person.java
public class Person
{
    private String name;
    private int age;

    // constructors
    public Person() { name = " "; age = 0; }
    public Person(String nm, int ag) { name = nm; age = ag; }

    protected void dump()
    {
        System.out.println( "name = " + name + "; age = " + age );
    }
}
$ cat BaseballPlayer.java
public class BaseballPlayer extends Person
{
    private int homeruns;
    private double bat_avg;

    // constructors
    BaseballPlayer()
    {
        super();
        homeruns = 0;
        bat_avg = 0;
    }
    BaseballPlayer(String name, int age, int hr, double bavg)
    {
        super(name, age); // THIS CAUSES AN ERROR. I DON'T KNOW WHY
        homeruns = hr;
        bat_avg = bavg;
    }
    protected void dump()
    {
        super.dump();
        System.out.println( "hr = " + homeruns + "; bavg = " + bat_avg );
    }
    public static void main( String... args )
    {
        BaseballPlayer bp = new BaseballPlayer( "Jethro", 42, 12, 0.333 );
        bp.dump();
    }
}
$ javac Person.java
$ javac BaseballPlayer.java
$ java BaseballPlayer
name = Jethro; age = 42
hr = 12; bavg = 0.333

Name: Anonymous 2011-08-22 12:14

>>5
Your stack size is pretty small, try adjusting it.

Name: Anonymous 2011-08-22 12:19

>>1

Oh my god; do you want to fucking put your code in fucking code tags? That's the whole reason there's BBcode on this board. I wish I could fucking hate you to death.

goto hell

Don't change these.
Name: Email:
Entire Thread Thread List