Create a subclass object point to the parent class object what use

Here is a simple example of it. Class A, class B inherits another class A. Have the following code:
A = a new new B (); This is called a point of reference to the parent object class subclass new B (), what it means.

If your A is an interface or an abstract class, it is not possible to create an object, you can also be able to write its reference to point to his sub-class or object that implements the class. This sentence written here feel is not obvious. 
It is mainly used in the method parameters, namely polymorphism (one of the three characteristics of java, you can think how important it), to make you understand the profound, I give an example.

If there is a class that needs to implement to eat a variety of fruits, bananas, apples, pears and so on.
Do we have to write:
public void EAT (Banana Banana) {// .....}
public void EAT (the Apple the Apple) {// .....}
and so on and so forth. This is not very troublesome ah, but if I were to write this method:
public void eat (Fruit Fruit) {// ....}
so long as the inheritance Fruit or Fruit implement an interface, you can eat as a parameter, is not greatly It simplifies programming.

Guess you like

Origin www.cnblogs.com/volvane/p/9435174.html