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?

asked Dec 1, 2019 at 6:18
2
  • 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? Commented Dec 1, 2019 at 6: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"? Commented Dec 1, 2019 at 6:22

2 Answers 2

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
45.5k5 gold badges58 silver badges107 bronze badges
answered Dec 1, 2019 at 6:36
Sign up to request clarification or add additional context in comments.

Comments

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.

answered Dec 1, 2019 at 8:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.