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?