Let's say, I have two classes that look like this:
public class classA {
private Boolean is_started;
public classA(Boolean is_started) {
this.is_started = started;
}
public Boolean getIs_started(){
return this.is_started;
}
}
public class classB {
private String token;
public classA(String token) {
this.token = token;
}
public String get_token(){
return this.token;
}
}
I am calling those two classes from another class like this:
public class CallingClass {
public void function1() {
ClassA stepA = new ClassA(<some boolean>);
commonFunction(stepA);
}
public void function2() {
ClassB stepB = new ClassB(<some string>);
commonFunction(stepB);
}
public <T> void commonFunction(Class<T> dataObject) {
//An if statement that has a condition that only calls a function in classB {
String token = dataObject.get_token();
}
//An if statement that has a condition that only calls a function in classA {
Boolean is_started = dataObject.getIS_started();
}
//It returns error : The method [get_token()/getIS_started()] is undefined for the type Class<T>
I want to make sure that I can call different objects without specifying them in the function. For example, I want to supply ClassA and ClassB as an argument to the commonFunction as in the example above. How do I make it happen with generics?
get_token.Class<T>refers tojava.lang.Class. You call your classclassAwith a lowercase in one snippet andClassAwith uppercase in the next. Neither class A or B extend/implementClass<T>. Not really clear what you're asking or trying to achieve.interface(asClassdoesn't define agetTokenmethod)