简体   繁体   中英

How do I change the signature of a method I have overriden from my interface that I implemented onto the concrete class I'm using?

For example

//This is part of Comparable Interface:
public int compareTo(T other);//T being any class/type of parameter

//This is part of my own interface: 
public void beeper(Object what);

//This is part of my own concrete class which implements both of the above interfaces
public int compareTo(Country other)//Java allows this...
{
  //code stuffs....
}
public void beeper(String what)//This does not work...
{
  //Code stuffs....
}

How would you make an abstract method that allows you to change the method signature like compareTo does?

Use parameterized type.

Parent interface :

public interface ParentClass<T>{
  void beeper(T what);
}

Child class :

public class ChildClass implements ParentClass<String>{
  public void beeper(String what){ 
    // your impl
  }
}

Comparable is using generics; you can, too:

public interface Beepable<T> {
    void beeper(T what);
}

Your code would do this:

public class StringBeeper implements Beepable<String> {
    public void beeper(String what) { // implement here }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM