Java Learning Lesson 15th (polymorphism and its basic applications)

Source: Internet
Author: User

Polymorphic :

Object-oriented third feature, definition: a certain kind of things exist in many forms, for example, the function has polymorphism, a function of the same name, its parameter list is not the same, its existence is not the same, there is the same function, placed in the parent class and placed in the subclass, its existence is not the same.

The object also has polymorphism.

Example: Animals have pigs, cats, dogs, etc.

Cats This object corresponds to the class is the Cat class

Cat x = new Cat ();

At the same time, the cat is also a kind of animal, you can turn the cat into an animal

Animal y = new Cat ();
Animal z = new Dog ();

Animals are the parents of dogs and cats in collective things.
The parent class reference points to the child class object

I. Overview


Object polymorphism//polymorphic representation, parent type pointing to Self object class Animal{}class cat extends Animal{}class Dog extends Animal{}public class Main{public static void Main (string[] args) {//An object two morphology animal small animal = new Cat ();//small animals through the cat to create objects, animals in the direction of * * * cat, such things as the cat shape, but also the animal form. * This is the polymorphism of things * that is, an object that corresponds to a different type * polymorphism in code: * (parent class/interface) reference to object of its subclass * */}}



Two, the advantages of polymorphism

Provides extensibility for code that can use later content (animals first, pigs only)

The following code shows:


Abstract class animal{abstract void Sing ();//called}class cat extends Animal{void sing () {System.out.println ("Meow Meow");} void Fun ()//cat's unique function {System.out.println ("catch Mouse");} Class Dog extends Animal{void sing () {System.out.println ("barking");} void Fun ()//dog's unique function {System.out.println ("housekeeping");}} Class Pig extends Animal{void sing () {System.out.println ("hem call");} void Fun () {System.out.println ("Arch");}} public class Main{public static void Main (string[] args) {//cat cat kitten = new Cat (); kitten sing ();//many cat cat two cat = new Cat (); Cat three Cat = new Cat (); Catqun (two cats); Catqun (three cats);//....//polymorphism, many animals dog puppy 1th = new Dog (), cat kitten 1th = new Cat (), Catqun (puppy 1th); Catqun (kitten 1th); Catqun (New Pig (););} static void Catqun (animal c)//animal c  = new Cat ()/dog ()/pig (); { C.sing ();}}



Iii. the drawbacks and preconditions of polymorphism


1. The drawbacks of polymorphism:


Pre-defined content cannot use content that is specific to later subclasses

static void Catqun (animal c) {c.sing ();//c.fun ();->animal there is no fun in this way}

PS: Of course you can use the cat static void Catqun (Cat c) to call, but we do not know how many species will appear in the end, the reuse of poor

2. The prerequisites for polymorphism:

⑴. Must have a relationship, either inherit, or implement
⑵. To have overrides (the parent class defines the function, the subclass is specifically implemented, the dog barks, the wolf barks, it's troublesome.) Dog department called, very Simple)
If you do not meet the ⑵, there is no cover to use polymorphism, such as: Dog Housekeeping, normal, Wolf housekeeping, this is not a problem.

The two preconditions of polymorphism are ensured, which can improve the extensibility of the program.

Iv. transformation

The code shows:


Abstract class animal{abstract void Sing ();//called}class cat extends Animal{void sing () {System.out.println ("Meow Meow");} void Fun ()//cat's unique function {System.out.println ("catch Mouse");} Class Dog extends Animal{void sing () {System.out.println ("barking");} void Fun ()//dog's unique function {System.out.println ("housekeeping");}} Class Pig extends Animal{void sing () {System.out.println ("hem call");} void Fun () {System.out.println ("Arch");}} public class Main{public static void Main (string[] args) {//previous Command object work/* * Cat kitten = new Cat ();      Kitten. Sing (); */animal a = new Cat ();//automatic type promotion, cat object lifted to animal, similar to byte x = 3;int y = x;a.sing ();//ps: Once the cat has been lifted to the animal, its unique features are inaccessible. Professional parlance, upward transformation. Purpose: Restrict access to unique features//if the cat's unique features are also available//You can move the object down, the cat C = (cat) a;//The animal A, down to the Cat Cc.fun ();//Downward transformation purposes: is to use a method that is unique to subclasses/*animal d = New Animal (); * Animal F = new Dog ();   *  Cat G = (cat) F;cat e = (cat) D; This type is not allowed, the small animal must be a cat */}}

Note: For transformation, the sub-class object is being transformed from start to finish: The Cat object transforms into an animal and transforms into a cat.
PS: Transformation is purposeful

Practice:


/* * BLF and BLF2 's story * BLF2 is BLF's son * */class blf{void function () {System.out.println ("write program in C + +");} void speaks English () {System.out.println ("Hello,world");}} Class BLF2 extends Blf{void function () {System.out.println ("write program in Java");} Void Speaking Chinese () {System.out.println ("Hello, World");}} public class Main{public static void Main (string[] args) {BLF x = new BLF2 ();//One day BLF2 impersonate blfx. function ();//can only be written in Java program, Because BLF2 only javax. Speak English ();//Can, let BLF2 like BLF, speak English//x. Speak Chinese ();//No, BLF2 has been converted upward to BLF, which prohibits the use of BLF2-specific features BLF2 z = (BLF2) x;//change back to Z. Speak Chinese ( );}}

Not finished ...


Java Learning Lesson 15th (polymorphism and its basic applications)

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.