Posts

Showing posts from August, 2014

Polymorphism,Overloading v/s Overriding

Polymorphism! One name.. but many functions. Polymorphism can be dynamic or passive. Dynamic as in terms of... the object knows which method to actually call when the JVM is up Passive polymorphism is when which method to call is already know when the classes are compiled. Example: We have a class A. B extends A. A has a method : public int getA(){ return 1; } B also has this method, but the value returned is different: public int getA(){ return 2; } In our main class, we create objects of A and B, always keeping reference to the object as A. So, our main looks like: A a = new A(); A b = new B(); System.out.println("a.getA() : "+a.getA()); System.out.println("b.getA() : "+b.getA()); Output: a.getA() : 1 a.getB() : 2 This is dynamic polymorphism, or overriding. If we carefully see, the method signature is the same!!!! IMP: 1. access modifier should be the same OR default can become protected, protected can be public but not otherwi