archives

Haskell V Java type checking

Below is a Haskell program with a sub-class.
It is followed my attempt to code the same concepts in Java.
Three questions:
1) Are the two examples close enough? (very subjective)
2) In this example, what are the advantages of the Haskell type checking over the Java type checking?
3) Are there more general advantages of Haskell type checking over Java type checking?

==== Haskell program===
data C = C deriving Show
data D = D deriving Show

class A t where
f::t -> t

instance A C where
f C = C

instance A D where
f D = D

class A t => B t where
g::t->t

instance B C where
g C = C

instance B D where
g D = D

-- The functions are only used for type, class and instance checking.
-- Check the type and class instances
:info f C
:info f D
:info f C
:info g C
-- output of last info command
class A t => B t where g :: t -> t -- Defined at t1.hs:14:2
data C = C -- Defined at t1.hs:1:6
instance Show C -- Defined at t1.hs:1:22-25
instance B C -- Defined at t1.hs:17:10-12
instance A C -- Defined at t1.hs:7:10-12

=====Java Program===
import java.lang.Class;

interface A {}
class A_INSTANCE implements A {}

interface B extends A{}
class B_INSTANCE implements B {}

class C {}
class D {}

public class DEMO1 {
public static void main(String args[]) {

// variables are used to check class
A_INSTANCE ac = new A_INSTANCE();
A_INSTANCE ad = new A_INSTANCE();
B_INSTANCE bc = new B_INSTANCE();
B_INSTANCE bd = new B_INSTANCE();

System.out.println("Object's Class name =>"+ ac.getClass().getName());
System.out.println("Object's Class name =>"+ ad.getClass().getName());
System.out.println("Object's Class name =>"+ bc.getClass().getName());
System.out.println("Object's Class name =>"+ bd.getClass().getName());

}
}
//Java output
Object's Class name =>A_INSTANCE
Object's Class name =>A_INSTANCE
Object's Class name =>B_INSTANCE
Object's Class name =>B_INSTANCE