0

I am getting error: sound method can't be abstract. But, when I remove 'abstract', I get class Dog and Cat needs to be abstract. What am I doing wrong?

The task says that class Dog and Cat should remain concrete. Task is below.

package inheritance

abstract class Animal(name: String) {
  abstract def sound(): String

  override def toString(): Animal
}

class Cat(var name: String)
  extends Animal(name) {
  override def sound() = {
    "meow"
  }
}

class Dog(var name: String)
  extends Animal(name) {
  override def sound() = {
    "woof"
  }
}

object Park {
  def animals() = {
    List(new Dog("Snoopy"), new Dog("Finn"), new Cat("Garfield"), new
        Cat("Morris"))
  }

  def makeSomeNoise(first: List[Animal]): List[String] = first.map(_.sound())
}

Task:

Question: [Scala] In a package named "inheritance" create an abstract class named "Animal" and concrete classes named "Cat" and "Dog".

Create an object named "Park":

Animal: A constructor that takes a String called name (Do not use either val or var. It will be declared in the base classes);

An abstract method named sound that takes no parameters and returns a String

• Override toString to return the name of this Animal

Cat: Inherent Animal; A constructor that take a String called name as a value (use val to declare name); Override sound() to return "meow

"Dog: Inherent Animal; A constructor that take a String called name as a value (use val to declare name); Override sound() to return "woof"

Park:
• A method named "animals" that take no parameters and returns a list of animals containing

• 2 dogs with names "Snoopy" and "Finn"

• 2 cats with names "Garfield" and"Morris"

• A method named "makeSomeNoise" that takes a list of animals as a parameter and returns a list of strings containing the noises from each animal in the input list

1
  • Please don't change your question, after the fact, such that the submitted answer(s) no longer have sufficient context. Commented Feb 21, 2019 at 4:16

1 Answer 1

2

OK, let's tackle a few of these.

An abstract method named sound that takes no parameters and returns a String

The modifier abstract is only used on class definitions. An abstract method is a method within an abstract class, and that method has no body, only the name and types are presented.

Override toString to return the name of this Animal

You haven't done this. Currently your toString method is also abstract.

Cat: Inherent Animal; A constructor that take a String called name as a value (use val to declare name); Override sound() to return "meow"

It says here to use val but your code has var.

So the reason you're having problems with Cat and Dog is because Animal, as you currently define it, has two abstract methods and you need to implement (override) both to create an instance of the animal.

Fix the toString method so that it is no longer abstract, and returns the correct result, and you should be good to go.

Sign up to request clarification or add additional context in comments.

9 Comments

How would I fix toString method? It is not 'override def toString(): Animal'
The instructions say to return the name of the animal. As it is, your method doesn't return anything, but it is defined to return an instance of type Animal, which is not what's required.
'override def toString(): {name}' Like this?
Don't forget the equals = sign. (Use override def sound() as a template.)
I did this 'override def toString() : Any = {name}' and got incompatible type
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.