Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend an interface

I am coding to interface.

I have an interface A and a class B that implements it.

I am told that I could override B's functionalities by extending it by C but I am asked to touch neither B nor C, and then later replace B with C as the implementation class of A in the configuration files.

I figure that I need a method that is not available in A, so I need to add a method to A that I could implement in C. But I am not allowed to touch A.

Could someone help me with how-could-it-be done?

Thanks.

EDIT: Example code:

A.java

public interface A {
    void X();
    void Y();
}

B.java

public class B implements A {
    public void X() {//something interesting}
    public void Y() {//something not very interesting}
}

Now because I was not allowed to touch either A or B I had to write another class C and extend it from B to do my things.

C.java

public class C extends B {
    public void Y() {//overriding B's not very interesting Y and making it interesting}
}

Now I need another method void Z() in C.java to do my thing but because I am coding to interface A if I add a method just on C.java while using A's reference variable I will not be able to call Z() so I will have to declare void Z() in A interface as well to use it like that but if I do that I will have to touch A which I am not allowed to. So how to get this issue resolved is what I've been trying to ask.

So essentially, I wont be able to do something like following:

A a = new C();
a.Z(); //can't do this

So is there any way for me to achieve something like that without touching A or B?

like image 322
skip Avatar asked Sep 08 '12 06:09

skip


People also ask

Can we extends an interface?

An interface can extend other interfaces, just as a class subclass or extend another class. However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all the interfaces that it extends.

How do I extend the interface in TypeScript?

Extending Interfaces in TypeScript # Use the extends keyword to extend interfaces in TypeScript, e.g. interface Dog extends Animal {age: number;} . The extends keyword allows us to copy the members from other named types and add new members to the final, more generic interface. Copied!


1 Answers

I think I understand what you want. You have the following:

public interface A {
  // can't touch this
}

public class B implements A {
  // can't touch this
}

public class C extends B {
  // you want to add your own method
  void someNewMethod();
}

// And you wish to do this:
A foo = new C();
foo.someNewMethod();

If this is what you want, then I'm afraid it cannot be done. Anyone working with A only knows about the methods defined in A; they won't know you've defined someNewMethod() in your class C. You need to be allowed to change A.

Note that in extremis, you could check the class type of instances of A:

A foo;
// ...

if (foo instanceof C) {
  ((C) foo).someNewMethod();
}

but that is really ugly and breaks many of the cardinal rules of OO programming.

like image 104
Duncan Jones Avatar answered Sep 18 '22 06:09

Duncan Jones