Java/Generics/Generic Interface
A generic interface example.
interface MinMax<T extends Comparable<T>> {
T min();
T max();
}
class MyClass<T extends Comparable<T>> implements MinMax<T> {
T[] vals;
MyClass(T[] o) { vals = o; }
public T min() {
T v = vals[0];
for(int i=1; i < vals.length; i++)
if(vals[i].rupareTo(v) < 0) v = vals[i];
return v;
}
public T max() {
T v = vals[0];
for(int i=1; i < vals.length; i++)
if(vals[i].rupareTo(v) > 0) v = vals[i];
return v;
}
}
public class GenIFDemo {
public static void main(String args[]) {
Integer inums[] = {3, 6, 2, 8, 6 };
Character chs[] = {"b", "r", "p", "w" };
MyClass<Integer> iob = new MyClass<Integer>(inums);
MyClass<Character> cob = new MyClass<Character>(chs);
System.out.println("Max value in inums: " + iob.max());
System.out.println("Min value in inums: " + iob.min());
System.out.println("Max value in chs: " + cob.max());
System.out.println("Min value in chs: " + cob.min());
}
}
Interface Generic (Has compile error)
import java.util.*;
interface BaseInterface<A> {
A getInfo();
}
class ParentClass implements BaseInterface<Integer> {
public Integer getInfo()
{
return(null);
}
}
class ChildClass extends ParentClass implements BaseInterface<String> { }
public class BadParents {
public static void main(String args[])
{
Vector<Integer> v1 = new Vector<Integer>();
Vector v2;
v1 = v2;
v2 = v1;
}
}