Java Multiple units

Source: Internet
Author: User

polymorphic ★★★★★ (one of the object-oriented features): The function itself is polymorphic, and a certain thing has different concrete embodiment.

embodiment: a reference to a parent class or interface refers to its own subclass Object. //Animal a = new Cat (); the parent class can call a subclass of the override (a method in the parent Class)

Benefits of Polymorphism: improve the extensibility of the Program. The inherited parent class or interface is typically something in a class library, (if you want to modify the way a method is implemented) only by subclasses to overwrite a method that you want to change, so you can invoke the override method by pointing the application of the parent class to the instance of the subclass!

Disadvantages of Polymorphism: when A parent class reference points to a subclass object, the extensibility is improved, but only the methods available in the parent class can be accessed, and the methods specific to the subclasses are not accessible. ( late-generation functionality cannot be used in the prophase, that is, access limitations )

Prerequisites for Polymorphism:

1: must have a relationship, such as inheritance, or Implementation.

2: There is usually a overwrite Operation.

the appearance of polymorphism has also changed: it used to be creating objects and directing objects to do things. With polymorphism, we can find the common types of objects, directly manipulate common types to do things, so you can direct a group of objects to do things, that is, by manipulating the parent class or interface Implementation.

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

Class Grandfather {

void lecture () {

System.out.println ("enterprise management");

}

void fishing () {

System.out.println ("fishing");

}

}

Class Teacher extends

void lecture () {

System.out.println ("JAVA");

}

void See movie () {

System.out.println ("see the film");

}

}

class {

public static void main (string[] Args) {

Uncle bi x = new Teacher (); //mr. Bi's Object was promoted to the grandfather Type.

X. Lectures ();

X. Watch a movie (); //error.

bi teacher y = (bi) x; //convert your grandpa type to CHENGBI teacher Type.

Y. See the movie ();// in polymorphic, from the beginning to the end, the subclass object is changing the Type.

}

}

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

If you want to use the subclass Object's unique method, How to determine which object is the specific subclass of the class?

You can use a keyword Instanceof;//determines whether the object implements the specified interface or inherits the specified class

format:< object instanceof type >, determines whether an object belongs to the specified type.

Student instanceof person = true;//student inherits the Person class

Java Multiple units

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.