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

A class implements Iterator interface. Please provide example on how you would i

ID: 3759396 • Letter: A

Question

A class implements Iterator interface. Please provide example on how you would implement the interface and what would the class (that is implementing interface) would do.

Following are a few scenarios.

In Java (also in C#), a single class can implement more than one interface, but can extend only one class.

public interface Intf1 {

     public void m1(); // interface shows only method, not implementation of the method

}

public interface Intf2 {

     public void m2(); // interface shows only method, not implementation of the method

}

// single class implements two interfaces

public class MyClas1 implements Intf1, Intf2{

// You need to implement the method of interface

     public void m1() {

         // Write some code

    }

     // You need to implement the method of interface

     public void m2() {

         // Write some code

    }

}

An interface Intf1 can be implemented by two different classes, MyClass2, and MyClass3.

public class MyClass2 implements Intf1{

    public void m1() {

       System.out.println("tiger");

    }

}

// MyClass3 and MyClass2 both implement the same interface Intf1

// but does different action in m1

public class MyClass3 implements Intf1{

    public void m1() {

        System.out.println("lion");

    }

}

Explanation / Answer

It is possible to use your own collection type classes with the new for-loop. To do so, your class must implement the java.lang.Iterable<E> interface. Here is a very basic example:

And here is the corresponding implementation skeleton of the MyIterator class:

Here is how to use the MyCollection generified, and with the new for-loop:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote