1

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.

7
  • Are you talking about generics? E.g. List<T>? Commented Nov 8, 2015 at 21:17
  • no, for example if method body in interface takes an object called Product p then this object name must be used also in all classes that implements the interface. but i have a class that needs different object name called for example: Supplier s Commented Nov 8, 2015 at 21:20
  • Method body in interface ?? Commented Nov 8, 2015 at 21:24
  • 5
    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). Commented Nov 8, 2015 at 21:24
  • 1
    No problem. Glad I could help. Commented Nov 8, 2015 at 21:34

1 Answer 1

4

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.