Answer to Question #205086 in Java | JSP | JSF for umme habiba sumi

Question #205086

(Abstract class, Interface, Method Overriding)Write a java program with proper

illustration to implement the following instructions:

e) Create a class Cat that extends Feline, then override the callSound() method and print

“meow” , also override the run() method and return 30.

f) Create a class Wolf that extends Canine, then override the callSound() method and

refer immediate parent class callSound() method , also override the run() method and

return 20.

g) Create a class Dog that extends Canine, then override the callSound() method, print

“woof” and refer immediate parent class callSound() method , also override the run()

method and return 10.

h) Create a class Main and write the main() method, then create object as-

Animals[] animals = new Animals[4];

animals[0] = new Cat();

animals[1] = new Dog();

animals[2] = new Wolf();

animals[3] = new Lion();

for i=0→animals.length: call animals[i].callSound() and analysis the outputs.



1
Expert's answer
2021-06-11T00:09:50-0400
interface Animals {


    void callSound();


    int run();
}


abstract class Feline implements Animals {


    @Override
    public void callSound() {
        System.out.println("roar");
    }


    @Override
    public int run() {
        return 0;
    }
}


class Cat extends Feline {


    @Override
    public void callSound() {
        //print 'meow'
        System.out.println("meow");
    }


    @Override
    public int run() {
        return 30;
    }
}


class Wolf extends Feline {


    @Override
    public void callSound() {
        super.callSound();
    }


    @Override
    public int run() {
        return 20;
    }
}


class Dog extends Feline {


    @Override
    public void callSound() {
        System.out.println("woof");
        super.callSound();
    }


    @Override
    public int run() {
        return 10;
    }
}


class Lion extends Feline {


    @Override
    public void callSound() {
        super.callSound();
    }


    @Override
    public int run() {
        return 40;
    }
}


public class Q205086 {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Animals[] animals = new Animals[4];
        animals[0] = new Cat();
        animals[1] = new Dog();
        animals[2] = new Wolf();
        animals[3] = new Lion();


        for (int i = 0; i < animals.length; i++) {
            animals[i].callSound();
        }
        System.out.println();
        for (int i = 0; i < animals.length; i++) {
            System.out.println("Run: "+animals[i].run());            
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS