This discussion is archived
3 Replies Latest reply: Jan 4, 2013 6:26 PM by rp0428 RSS

Interface and methods

880368 Newbie
Currently Being Moderated
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 Guru
    Currently Being Moderated
    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-Consulting-com Expert
    Currently Being Moderated
    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 Guru
    Currently Being Moderated
    >
    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points