Static and overriding in Java

Posted by Abhishek Jain on Stack Overflow See other posts from Stack Overflow or by Abhishek Jain
Published on 2010-06-17T09:36:12Z Indexed on 2010/06/17 9:43 UTC
Read the original article Hit count: 282

Filed under:
|
|
|

public class B {

    static int i =1;

    public static int multiply(int a,int b)
    {
        return i;
    }

    public int multiply1(int a,int b)
    {
        return i;
    }

    public static void main(String args[])
    {
        B b = new A();
        System.out.println(b.multiply(5,2));
        System.out.println(b.multiply1(5,2));
    }
}

class A extends B
{
    static int i =8;

    public static int multiply(int a,int b)
    {
        return 5*i;
    }

    public int multiply1(int a,int b)
    {
        return 5*i;
    }

}

Output:

1

40

Why is it so? Please explain.

© Stack Overflow or respective owner

Related posts about java

Related posts about inheritance