0

I am learning about method overriding in Java. Below is an example of method overriding.

class ABC{
   public void myMethod(){
    System.out.println("Overridden Method");
   }
}
public class XYZ extends ABC{

   public void myMethod(){
    System.out.println("Overriding Method");
   }
   public static void main(String args[]){
    ABC obj = new XYZ();
    obj.myMethod();
   }
}

When an overridden method is called through a reference of parent class, then type of the object determines which method is to be executed. Now consider below :

ABC obj = new ABC();
obj.myMethod();
// This would call the myMethod() of parent class ABC (1)

XYZ obj = new XYZ();
obj.myMethod();
// This would call the myMethod() of child class XYZ (2)

ABC obj = new XYZ();
obj.myMethod();
// This would call the myMethod() of child class XYZ (3)

If (2) and (3) gives same output : Overriding Method, then why use (3)? We could just use (2) right?

I am new to Java, so I will tend to ask many doubts! Please help! Thank you!

  • 3
    An overridden method is not called through a reference of parent class. That is the point. [What does it mean to “program to an interface”?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) You have a reference of the child class, and through the "is-a" relationship, the child is-a parent. – Elliott Frisch Mar 19 '18 at 17:19
  • The difference between (2) and (3) is just which type you use to declare the variable `obj`. – lexicore Mar 19 '18 at 17:20
  • 1
    Think about the case `ABC obj = some_function_that_might_return_an_ABC_or_an_XYZ_or_something_else_compatible();`. The raw new like you have doesn't illustrate the point very well. – Mat Mar 19 '18 at 17:23

1 Answers1

0

The reason for this is to follow concept of "Programming to Interfaces , Not Implementation"

In your code , you always refer the object by parent class reference is because you can the change the implementation at run-time if needed.

In your example XYZ is only one implementation of ABC. What if at runtime ( Using DI) you want to change the implementation to XYXTest class! In your code if you have referred everywhere by XYZ , then you need to change every place to XYZTest. Now keeping the reference at parent ABC, you just change at one place where you initialize.( by DI or constructor)

Mandrake
  • 411
  • 4
  • 2
  • Thank you! I understood a bit, but could you provide a small example? – Vijayachandran Chettiar Mar 19 '18 at 17:41
  • Simple example is 'code' class XYZ extends ABC{ ... myMethod()} and class XYZTest extends ABC {... myMethod()} 'code' . While testing you want to test class XYZ method myMethod() , but is hard to test individually so you can create a mock class XYXTest and wanna test that. so in main class simply in constructor pass ABC abc = new XYZTest() instead of XYX() or while initialization . So everywhere where ABC reference is used , XYZTest.myMethod() will be called not of XYZ().myMethod(). Just like links in Unix. – Mandrake Mar 19 '18 at 17:52
  • Sorry, This was a bit complicated. – Vijayachandran Chettiar Mar 19 '18 at 18:03
  • @Mandrake - please **edit your answer** to include this additional information. Comments are not intended for permanent content, and especially not for code, which is vey hard to read without decent formatting. – APC Apr 02 '18 at 15:37