Hey there I have just started Java for a course I will be taking soon (yeah I'm a nerd, I study before I have start the class). I wanted to cram a few concepts together to learn how everything works together in Java.
Now, what I wanted to try to do is a simple Shape (Forme in french) class which has a least 1-2 classes inheriting from it and make a few instances inside a collections object. I also wanted to make those classes generics to try that out as well. That is the part I'm having trouble with.
I'm very used to C++ so that may be where the confusion lies, but it seems that I can't use generics so that, for example, I can specify that dimensions will be in, say, Integer or Double or Float or whatever. Is it possible ?
The GetArea() method below can't compile because of base and hauteur.
package testconcepts;
import java.util.*;
import java.lang.*;
abstract class Forme <T extends Number> {
protected String nom_forme;
String NomForme() { return nom_forme; }
abstract Double GetArea();
}
class Triangle <T extends Number> extends Forme {
protected T base, hauteur;
Triangle() {
this.nom_forme = "TRIANGLE";
}
@Override
Double GetArea() { return base * hauteur / 2; }
T GetBase() { return base; }
T GetHauteur() { return hauteur; }
void SetBase(T base) { this.base = base; }
void SetHauteur(T hauteur) { this.hauteur = hauteur; }
}
public class TestConceptsJava {
public static void main(String[] args) {
ArrayList list = new ArrayList();
list.add(new Triangle<Double>());
}
}
Notice that I also cannot make assignments to 0 in the constructor because of type issues.
Also, I know there are very similar topics, but I haven't found a solution to my problem yet in those other questions.
TLDR How can I make work generics with primitive type wrappers (Double, Integer..) ?