0

I have confusion regarding Method Overriding in java.

From it's definition it says :

In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.

Now, below is one example regarding it :

  class Parent { 
    void show() 
    { 
        System.out.println("Parent's show()"); 
    } 
} 
class Child extends Parent {  
    @Override
    void show() 
    { 
        System.out.println("Child's show()"); 
    } 
} 
class Main { 
    public static void main(String[] args) 
    { 
        Parent obj1 = new Parent(); 
        obj1.show(); 

        Parent obj2 = new Child(); 
        obj2.show(); 
    } 
} 

I have doubt at this line :

Parent obj2 = new Child(); 

I can do the same thing using :

Child obj2 = new Child();  

then why I need to declare it with the name Parent class?

What it's purpose?

Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72
  • 1
    You could change implementation at runtime (based on multiple implementation and use action) if you use `Parent` on the left side. If not then why would you override in first place? – SMA Dec 01 '19 at 06:20
  • 1
    Your question is not *strictly* related to overriding as such. `Parent obj2 = new Child();` is used in your example to illustrate that the child's method is executed (even if the object's declared type is parent). For an answer to your direct question, please read through [What does it mean to “program to an interface”?](https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface) – ernest_k Dec 01 '19 at 06:22

2 Answers2

2

Like you said, you don't need to declare subclass objects as their parent class, but there are other cases where this may be important, such as when you are trying to make things abstract.

What is Abstraction?

Abstraction is removing everything but the most essential. A really good example you probably already use a ton is Lists.

Whether you are using an ArrayList, a LinkedList, or any other type of Java list, you know that there are certain properties that you can always count on being there, like getting the size of the list, getting a certain element at a certain point, etc.

This DRAMATICALLY simplifies the use of these, and you can interchange them depending on your application. This is all because they are a subclass of List, in which these methods come from.

The ways that an ArrayList, and a LinkedList get and set data are different, but from the perspective of you, the user of these sub classes, the implementation is the same, you just use the classes that were overridden. It's super convenient, because you don't need to know a thing about coding, try whatever you're trying to do with a linkedlist, then with an arraylist, and see whats faster.

What's your part in this?

In the example you gave, it is very simple, and doesn't matter, but say you were making a class that sorted lists in a particular, fun, amazing way.

If you declared everything as just a List, users of your class could pass in both ArrayLists, and LinkedLists, depending on what they were doing, and both would work. So, to be a good programmer, try to keep everything as abstract as possible. It's a good rule to learn early on.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
Andrew James
  • 372
  • 1
  • 8
1

Inheritance allows us to reuse of code, it improves reusability in your java application. Note: The biggest advantage of Inheritance is that the code that is already present in base class need not be rewritten in the child class.

kajenC
  • 11
  • 3