Java understands polymorphism (cainiao) and java Polymorphism

Source: Internet
Author: User

Java understands polymorphism (cainiao) and java Polymorphism

I am a freshman dog. The content is only my first experience. Sorry for the error.

A beginner may have been very familiar with some new terms.

Such as polymorphism, generics, inheritance, and interfaces. In fact, this is not very difficult. Don't be scared by the name, don't be afraid, and you will understand him slowly.

I think polymorphism is based on inheritance.

Let's look at inheritance.

Class father {String sex = "man"; public void sport () {System. out. println ("") ;}} class son extends father {String sex = "small boy"; int age = 15; public void sport () {System. out. println ("playing") ;}} public class DuotaiDemo {public static void main (String [] args ){
    father xiaoming=new son();
 

 

        System.out.println(xiaoming.age);    }}

Here we use the subclass to inherit the parent class, And the subclass inherits the attributes and methods of the parent class, such as the sex and sport methods.

class son extends father{    }

Even if there are no methods or attributes during inheritance, the subclass has the methods and attributes of the parent class because of inheritance.

-------------------------------------------

The so-called polymorphism is

 father xiaoming=new son();

The parent class reference of the subclass object may be a bit confusing at the beginning, so let's break it down.

New son () creates an object here, and then we use a parent type to reference it as the father class.

Originally, inheritance is a large box and small box. We created a large box, which we use to point to this large box.

There are many things in the small box, but not in the large box.

Similarly, the property method subclass of the parent class exists, but the extra property method of the Child class does not exist.

Polymorphism is to use a small box to direct to a large box.

class son extends father{

Public void homework ()
{
System. out. println ("do homework ");
}

}

For example, the subclass adds a job method, but the parent class does not.

Then we return to polymorphism, and our subclass object has a parent class to reference, so this reference variable does not work to call homework.

Because the parent class does not have the homework method, there is nothing in the big box (outside the small box)

If the sport method is called, The subclass method is called. (This is Special)

In my simple understanding, this parent class reference refers to a parent class object, not pointing to a subclass object.

It is a little special. If there are methods in common with sub-classes, the sub-class method is called. (Of course not rigorous)

That's all. There is nothing special about others. It's still the same as usual. For example, if you call the sex of an object, it's still the parent class.

Related Article

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.