3 Replies Latest reply: Jan 4, 2013 8:26 PM by rp0428 RSS

    Interface and methods

    880368
      Consider


      Interface A {
      method 1 {}
      method 2{}
      }


      class B extends A {
      method1 {.....}
      method2 {...}
      }

      class C extends A {
      method1 {....}
      method2 {....}
      method3 {....}
      }


      public class Test {

      private A aType;

      public void testMethod {

      ...
      aType.method1();
      .....
      }

      In the test class ...if aType is of type Class C then how can I execute method3 . in testMethod.thanks
        • 1. Re: Interface and methods
          gimbal2
          Typecasting is one option, adding method3 to the interface is another.

          btw: after 45 posts you should have noticed the nicely formatted code in other posts by now. You can achieve that yourself by using \
           tags.                                                                                                                                                                                                                                                                                                                                                                                                                                                        
          • 2. Re: Interface and methods
            TPD-Opitz
            C cType =new C();
            cType.method3();
            bye
            TPD

            Edited by: TPD Opitz-Consulting com on 04.01.2013 22:45
            • 3. Re: Interface and methods
              rp0428
              >
              Interface A {
              method 1 {}
              method 2{}
              }

              class B extends A {
              method1 {.....}
              method2 {...}
              }
              . . .
              In the test class ...if aType is of type Class C then how can I execute method3 . in testMethod.thanks
              >
              You can't. What you posted is invalid.

              Classes don't extend interfaces; they extend classes and implement interfaces.

              Interfaces extend interfaces.