How to declare a variable of type Interface and then assign to the variable an object of a Class that implements the Interface, and how to test this?

Darcey BM :

I am struggling with the interaction between classes and interfaces that they implement. Specifically, how to declare a variable of type Interface and then assign to the variable an object of a Class that implements the Interface, when the constructor of the class require parameters. I am implementing this based on a specification, so I cannot just add a constructor which requires no parameters.

I have a constructor, which requires 2 parameters, and getter methods:

public class exampleImpl implements example{

    private int var1;
    private int var2;

    public exampleImpl(int var1, int var2) {
        if(var2< 0) {
            var1 = 1;
            var2 = 1;
        }

    public int getVar1(){
        return Var1;
    }
    public int getVar2(){
        return Var2;
    }

I then have a method "manipulate" which must take the interface as an input and return it as an output:

    public example manipulate(example e) {
        example eInst = (exampleImpl)e;
        int newVar1 = (this.getVar1()*eInst.getVar2() + this.getVar2()*eInst.getVar1());
        int newVar2 = this.getVar2()*eInst.getVar2();
        Pair<Integer, Integer> pair  = normaliseExample(newVar1, newVar2);
        int normalisedNewVar1 = pair.getKey();
        int normalisedNewVar2 = pair.getValue();
        example summedE = new exampleImpl(normalisedNewVar1, normalisedNewVar2);
        return summedE;
    }

The interface itself only defines this method as:

public interface example extends Example {

    public example manipulate(example e);
}

Is my method valid in terms of declaring a variable of interface example and then assigning to the variable an object of exampleImpl that implements example? It must also return a variable of type example, but I do not know where newVar1 and newVar2 go when this happens since they are not defined in the interface.

I also need to test this method using JUnit 4 and I am at a complete loss on how to do this, given that you cannot instantiate an object of type example, so how can I pass in parameters e.g:

class exampleImplTest {
    @Test
    public exampleImpl testManipulate()
    {
        assertEquals(example,exampleImpl.manipulate(example(3,4)));
    }

This shows an error because manipulate is not a static method, but how can I make it a static method when it requires an object (this) to work? Can I create an object within the test class to call this test on? I want to check it returns the right manipulation based on an object "this" and the new object "e". Is there a way to do this?

Apologies if this question is wrought with misunderstanding, I am a complete beginner. I am having a very hard time understanding how interfaces can be used as the specification requires me to use them here. Any help would be very appreciated.

Progman :

(I will use uppercase for the start of class and interface names)

Is my method valid in terms of declaring a variable of interface example and then assigning to the variable an object of exampleImpl that implements example?

Yes, but in this case it isn't needed and you shouldn't do it like this anyway. You assume that the object coming via the parameter e will always be a ExampleImpl instance because you have a hard coded cast of that variable. If it is not such a class you will get a ClassCastException. In this case you can remove the

Example eInst = (ExampleImpl)e;

line and use the variable e instead.

It must also return a variable of type example, but I do not know where newVar1 and newVar2 go when this happens since they are not defined in the interface.

You already have written the code

Example summedE = new ExampleImpl(normalisedNewVar1, normalisedNewVar2);
return summedE;

which will return an object, which implements the Example interface. So everything is fine here. If you want to use newVar1 and newVar2 depends on your implementation and requirement for the manipulate method.

Can I create an object within the test class to call this test on?

Yes, that's the normal way to do so. Write

Example obj = new ExampleImpl(4, 5);

in your test method to create an Example object you can work with. You can call the manipulate() method with a different Example object like this:

Example obj = new ExampleImpl(4, 5);
Example obj2 = new ExampleImpl(10, 20);
Example obj3 = obj.manipulate(obj2);

I want to check it returns the right manipulation based on an object "this" and the new object "e". Is there a way to do this?

That depends on what other methods are defined on your Example interface. You have written that it extends from a different interface, also named Example. Base on what is defined in that interface, you might call other methods on your Example object like this:

 Assert.assertEquals(42, obj3.getDifference()); // or whatever other methods you have

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=396281&siteId=1