Question

Good day, I have the following problem: class B extends class A and methods of both are called by another method in another class after instantiating class B (example follows):

public class A{
    //fields
    //constructors
    //methods
}

public class B extends A{
    //fields
    //constructors
    //methods
}

public class CALLER{

    public A getA(enum E){
        return Factory.getB(otherobject,E);
    }
}

public class Factory{
    public static B getB(object o,enum e){
         //do something with enums and get B
         b = new B();
         //populate b
         return b;
    }
}

Class B does not override any method of class A. Somehow at compile time this doesn't get any error but at runtime class CALLER excepts: java.lang.NoSuchMethodError: Factory.getB(object,enum) A

My question is: if B extends A why a method from a different class can't return A even if its return clause returns a B object directly? In fact changing:

public static B getB(object, enum);

with

public static A getB(object, enum);

solves the exception but then I get another exception (classCast) because obviously in other parts of the code it is awaiting a B type object, not an A.

Thanks in advance.

Was it helpful?

Solution

You would get this exception if you had compiled CALLER.java with another version of Factory.java that would have getB returning A, then updated Factory.java so that getB returns B, then recompiled Factory.java but not CALLER.java

UPDATE:

Perhaps you want to do something like this:

public abstract class Factory {
    public abstract A getInstance(object o, enum e);
}

public class FactoryB extends Factory {
    @Override
    public B getInstance(object o,enum e){
         //do something with enums and get B
         b = new B();
         //populate b
         return b;
    }
}

But the factory would then need to be instanciated.

OTHER TIPS

The first one looks like a reflection error. The java reflection classes look for the exact method signature "A getB(Object,Enum)" and not "B getB(Object,Enum)".
The second, as long as you actually create an object of type B in your getB(..) method, it will return this object. The classCast exception will only be thrown if you create a new A instead of a new B.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top