The benefits and drawbacks of java8-3 polymorphism and the understanding of polymorphism

Source: Internet
Author: User


Benefits of Polymorphism:
A: Improved maintenance of code (inheritance guarantee)
B: Improved code extensibility (guaranteed by polymorphism)

Cat and Dog Case code

1 classAnimal {2      Public voideat () {3System.out.println ("Eat");4     }5     6      Public voidsleep () {7System.out.println ("Sleep");8     }9 }Ten  One classDogextendsAnimal { A      Public voideat () { -System.out.println ("Dog eats meat"); -     } the      -      Public voidsleep () { -System.out.println ("The dog is standing and sleeping"); -     } + } -  + classCatextendsAnimal { A      Public voideat () { atSystem.out.println ("Cat Eats fish"); -     } -      -      Public voidsleep () { -System.out.println ("The cat sleeps on its knees"); -     } in } -  to classPigextendsAnimal { +      Public voideat () { -System.out.println ("Pig eats cabbage"); the     } *      $      Public voidsleep () {Panax NotoginsengSystem.out.println ("Pig sleeping on the side"); -     } the } +  A //tool classes for animal operations the classAnimaltool { +     PrivateAnimaltool () {} -  $     /* $ //Call the function of the cat - Public static void Usecat (Cat c) { - c.eat (); the c.sleep (); -     }Wuyi      the //Call the function of the dog - Public static void Usedog (Dog d) { Wu d.eat (); - d.sleep (); About     } $      - //Call the function of the pig - Public static void Usepig (Pig p) { - p.eat (); A p.sleep (); +     } the     */ -      Public Static voidUseanimal (Animal a) { $ a.eat (); the a.sleep (); the     } the     //To classify all possible as animals. the } -  in classDuoTaiDemo2 { the      Public Static voidMain (string[] args) { the         //I like cats and I have one. AboutCat C =NewCat (); the c.eat (); the c.sleep (); the          +         //I like cats very much, so I have another one. -Cat C2 =NewCat (); the c2.eat ();Bayi c2.sleep (); the          the         //I especially like cats, and I have another one. -Cat C3 =NewCat (); - c3.eat (); the c3.sleep (); the         //... theSystem.out.println ("--------------"); the         //The problem is, I have a lot of cats, every time you create an object is acceptable -         //but what about calling the method, don't you think it's very similar? Just the object name is not the same.  the         //We're going to use methods to improve the         //call Way Improved version the         //Usecat (c);94         //Usecat (C2); the         //Usecat (C3); the          the         //Animaltool.usecat (c);98         //Animaltool.usecat (C2); About         //Animaltool.usecat (C3); -         101 Animaltool.useanimal (c);102 animaltool.useanimal (C2);103 Animaltool.useanimal (C3);104System.out.println ("--------------"); the         106         //I like dogs .107Dog d =NewDog ();108Dog D2 =NewDog ();109Dog D3 =NewDog (); the         //Animaltool.usedog (d);111         //Animaltool.usedog (D2); the         //Animaltool.usedog (D3);113 Animaltool.useanimal (d); the Animaltool.useanimal (D2); the Animaltool.useanimal (D3); theSystem.out.println ("--------------");117         118         //I like pet pigs .119         //define a pig class that inherits from the animal, provides two methods, and also adds the class method call to the tool class -Pig p =NewPig ();121Pig P2 =NewPig ();122Pig P3 =NewPig ();123         //Animaltool.usepig (p);124         //Animaltool.usepig (p2); the         //Animaltool.usepig (p3);126 Animaltool.useanimal (p);127 animaltool.useanimal (p2); - Animaltool.useanimal (p3);129System.out.println ("--------------"); the         131         //I like pet wolves, tigers, leopards ... the         //define the corresponding class, inherit from the animal, provide the corresponding method override, and add the method call in the tool class133         //the first few must be written, and I have no opinion of134         //However, the tool class is changed every time, trouble does not135         //I just thought, could you stop it?136         //too simple: Write all the animals. The question is what is the name, and what needs to be added to it ?137         //use a different solution instead. 138         139     } $     141     /*142 //Call the function of the cat143 Public static void Usecat (Cat c) {144 c.eat ();145 c.sleep ();146     }147     148 //Call the function of the dog149 Public static void Usedog (Dog d) { Max d.eat ();151 d.sleep (); the     }153     */154}

2.

Disadvantages of polymorphism:
You cannot use features that are unique to subclasses.

I just want to use the special features of subclasses, okay?
Yes.

How to use it?
A: You can call A method to create A subclass object. (yes, but a lot of the time is unreasonable.) Also, it takes up too much memory)
B: Cast a reference to the parent class into a reference to the subclass. (Downward transition)

Issues of transformation between objects:
Upward transformation:
Fu f = new Zi ();
Downward transformation:
Zi z = (Zi) F; Requires that the F must be capable of being converted to Zi. (affiliation)

The problem of polymorphism is understood:
Class Confucius Father {
public int age = 40;

public void Teach () {
System.out.println ("Explaining Javase");
}
}

Class Confucius extends Confucius Father {
public int age = 20;

public void Teach () {
System.out.println ("Explaining the Analects");
}

public void PlayGame () {
SYSTEM.OUT.PRINTLN ("League of Legends");
}
}

Java Training Special Fire, many people to ask Confucius Father to lecture, this day, Confucius father was invited to go
But there are still people to please, left Confucius at home, the price is quite high. Confucius thought, can I think about it?
Then put on dad's clothes, take Daddy's eyes, stick to father's beard. Just start pretending to be dad.
Upward transformation
Confucius Father K-father = new Confucius ();
Go to someone else's place
System.out.println (K-Father age); 40
K-dad teach (); Explaining the Analects
K-dad PlayGame (); This is what a son can do.


I'm done, I'm home from work.
Take off daddy's gear and put on your gear.
Down transformation
Confucius K = (Confucius) K-Father;
System.out.println (K.age); 20
K.teach (); Explaining the Analects
K.playgame (); League of Legends

2. Possible anomalies in the downward transition:
ClassCastException: Type Conversion exception
Generally easy to appear in a polymorphic downward transition

The benefits and drawbacks of java8-3 polymorphism and the understanding of polymorphism

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.