Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: userc865b
Added: Mar 13, 2018 12:09 AM
Views: 11
Tags: java
cast from Interface to class that implents the interface to call the method of the class not the metho from interface!
  1. I have a question about interface and class implementing interface.
  2.  
  3. This is my code:
  4.  
  5. interface iMyInterface {
  6.     public iMethod1();
  7. }
  8.  
  9. public class cMyClass implements iMyInterface {
  10.     public iMethod1() {
  11.         // some code
  12.     }
  13.     protected iMethod2() {
  14.         // some code
  15.     }
  16. }
  17. I would like to create an instance of iMyInterface as this :
  18.  
  19. iMyInterface i = new cMyClass();
  20. i.iMethod1();
  21. It's ok, but how can I call iMethod2() from my interface instance? Is this working and safe:
  22.  
  23. ((cMyClass)i).iMethod2(