Forgot your password?
typodupeerror
User Journal

Journal shitface's Journal: static typing bad for OO languages 4

I am becoming more and more convinced that static typing just does not belong with object oriented programming. Explicit downcasting is a pain in the ass (and most people confuse downcasting with upcasting). Besides the woes of casting, there is the fact that the compiler does a static guess as to what function to call. Guess what the output for the following Java code is:

public class example{
    static class class1{
    String name="class1";
    }
 
    static class class2 extends class1{
    class2(){name="class2";}
    }
 
    static void fn(class1 c)
    {
    System.out.println("class1");
    }
 
    static void fn(class2 c)
    {
    System.out.println("class2");
    }
 
    static void fn2(class1 c)
    {
    fn((c instanceof class2)?(class2) c:c);
    }
 
    public static void main(String[] args)
    {
    class1 c1=new class1();
    class2 c2=new class2();
 
    fn2(c2);
    }
}

The output is "class1" (without the quotes). The same results can be had with similar code in C++. Does that seem right to you? It certainly does not seem correct to me.

This discussion has been archived. No new comments can be posted.

static typing bad for OO languages

Comments Filter:

We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise. -- Larry Wall

Working...