-1

I honestly feel stupid having to ask this question but I think there's something wrong and I couldn't find the answer. In a project there are classes extending a generic super class without specifying the type. For example :

public abstract class Vehicle<T> {}

public class Car extends Vehicle {}
public class Van extends Vehicle {}

I'm pretty sure Java 7 used to give a compile error but the project I'm working on is using Java 8. Is this a new feature? How does the compiler know what is the actual type?

algiogia
  • 936
  • 2
  • 14
  • 40
  • 1
    http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it ... just a note, that you shouldn't do that. – Tom Jun 22 '16 at 13:10
  • @Tom I know it's bad, that's why I'm asking the question :) – algiogia Jun 22 '16 at 13:11
  • Oh ok. Mind that this also works with Java 7. And if your IDE doesn't show a warning (like you wrote in the below comment), then it isn't configured to show them. Check the settings for warning on raw type usage (don't know the NetBeans settings menu, so I can't tell you exact location of this setting). – Tom Jun 22 '16 at 13:15
  • @Sotirios Delimanolis I dont see how this is a duplicate. I'm not asking what is a raw type. – algiogia Jun 22 '16 at 13:28
  • Implicitly, you are. That question is a superset of yours. – Sotirios Delimanolis Jun 22 '16 at 13:29

1 Answers1

2

There won't be any compiler error in either Java version, but a warning.

Your Car and Van classes essentially extend a raw Vehicle:

Vehicle is a raw type. References to generic type Vehicle should be parameterized

This is equivalent to extends Vehicle<Object>.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • NetBeans doesn't show the warning. I thought it was a new Java 8 feature :S – algiogia Jun 22 '16 at 13:13
  • @algiogia do yourself a favor and switch to Eclipse. I know it's a matter of opinion, but most software engineers with a few year's experience will agree with me. Note that maybe netbeans can be configured to show you raw type-related warnings, I don't know - haven't used it in ages. – Mena Jun 22 '16 at 13:14
  • 2
    @algiogia *"do yourself a favor and switch to ..."* IntelliJ IDEA (there is a free community edition). Eclipse is also a "pain in the ass" (imo). – Tom Jun 22 '16 at 13:16
  • @Tom I don't dislike IntelliJ but afaik it's proprietary. I don't agree with Eclipse being a pain - aside from the few occasional wtf's I love it. But yeah, all this is flame war material - shame on me for starting. – Mena Jun 22 '16 at 13:18
  • @Mena I wish I could! I'e always used Eclipse and started to hate it lately. Got a new job where they use NetBeans... You know what they say "be careful what you wish for" ;) – algiogia Jun 22 '16 at 13:20
  • @Tom earlier version of Eclipse used to work fine. With the latest 2 I had a lot of crashes but definitely better than NetBeans. – algiogia Jun 22 '16 at 13:21
  • No worries, there is no need to have a flame war :). Both, Eclipse and IntelliJ IDEA, are mature IDEs and there is _no_ real harm in using either of them. Its mostly a matter of taste. Btw: there is also a free version of IntelliJ IDEA, but he ultimate edition (which costs "some" bucks) is worth it if one is a professional developer who uses the supported library integrations. – Tom Jun 22 '16 at 13:21