7

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 seems to be getting 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();
1
  • 3
    have you overriden doWork in C ?? please show the relevant code fully Commented Mar 3, 2011 at 8:24

4 Answers 4

5

The only thing I can think of is that you haven't overridden doWork() in C.

Maybe you have tried ot override but with some spelling mistake in method name or wrong parameters?

Sign up to request clarification or add additional context in comments.

1 Comment

As it turns out there is an obsolete jar in the build path, it so happens in this jar C has not yet override B's doWork. duh!
1

Assuming you are using a newer version of Java add @Override to the doWork method. If you have incorrectly overriden the method the compiler will then let you know.

Comments

0

Are you sure that C overrides the doWork method? Based on the question as it is posed it is perfectly reasonable/legal to expect that as part of extending B, C does not provide its own implementation for doWork and instead relies on the one provided the superclass B.

Comments

0

I see the outcome:

A's do work

whats the things you are expecting ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.