Interfaces | Sololearn: Learn to code for FREE!

0

Interfaces

You love all animals, and have a dog and a cat as pets. class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.swim(); cat.swim(); dog.play(); cat.play(); } } interface Swimmer { void swim(); } interface Player { void play(); } //implement the Swimmer and the Player interfaces class Animal implements Swimmer, Player{ } abstract class Animal{ } class Dog extends Animal { //Override the swim() and the play() methods @Override public void swim() { System.out.println("Dog is swimming"); } @Override public void play() { System.out.println("Dog is playing"); } } class Cat extends Animal { //Override the swim() and the play() methods @Override public void swim() { System.out.println("Cat is swimming"); } @Override public void play() { System.out.println("Cat is playing"); } } I can't find where is the problem here. it doesn't give any output.

7/2/2021 1:45:54 PM

Migel

2 Answers

New Answer

+1

Migel Create abstract class Animal, define abstract methods and implement Swimmer and Player interface. public abstract class Animal implements Swimmer, Player{ public abstract void swim(); public abstract void play(); }

+1

You declare Animal class 2 times. Both are not correct way.. remove those and add this : abstract class Animal implements Swimmer, Player{ }