overloading an overriden generic method - java

PumpkinBreath :

I am revising for a java exam by going through some past papers and am completely stumped about this questions use of method overloading.

The question is:

Consider the following Java interface.

public interface Converter<A, B> { 
    B convert(A xs); 
}

(a) Write a default method convertAll in the interface Converter that takes an ArrayList xs as input and returns a new ArrayList. If the ArrayList xs is [x1, ..., xn], the method returns [convert(x1), ..., convert(xn)]. In your method you may assume that xs is never null. If one of the calls to convert throws an exception, your method should not catch it.

(b) Write a class StringConverter that implements the above interface suitably. In particular, the class is supposed to have a method public Integer convert(String word) that behaves as follows:

• If word is the null reference, an IllegalArgumentException is thrown.

• Otherwise the method returns the length of word.

Here is what I have done:

Converter.java

package com.sp2.bbk.q4;

import java.util.ArrayList;

public interface Converter<A, B> {

    B convert(A xs);

    default ArrayList<B> convertAll(ArrayList<A> xs) {
        ArrayList<B> newArrayList = new ArrayList<>();
        for (A a : xs) {
            newArrayList.add(convert(a));
        }
        return newArrayList;
    }
}

StringConverter.java

package com.sp2.bbk.q4;

import java.util.ArrayList;

public class StringConverter<A, B> implements Converter<A, B> {

    private ArrayList<A> stringList;

    public StringConverter() {
        this.stringList = new ArrayList<>();
    }

    public ArrayList<A> getStringList() {
        return stringList;
    }

    public Integer convert(String word) {
        if (word == null)
            throw new IllegalArgumentException();
        return word.length();
    }

    @Override
    public B convert(A xs) {
        // what happens here??
    }
}

Main.java

package com.sp2.bbk;

import com.sp2.bbk.q4.StringConverter;

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

        // Question 4
        StringConverter<String, Integer> stringConverter = new StringConverter<>();
        stringConverter.getStringList().add("This");
        stringConverter.getStringList().add("is");
        stringConverter.getStringList().add("a");
        stringConverter.getStringList().add("sentence");

        ArrayList<Integer> convertedList = stringConverter.convertAll(stringConverter.getStringList());
        System.out.println(convertedList);
    }
}

It seems straightforward enough, the convertAll() method converts all of the words into an integer representing how long each word is by calling the convert() method and storing the result in a list.

What I can't work out is how to use the implemented convert() method to call the other convert() method in StringConverter.java and actually do the conversion. I either start an infinite recursive loop or get an ambiguous method call warning. This is not homework, just revision. Any help getting me to understand this would be greatly appreciated.

Henrique Sabino :

What you need to do is implement the interface like this:


public class StringConverter implements Converter<String, Integer> { 
    //...
}

Then your method can be overridden like this:

@Override
public Integer convert(String word) {
    if (word == null)
        throw new IllegalArgumentException();
    return word.length();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=132616&siteId=1