Skip to content

Instantly share code, notes, and snippets.

@dapurv5
Created June 12, 2015 09:55

Revisions

  1. dapurv5 created this gist Jun 12, 2015.
    30 changes: 30 additions & 0 deletions InheritanceDemo
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@

    class Base {
    static int x = 0;
    int m = 0;
    }

    class Derived extends Base
    {
    static int x = 2;
    int m = 3;
    public static void fun() {
    System.out.println(x); // Compiler Error: non-static variable
    // cannot be referenced from a static context
    }

    public void fun2() {
    System.out.println(m);
    }
    }

    /**
    * @author dapurv5
    */
    public class InheritanceDemo {
    public static void main(String[] args) {
    Derived d = new Derived();
    Derived.fun();
    d.fun2();
    }
    }