How can I access a method of a class from a generic method

Pedro Todorovski

I'm working on a tiny exercise java program that calculates circle and square (classes) area, that implements surface (interface) which has a method called area(). A requirement is that I have to implement a class called SumArea that has a generic method called calcArea() that receives Circle circ[] and Square square[] arrays and executes area calculation.

Program structure:

-> UseSumArea.java (main method)
-> Surface.java (interface)
-> Square.java (class that implements Surface.java)
-> Circle.java (class that implements Surface.java)
-> SumArea.java (class that executes calcArea() method)

UseSumArea.java

public class UseSumArea {
        public static void main(String[] args) {
            Square square[] = { new Square(2.0), new Square(5.0) };
            Circle circ[] = { new Circle(3.0), new Circle(2.0) };
            Surface surf[] = new Surface[square.length + circ.length];
            surf[0] = square[0];
            surf[1] = square[1];
            surf[2] = circ[0];
            surf[3] = circ[1];
            SumArea sum = new SumArea();
            System.out.println("Square's sum area = " + sum.calcArea(square));
            System.out.println("Circle's sum area = " + sum.calcArea(circ));
            System.out.println("Surface's sum area = " + sum.calcArea(surf));
        }
    
    }

Surface.java

public interface Surface {
    public double area();
}

Square.java

public class Square implements Surface {

private double area;
private double side;

public Square(double l) {
    this.side = l;
    area();
}

@Override
public double area() {
    return this.area = (this.side)*(this.side);
}

public double getArea() {
    return area;
}

public void setArea(double area) {
    this.area = area;
}

public double getSide() {
    return side;
}

public void setSide(double side) {
    this.side = side;
}

}

Circle.java

public class Circle implements Surface {

private double area;
private double radius;

public Circle (double r) {
    this.radius = r;
    area();
}

@Override
public double area() {
    return area = (((this.radius)*(this.radius))*(Math.PI));
}

public double getRadius() {
    return radius;
}

public void setRadius(double raio) {
    this.raio = raio;
}

public double getArea() {
    return area;
}

public void setArea(double area) {
    this.area = area;
}

}

SumArea.java

public class SumArea {

private double area;

public <T> double calcArea(T[] t) { //generic method that receives Square and Circle arrays
    double arrayArea = 0;
    for (T a : t) {
        arrayArea = arrayArea+(a.area()); 
    }
    return this.area = arrayArea;
}
}

My doubt is over this SumArea's code snippet:

arrayArea= arrayArea+(a.area());

How can I access the area() method of each Circle and Square objects inside this generic method?

Andy Turner

You need to bound the type variable:

public <T extends Surface> double calcArea(T[] t) {

or just declare the parameter as an array of Surfaces:

public double calcArea(Surface[] t) {

Note that the latter is preferable because generics and arrays don't play very nicely together. If you were to need to have a type variable for other reasons, it would be advisable to change to a Collection, or similar:

public <T extends Surface> double calcArea(Collection<T> t) {

(And, as a minor matter of preference, I would use S rather than T to name a type variable which extends Surface)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I return a generic class from a method?

How can I access class variable from method JS

How can I access a cudafied method from a different class?

Android:How can I access a value from a method of a class?

How can I access my method from another class

How to access Properties of a class from a Generic Method - C#

How can I pass a generic class to a method in Java?

How can I stub a method that takes a Generic class as a param?

How can I reference a companion object's method in a generic class?

C# how can I get generic class reference at method

In R, how can I set a generic method on a class from another package?

how do I Instantiating a java class from generic method

How can I access a class data member from a method within the same class?

How can I access an object's method from another class giving the class not to much visibility?

How can I call an instance method from a generic object?

How can I return NULL from a generic method in C#?

Can't access properties of the base class of the class I am using as the type argument in a generic method

Can't access Enum<?>.valueOf(String) method for generic type (or how to get .class of generic argument)?

JavaFX: How can I access a cotroller method from a normal java class?

How can I access a containing class from its attribute's method in Java

How can I invoke a method or access a field from other class in the same package in JAVA

How can I access only one specific method from the UWP class?

How can I access a class component method in a function component?

C++: How can I access a method of an inner class?

How can I access a method defined with new keyword in a derived class

How can I access a variable that is defined within the method in the class?

How Can I Call a Class Method From a Different Class in Python?

How can I use a Method from a class in a different class?

How do I access an ArrayList from a previous method in the same class?