Java Fundamentals 14-polymorphism (thought, transformation, instanceof)

Source: Internet
Author: User

First, what is polymorphic?

1. Polymorphism Definition

Polymorphism, literally means a variety of States, he represents a variety of things.

classanimal{ Public voideat () {System.out.println ("The food was very simple."); }}//dogs and cats inherit animal classes and rewrite the Eat methodclassDogextendsanimal{ Public voideat () {System.out.println ("Eat bones."); }}classCatextendsanimal{ Public voideat () {System.out.println ("Eat fish"); }} Public classDuotai { Public Static voidMain (string[] args) {//The contemporary code becomes a bit polymorphic and it producesAnimal a=NewDog ();        A.eat (); A=NewCat ();        A.eat (); }}

There are two types of top object a
Compile type : Declares the type of the object variable, animal indicates what type to see the object
Run type : The True type of the object, the real type of the dog run type Object
The compilation type must be the parent class of the run type or the same
When the compilation type and run type are different, polymorphism appears

The so-called polymorphic: Objects have many forms, and objects can exist in different forms.

In fact, we created an animal object that could be a cat, a dog, a cat, a fish, a dog, and a bone to eat.
Animal a=new Dog ()//At this time a denotes the form of the Dog type
A=new Cat ()//At this time a indicates the pattern of cat type

2. Conditions and characteristics of polymorphism

The premise of polymorphism: there must be an inheritance relationship ( wrong argument )
can be an inheritance relationship, or it can be an implementation relationship (interface and implementation Class). correct
Characteristics of polymorphism:
Assigning a Class object to a parent class variable, showing a specific subclass feature at run time

2. Polymorphism Benefits

Suppose we now have the need to provide breeders with a way to feed animals to feed animals

No polymorphism, for different types of animals, we have to provide different feed methods to feed, very cumbersome, not concise.

So how do you provide a single way to feed all animals uniformly?

That's the benefit of polymorphism.

The different sub-class objects as the parent type of view, you can block different sub-class objects between the implementation of differences, so that the code to write common code to achieve universal programming to adapt to the different needs of the change,

Improved Code Extensibility

II. Multi-State transformation

1. Upward transformation

When a subclass object is assigned to a parent class reference, it is an upward transformation, and polymorphism itself is the process of upward transformation.

2. Downward transition

A child class object that has been transformed upward can use the format of coercion type conversion to convert the parent class reference to a subclass reference, which is a downward transformation. If you create a parent class object directly, you cannot change it down!

Run an error because we can't convert the cat into a dog

When do you use the upward transformation?

When you do not have to face a subclass type, you can use the upward transformation by increasing extensibility, or by using the functionality of the parent class to accomplish the appropriate action.

such as: Animal a = new Dog ();

A.eat ();

When do I use downward transformation?

When you want to use subclass-specific functionality, you need to use a downward transformation.

such as: Dog d = (dog) A; Down Transformation

D.lookhome ();// Lookhome method of calling Dog class

Benefits of Down conversion: You can use subclass-specific features.

The disadvantage is that it is necessary to face a specific subclass object, which is prone to classcastexception type conversion exceptions in the downward transition.

How to solve this shortcoming?

3.instanceof keywords

When this happens, we need to use the instance of this keyword

Instanceof Determines whether an object is an instance of a class
* Syntax Format: Boolean b= object A instance of Class B
* Determine if a object is an instance of a B object

Java Fundamentals 14-polymorphism (thought, transformation, instanceof)

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.