Java instantiates a class from a string

I have the following,

public interface SuperbInterface
public class A implements SuperbInterface
public class B extends A
public class C extends B

I want to instantiate C, but I seem to get B. what did I do wrong?

Class classz = Class.forName("C");
SuperbInterface superb = (SuperbInterface)classz.newInstance();

//Here when I debug it seems to be going to B.doWork() methods instead of C.doWork().
superb.doWork();

Solution

The only thing I can think of is that you didn't override dowork () in C

Maybe you've tried to override, but there are some spelling mistakes in the method name or wrong parameters?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>