package overriding; /** Overriding example. * doSomething(int) overrides the method defined in class B. */ public class B extends A { void doSomething(int k) { System.out.println("Class B"); } public static void main(String[] args) { A a = new A(); a.doSomething(1); B b = new B(); // Create an instance of class B b.doSomething(2); // Access class B method // doSomething() will be // called. A ab = new B(); // Create an instance of class B // but use A type reference. ab.doSomething(3);// Though the A type reference // is used, the class B method // doSomething() will be called. } }