As per my Previous Question, I am reading the article from Angelika Dissecting Enum. Except for the points that a type can only be instantiated for its subtypes and the subtypes do inherit some common methods, I am not able to understand the article.
What is the meaning of abstract Enum class declared in this way? How is it helpful?
The document in the last part has described three aspects, can someone explain them in easier terms to me?
I do see in the code sketch the Enum class is declaring the compareTo method. When Enum is implicitly implementing Comparable interface. Why do it needs to define its own compareTo method?
Seems like it is a concept of recursive generics. What does recursive generics exactly mean? After doing a bit of R&D and understanding my last question answer, I understand that it forces the class to be parameterized on itself.
Still, a detailed explanation would be useful.
I think the main benefit of declaring generic types as Type<E extends Type<E>> is that such generic classes will make subclasses to inherit methods which return or accept arguments with subtype's type. Such methods in java.lang.Enum are:
public final int compareTo( E o) { ... }
public final Class< E > getDeclaringClass() { ... }
So, if we declare the enum Color, that implicitly means:
public class Color extends Enum<Color>
so in this instantiation of Enum the type paramater E is assigned the type argument Color, so the above methods will look like these:
public final int compareTo(Color o) { ... }
public final Class<Color> getDeclaringClass() { ... }
When saying something like Enum<Color extends Enum<Color>>, that sounds like you are declaring a generic type parameter Color that makes sure that it extends Enum with a type parameter matching Color.
But that isn't where generic type parameters for a class are declared. You must declare them next to the class name; you can only use them later in the extends clause. E.g.
// Use "extends" here ... not here.
public class MyClass<E extends MyClass<E>> extends MySuperClass<E>
In this example, you are declaring the class Color to be the value of the generic type parameter that is already defined on Enum.
Related
In java, can I do something -more or less- like this? and how?
public class SomeGenericClass<T> extends T{
}
I think this is the relevant quote from the JLS (§8.1.4):
The ClassType [provided in the extends clause] must name an accessible (§6.6) class type, or a compile-time error occurs.
(The bit about accessibility isn't relevant).
A class type is not the same thing as a type variable (§4.3) (which T is), so attempting to do this would be a compile-time error.
you can't do public class SomeGenericClass<T> extends T for multiple reasons:
Type erasure will erase T to Object., and it would be pointless to say public class SomeGenericClass<T> extends Object ,as Object is already the base class of all types.
Imagine if it were allowed and i did something like public class SomeGenericClass<T extends Comparable> extends T , type erasure will turn this into public class SomeGenericClass<Comparable> extends Comparable but interface is implemented not extended .
public class SomeGenericClass<T> extends T{
}
You must understand Generics better. SomeGenericClass is a template (C++ terminology). As it is, it means nothing to compiler. You cannot write
SomeGenericClass<T> obj = new SomeGenericClass<T>() // this is invalid
Instead you need to supply the type attribute. This information complements the above answer provided by #Andy Turner
I'd like to do something like this:
private Class<? extends Enum<?> implements IMultiBlockEnum> typeEnum;
How would I do that? The "&" instead of "implements" doesn't work, but Eclipse doesn't give a proper explanation either.
Christopher Klinge
You can only use & when declaring an inferred parameter type, like this:
<T extends Enum & IMultiBlockEnum> void x(T a) {}
Wildcard types may not specify a type intersection as an upper bound.
If you start wondering why it is so, consider what would be the return type of typeEnum.newInstance(). It would have to be both Enum<?> and IMultiBlockEnum at the same time.
For the relevant JLS quote please refer to this answer.
P.S. Another thing that makes little sense in your example is that you end up using two independent wildcards, but obviously want it to be captured as the same type.
You can't extend Enum
Enum types are final by design.
You can create your own class using the original Enum as you like without extend it
If you want to require that the implementation be a certain enum, but also implement an interface, you need to define your own enum "class" implementing the interface(s) , e.g.
public enum MyFancyEnum implements IMultiBlockEnum {
A,B,C,D;
// put code to implement IMultiBlockEnum here, e.g.
public void doTheMultiBlockEnumStuff(String input) {
...
}
}
and then declare your variable as a MyFancyEnum, e.g.
private MyFancyEnum typeEnum = MyFancyEnum.C;
I have recently started reading Core Java. But I am having a hard time grasping the concept of wildcards.
Specifically, I'm confused about the difference between the following:
public class A<T extends {some_class}> {/*...*/}
and
public class A<? extends {some_class}> {/*...*/}
Can anyone help me understand the difference if there is at all?
The difference is that you cannot use the ? elsewhere while you can use T. For example:
public class Foo<T extends Number> {
T value; // you can declare fields of type T here
int foo() {
// Since you said T extends Number, you can call methods of Number on value
return value.intValue();
}
}
So why would you use ? at all? If you don't need the type. It wouldn't make sense to use it in a class definition any way that I can think of. But you could use it in a method like this:
int getListSize(List<?> list) {
return list.size();
}
Any kind of method where you're more interested in the overall class and it has a method that doesn't involve the paramaterized type would work here. Class.getName() is another example.
They are the same, except with the wildcard you can't refer to the type in the code of your class. Using T names the type.
In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.
The Type T is a defined type or known type
Hope this helps
? extends some class or T extends some_class
means some_class itself or any of its children and anything that would work with instanceof some_class.
As per conventions T is meant to be a Type and ? is unknown type.
now i want to implement this interface with the class.
so how should i do it?
public class TMark<E> implements ITMark{}
is this the way but throwing errors
I am getting the following:
ITMark is a raw type. References to generate type ITMark<E> should be parametrized
I am implementing this code in Eclipse IDE
ITMark is a raw type because it has no declared generic parameters.
If you declared TMark as TMark<E extends Comparable<E>> implements ITMark<E>, it would not longer be a raw type because you declared its generic parameter.
You left out the generic parameter, that is, the part that goes in the angle brackets. You need something like:
public class TMark <E extends Comparable <E> implements ITMark<E>
{
...
}
For a particular generic type you put a suitable 'Comparable' type inside the angle brackets, something like:
public class IntegerTMark extends TMark <Integer>
{
...
}
For a good introduction to generics, read the Java tutorials, the free chapter from Joshua Bloch's Effective Java at http://java.sun.com/docs/books/effective/generics.pdf and the many articles about generics at https://www.ibm.com/developerworks/java/.
Do this:
public class TMark<SomeComparableClass> implements ITMark<SomeComparableClass> {
// implement the methods of ITMark for type SomeComparableClass
}
You must specify which Comparable class you are implementing for this class. FYI, most common java types (eg Integer, String, Date, etc) are Comparable.
I'd like to define a generic type, whose actual type parameter can only be
One of the numeric primitive wrapper classes (Long, Integer, Float, Double)
String
I can meet the first requirement with a definition like this
public final class MyClass<T extends Number> {
// Implementation omitted
}
But I can't figure out how to meet both of them. I suspect this is not actually possible, because AFAIK there's no way to specify "or" semantics when defining a formal type parameter, though you can specify "and" semantics using a definition such as
public final class MyClass<T extends Runnable & Serializable > {
// Implementation omitted
}
Cheers,
Don
Java generics does not support union types (this parameter can be A OR B).
On a related note that may be of interest to some, it does support multiple bounds, if you want to enforce multiple restrictions. Here's an example from the JDK mentioned in the Java generics tutorial:
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll)
You could use factory methods for all supported types and make the constructor private/protected. You have to fix the generic type in the constructor anyway so that it makes sense, so you could probably code it like this:
public final class MyClass<T> {
public static MyClass<Integer> newInstance(int i) {
return new MyClass<Integer>(i);
}
public static MyClass<String> newInstance(String s) {
return new MyClass<String>(s);
}
//More factory methods...
protected MyClass(T obj) {
//...
}
}
Or if you do not want the constructor parameter, something like this:
public final class MyClass {
public static MyClass newIntegerInstance() {
return new MyClass();
}
//...
}
As erickson stated, the common implementation can rely only on Object anyway, so the only restriction is, that you can create other implementations for other types besides the primitive and String.
While generics won't work here, a base type with derived types for Number and String will. Since a generic type would have erased to Object anyway, any functionality you would have put there can go in an abstract base class. You're likely to need only a type-specific accessor on the subclass to get the value.
Also, be careful with the Number class. It's not limited to boxing the primitive types, as anyone can extend it—e.g., BigInteger.
Interesting question, it boggled me a bit. However apparently this is impossible. I tried several different hacks, none really work.
Maybe you could do what follows:
Make MyClass<T> a package-default class, invisible to other components, or at least with a package-default ctors only, so that it cannot extended or instantiated outside the package.
Create two public classes in the package of MyClass<T>:
MyNumericClass<T extends Number> extends MyClass<T>
MyStringClass extends MyClass<String>
This way all subclasses of MyClass will be limited to those parametrized with a Number subclass or String.