-1

How can I cast a interface to the child interface?

Example:

public interface A { ..... }
public interface B extends A { ..... }
... 
A objA = generateA();
B objB = (B) objA; 
...
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Riccardo
  • 33
  • 4

1 Answers1

1

What you've done in your example is correct.

ParentInterface objA = new ChildImplementation();
ChildInterface objB = (ChildInterface) objA;

You will get a runtime cast exception if objA is not really of the ChildInterface type.

Gonen I
  • 5,576
  • 1
  • 29
  • 60