The Java parent refers to the object meaning of the child class

Source: Internet
Author: User
Class A, and another class B inherits Class A. The following code is available:
A = new B (); This is called the parent class's reference A to the subclass object, new B ();
If your A is an interface, or an abstract class, then it is not possible to create an object, so that writing can also be used to refer to his subclass or to the object that implements the class. It's not obvious that this sentence is written here. In fact, it is mainly used in the parameters of the method, that is, polymorphism (one of the three characteristics of Java, can think how important), in order to let you understand deeply, I give an example.
If there is a class, this class needs to be implemented to eat all kinds of fruits, bananas, apples, pears and so on.
Do we have to write:
Public void Eat (Banana Banana) {//...}
Public void Eat (Apple apple) {//...}
Wait, wait. This is not a very troublesome ah, but if I write this method:

So as long as the inheritance of fruit or implement fruit interface, can be used as eat parameters, is not greatly simplified programming.
Object-oriented, you slowly realize it, fun.

The Java parent refers to the object meaning of the child class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.