Nabz
Nabz

Reputation: 126

Confused about error while learning Polymorphism

I'm learning polymorphism and I am getting this red line in my superclass and sublcass it's commented on my code:

    public class Animals {
  private String name;

  public Animals(String name) {
      this.name = name;
        }

  public void changeName(String name){
      this.name= name;
  }

  public String getName(){
      return this.name;  // 
  }

}

here is my subclass:

public class Dog extends Animals {
    private String colour;

    public Dog(String name, String colour){
        super(name);  
        this.colour = colour;
    }

    public void changeColour(String colour) {
        this.colour = colour;
    }
    public String getColour(){
        return this.colour;
    }

}    

Here is the other script with the main method:

public class AnimalPolyTesting {
    public static void main(String[] args) {
        Animals puppy = new Dog("homie", "black"); // constructor Dog cannot be applied to given types;
        puppy.getName();
        (Dog) puppy.getColour(); // not a statement

    }
}

I'm not sure why I'm getting these red lines Edit: The code runs but nothing comes out. Edit2: Fixed the classes.

Upvotes: 0

Views: 94

Answers (4)

cybersam
cybersam

Reputation: 66957

[UPDATED]

I have annotated the problematic lines in your code.

public class Animals {
  private String name;

  /*
   * This is a method, not a constructor, because it has a return type.
   * Since there is no constructor, Java will automatically generate an implicit
   * `Animal` constructor for you -- one with no parameters.
   *
   * To fix: remove the return type.
   */
  public void Animals(String name) {
      this.name = name;
  }

  public void changeName(String name){
      this.name= name;
  }

  /*
   * The return type should be String, not void.
   */
  public void getName(){
      return this.name;  // RED LINE: Unexpected return value
  }
}

public class Dog extends Animals {
    private String colour;

    /*
     * 1. A "super(...)" call only makes sense from within a constructor, but this is not
     * a constructor, due to the void return type.
     * 2. Java will automatically generate an implicit `Dog` constructor for you -- one with no parameters.
     * 3. Since the only Animals constructor is the implicit constructor, which has no parameters,
     * the "super(...)" call would fail even if this was a constructor.
     * 
     * To fix: Fix Animals, and remove the void return type.
     */
    public void Dog(String name, String colour){
        super(name);  // RED LINE: constructor Animals in Animals class cannot be applied to given types; 
        this.colour = colour;
    }

    public void changeColour(String colour) {
        this.colour = colour;
    }

    /*
     * The return type should be String, not void.
     * 
     * To fix: change "void" to "String".
     */
    public void getColour(){
        return this.colour; //RED LINE: unexpected return value
    }
}

public class AnimalPolyTesting {
    public static void main(String[] args) {

        /*
         * The only Dog constructor is the implicit constructor, which has no parameters.
         * 
         * This will be fixed once Dog and Animals are fixed.
         */
        Animals puppy = new Dog("homie", "black"); // constructor Dog cannot be applied to given types;

        puppy.getName();

        /*
         * Dog.getColour() returns nothing (since its return type is void).
         * It makes no sense to try to coerce nothing to be of type Dog.
         * 
         * To call getColor(), you need to coerce puppy to be a Dog this way:
         * ((Dog) puppy).getColour();
         */
        (Dog) puppy.getColour(); // not a statement

    }
}

Upvotes: 0

Tophandour
Tophandour

Reputation: 297

It looks like getName() is set to have a return type of void and you're attempting to return a String. Change void to String in this method, and the red line should disappear there. You also have the same problem in getColour().

In addition to this, you cannot have a return type on your constructors, so you should remove void on those.

Your line (Dog) puppy.getColour(); is attempting to cast the String that is returned from puppy.getColour() to a Dog object. I believe that what you're trying to do is call getColour() on puppy but you noticed that it needs to be a Dog first. Try this instead: ((Dog)puppy).getColour(); so that you cast puppy to Dog and then you can call the method that you want.

Upvotes: 0

Brandon Laidig
Brandon Laidig

Reputation: 420

Your animals class should look like this

public class Animals {
    private String name;

    public Animals(String name) {
        this.name = name;
    }

    public void changeName(String name){
        this.name= name;
    }

    public String getName(){
        return this.name;
    }

}

The problem you have is that your constructor had a void return type. Constructors should not have a return type. Secondly, your getName() method had a return type of void. To get it to work properly you need to declare what it's returning. Given this, I would leave it to you to implement the rest of your code.

Upvotes: 1

Jean-Baptiste Yunès
Jean-Baptiste Yunès

Reputation: 36391

Constructors must not have return type, as it is implicit (a constructor for Dog obviously returns a Dog):

public Dog(...)

not

public void Dog(...)

The same applies for Animals of course.

And your get methods declare a void return type which is contradictory to the intent, so change to:

public String getName()...
public String getColour()...

Upvotes: 0

Related Questions