Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1, TF (a) Any class that implements an interface must have all the interface s m

ID: 3812599 • Letter: 1

Question

1, TF (a) Any class that implements an interface must have all the interface s methods. (b) More than one class can implement the same interface. (c) Object a of class Foo that implements InterfaceA can be instantiated (initialized) by doing the following: InterfaceA a new Foo(); (d) Interfaces can help eliminate redundancy in code. (e) Interfaces never specify constructors Every object is associated with an interface and a class. (g) Different classes that implement the same interface may also have different and additional methods not specified by that interface. (h) Many classes implementing a common interface is an example of polymorphism. 2. Given an Animal interface, three classes (Puppy, Bunny, Kitten), and a main method at the end for using them, answer questions (a) (d public interface Animal public void speak public class Puppy implements Animal public void bark() System. out.println("arf"); public class Bunny public void speak() System. out.println( public class Kitten implements Animalf public void speak system out.println( meoW public void catch Butterfly system. out.println ("Catching butterflies.

Explanation / Answer

T/F

a   True

b   True

c    True

d     True

e     True

f     False

g    True

h    True

import java.util.*;

public interface Animal
{
    public void speak();
}
public class Puppy implements Animal
{
    public void bark()
    {
        System.out.println("arf");
    }
    public void speak()
    {
        System.out.println("bark");
    }
}

public class Bunny implements Animal // a ) Bunny implements Animal interface and override its speak() method
{
  
    public void speak()
    {
        System.out.println("don't speak");
    }
}
public class Kitten implements Animal
{
    public void speak()
    {
        System.out.println("meow");
    }
    public void catchButterfly()
    {
        System.out.println("Catching butterflies");
    }
}

public class TestAnimals
{
   public static void main (String[] args)
   {
       Animal[] animal = new Animal[3]; //Animal array of length 3
      
       animal[0] = new Puppy(); // b) initialize the Animal array with Puppy,Kitten and Bunny objects
       animal[1] = new Kitten();
       animal[2] = new Bunny();
      
       for(int i=0;i<animal.length;i++)
       {
            animal[i].speak();   //call speak() method for all Animal objects
       }
      
      
   }
}

c) Output:

bark

meow

don't speak

d)

Interface declare the common methods of all classes and the implementation of those methods are done in the classes. It helps in calling those methods.