+ 2

Java Interface exercise Animal Lovers

Hi, I am doing the Java interface exercise Animal Lovers. When I execute the code it gets no input and of course no output. Looking a the main method, I am thinking it should have executed since I did override the the methods, but I guess I am wrong This is what I have ``` class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.swim(); dog.play(); cat.swim(); cat.play(); } } interface Swimmer { void swim(); } interface Player { void play(); } //implement the Swimmer and the Player interfaces abstract class Animal implements Swimmer,Player{ public abstract void play(); public abstract void swim(); } class Dog extends Animal { //Override the swim() and the play() methods public void swim(){ System.out.println("Dog is swimming"); } public void play(){ System.out.println("Dog is playing"); } } class Cat extends Animal{ //Override the swim() and the play() methods public void swim(){ System.out.println("Cat is swimming"); } public void play(){ System.out.println("Cat is playing"); } } } ``` Where am i going wrong with this.

12th May 2021, 3:02 PM
Curtis Chadwell
Curtis Chadwell - avatar
4 Antworten
+ 4
It's working.. edit: try make main() public. public class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.swim(); dog.play(); cat.swim(); cat.play(); } } interface Swimmer { void swim(); } interface Player { void play(); } //implement the Swimmer and the Player interfaces abstract class Animal implements Swimmer,Player{ public abstract void play(); public abstract void swim(); } class Dog extends Animal { //Override the swim() and the play() methods public void swim(){ System.out.println("Dog is swimming"); } public void play(){ System.out.println("Dog is playing"); } } class Cat extends Animal{ //Override the swim() and the play() methods public void swim(){ System.out.println("Cat is swimming"); } public void play(){ System.out.println("Cat is playing"); } } //}
12th May 2021, 3:17 PM
Jayakrishna 🇮🇳
+ 3
//Animal lovers java challenge class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.swim(); dog.play(); cat.swim(); cat.play(); } } interface Swimmer { void swim(); } interface Player { void play(); } //implement the Swimmer and the Player interfaces abstract class Animal implements Swimmer, Player{ } class Dog extends Animal { //Override the swim() and the play() methods public void swim(){ System.out.println("Dog is swimming"); } public void play(){ System.out.println("Dog is playing"); } } class Cat extends Animal { //Override the swim() and the play() methods public void swim(){ System.out.println("Cat is swimming"); } public void play(){ System.out.println("Cat is playing"); } } //Good luck everyone
18th Jun 2022, 7:56 AM
Sachini Anjana
Sachini Anjana - avatar
+ 1
package html; class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.swim(); dog.play(); cat.swim(); cat.play(); } } interface Swimmer { void swim(); } interface Player { void play(); } //implement the Swimmer and the Player interfaces abstract class Animal{ public abstract void swim(); public abstract void play(); } 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 public void swim() { System.out.println("Cat is swimming"); } @Override public void play() { System.out.println("Cat is playing"); } //Override the swim() and the play() methods } //100%
30th May 2022, 3:16 PM
Javohir
Javohir - avatar
+ 1
I experimented and now this code works: public class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.swim(); dog.play(); cat.swim(); cat.play(); } } interface Swimmer { public void swim(); } interface Player { public void play(); } //implement the Swimmer and the Player interfaces abstract class Animal implements Swimmer, Player { abstract String getName(); public abstract void swim(); public abstract void play(); public String name; } class Dog extends Animal { //Override the swim() and the play() methods String getName() { return name = "dog"; } public void swim() { System.out.println("Dog is swimming"); } public void play() { System.out.println("Dog is playing"); } } class Cat extends Animal { //Override the swim() and the play() methods String getName() { return name = "cat"; } public void swim() { System.out.println("Cat is swimming"); } public void play() { System.out.println("Cat is playing"); } }
9th Nov 2022, 8:49 PM
Yuliacita