1

Write a single class declaration through which one can interact with any type of data.

this is one of my exam paper question. I have no clear idea how to write a answer for this question. Is it correct if I override a method with different type of parameters changing data types?

Sujatha Girijala
  • 1,141
  • 8
  • 20
  • 5
    There's really not enough information and / or context to begin answering this question – Phil Aug 07 '18 at 05:24
  • 2
    Do you mean something like List or List? Then you might want to take a look at Generics: https://stackoverflow.com/questions/462297/how-to-use-classt-in-java – svdragster Aug 07 '18 at 05:28
  • 3
    Such a vague question... You could argue that `setObject(Object o)` and `Object getObject()` would be enough, but that's probably not what your teacher is looking for. – Stefan Aug 07 '18 at 05:29
  • 2
    This is kind of a poorly worded question... What do they mean by "interact"? One thing to keep in mind is that Java has primitives and generics, but not generic primitives. So if they really want "any type of data" you should create a class with generics, and also some methods that "interact" (whatever that means) with each of the primitive data types. – mypetlion Aug 07 '18 at 05:35

1 Answers1

2

NO?! :-)

public class DataHolder<T> {

    private T data;

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35