简体   繁体   中英

How to implement interface methods in java?

I have some strange question.

I have an interface in java:

public interface Inventory{
public void add(// take an object of a class);
public int delete(// take an object of a class);
public int edit(// take an object of a class);
}

then i wrote a class that implements the interface.

public class Supply implements Inventory{
public void add( // object of some class) {}
public int edit( // object of some class) {}
public int delete( // object of some class) {}
}

then i wrote another class that implements the interface but with different object name in parentheses.

public class support implements Inventory{
public void add(// object of different class) {}
public int edit(// object of different class) {}
public int delete(// object of different class) {}
}

my question is: what is the parameter that must be written in interface methods in order to make the other classes take any object name that would like to implement in its own way.

That is what generics are for. You can have an interface Inventory<T> with a method void add(T t) . Then you can have a class that implements Inventory<Product> with a method add(Product p) but you could also have a class that implements Inventory<Supplier> with a method void add(Supplier s) .

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