简体   繁体   中英

Calling methods for generic types

I am new to Java. Please help me with this code.

I am creating a utility class with generic parameter. In the API of utility class, I called some API of generic type.

I am assuming that the user of this class will pass only that generic which has this API (getInt in my case) implemented.

public class Utility <DataType> {

   private DataType d;

   public Utility (DataType d_)
   {
       d = d_;
   }

   public int getValue ()
   {
      return d.getInt();
   }
}

Please point me what I am doing wrong here.

Compilation error in d.getInt(). Cannot resolve getInt method.

I added the abstract interface:

abstract interface DataType {
  public int getInt();
}

Still it is erroring out.

This might be what you are looking for:

Instead of Utility<DataType> , you need a type placeholder. Replace it with Utility<T> and replace all DataType to T .

public class Utility <T extends DataType> {

   private T d;

   public Utility (T d_)
   {
       d = d_;
   }

   public T getValue ()
   {
      return d;
   }
}

You cannot simply promise your compiler that a method will exist in all derived classes. You must make it exist. You have to define it. Use inheritance or an interface to accomplish that.

Option 1. Inheritance

Define an abstract base class with an abstract method getInt() . All non-abstract children must then implement getInt() .

public abstract class DataType() {
    public abstract int getInt();
}

Children of this class would look like this:

public class MyDataType() extends DataType {
    public int getInt() {
       return 3;
    }
}

Option 2. Interface

Define an interface with a method getInt() . All classes implementing that interface must then define a method getInt() . By the way, interface names are generally adjectives.

public interface DataTypeable {
    public int getInt();
}

An implementation of this interface would look like this:

public class MyDataType() implements DataTypeable {
    public int getInt() {
       return 5;
    }
}

Now your Utility class can use the base class or interface like this (replace DataType with DataTypeable if you go the interface route:

public class Utility {

    private DataType d;

    public Utility(DataType d) {
        this.d = d;
    }

    public int getValue() {
        return d.getInt();
    }
}

Option 3. Generics plus one of the other options

For the sake of actually answering the question, here's how to force it to work using generics.

public class Utility <T extends DataType> { // or <T implements DataTypeable>

    private T d;

    public Utility(T d) {
        this.d = d;
    }

    public int getValue() {
        return d.getInt();
    }
}

However, in this case DataType must be one of the other options mentioned above. There is really no point in using generics here.

As my understanding, you would like to use the DataType as a generic object. So I don't think you have to create interface DataType. Just need to getValue the DataType like the code below:

public class Utility<DataType> {

    private DataType d;

    public Utility (DataType d_)
    {
        d = d_;
    }

    public DataType getValue ()
    {
        return d;
    }
}

Hope this help!

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