justik justik - 21 days ago 5x
Java Question

Run method of a generic type

I am not sure how to run a method of the generic type Point. Suppose the following classes

class Point1 {
double x, y;
public Point1 (double x_, double y_) {x=x_; y = y_;}
public double getX() {return x;}
public double getF1() {return x;}

class Point2 {
double lat, lon;
public Point2 (double lat_, double lon_) {lat = lat_; lon = lon_;}
public double getLat(){return lat;}
public double getF1() {return lat;}

sharing the same method getF1() and a method

public <Point> void test(List<Point> points) {
for (Point point:points)
double x = point.getF1(); //Error Can not find symbol getF1()

public static void main (String [args]) {
List <Point1> points = new ArrayList<>();

How to run the method getF1() associated with the Point1 type for the generic type Point (Point = Point1)? Is it possible to use the interface

public interface ICoord {
double f();


public <Point> void test(List<Point> points, ICoord function) {
for (Point point:points)
double x = point.function.f();


It does look like you're just missing the definition of Point here.

public interface Point {
    double getF1();

This also means that each of your Point classes would have to implement this interface:

public class Point1 implements Point { }
public class Point2 implements Point { }

...and then you can use it, but you wouldn't need the generic parameter at all.

public void test(List<Point> points) {
  for (Point point: points) {
      double x = point.getF1();