Thomas Smith
Thomas Smith

Reputation: 27

Java Polymorphism "cannot be applied to given types"

I'm trying to make my program say:

chick goes chirp chirp

pig goes oink oink

Elsie goes moo

Belle goes moo


But I keep getting 2 errors which is:

error: constructor NamedCow in class NamedCow cannot be applied to given types;

Is there a way I can fix it so my program can output this:

chick goes chirp chirp

pig goes oink oink

Elsie goes moo

Belle goes moo

import static java.lang.System.*;
import java.util.*;

public class Java5203{
    public static void main(String[] args) {
        new Environment();
}}


class Environment
{
    Farm mcDonald;

    public Environment()
    {
        mcDonald = new Farm();

        run();
    }

    public void run()
    {
        out.println();
        mcDonald.animalSounds();
        out.println();
    }
}

class Farm
{
    ArrayList<Animal> liveStock = new ArrayList<Animal>();

    public Farm()
    {
        liveStock.add( new Chick()           );
        liveStock.add( new Pig()             );
        liveStock.add( new NamedCow("Elsie") );
        liveStock.add( new NamedCow("Bell")  );
    }

    public void animalSounds()
    {
        for(int i=0;i<liveStock.size();i++)
        {
            out.println(liveStock.get(i).getType() + " goes " + liveStock.get(i).getSound());
        }
    }
}
abstract class Animal
{   
    protected String myType;

    protected String mySound;

    public abstract String getSound();

    public abstract String getType();
}   


class Cow extends Animal
{
    Cow()
    {
        myType = "cow";
        mySound = "moo";
    }

    public String getSound()
    {
         return mySound;
    }

    public String getType()
    {
         return myType;
    }
}

class Chick extends Animal
{
    Chick()
    {
        myType = "chick";
        mySound = "chirp chirp";
    }

    public String getSound()
    {
         return mySound;
    }

    public String getType()
    {
         return myType;
    }
}

class Pig extends Animal
{
    Pig()
    {
        myType = "pig";
        mySound = "oink";
    }

    public String getSound()
    {
         return mySound;
    }

    public String getType()
    {
         return myType;
    }
}


class NamedCow extends Cow
{
    public String getSound()
    {
         return mySound;
    }

    public String getType()
    {
         return myType;
    }
}

Upvotes: 2

Views: 196

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

You're calling the NamedCow constructor, passing in a String parameter,

liveStock.add( new NamedCow("Elsie") );

but this class has no constructor that takes a String parameter, and in fact the class NamedCow has no constructor at all.

Solution: give it a constructor that takes a String and uses it to set a field. e.g.,

public class Foo {
   private String bar;

   public Foo(String bar) {
      this.bar = bar;
   }

   // getters, setters, other methods,...
}

Upvotes: 4

Related Questions