I have confusion regarding Method Overriding in java.
(我对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?
(目的是什么?)
ask by Jaimin Modi translate from so