Java polymorphism and multiple inheritance

Source: Internet
Author: User

/**
* For each operation in Java, the data type of the operation must be valid. The parent class has a method subclass that has
* You can use subclasses to generate objects wherever you can generate objects using the parent class.
*
* Polymorphism
* means that objects of different classes are allowed to respond to the same message.
* Polymorphism consists of parameterized polymorphism and inclusion polymorphism. The polymorphic language has the flexibility,
* Abstract, behavior sharing, code sharing advantages, a good solution to the application function with the same name problem.
* There are two forms of polymorphism: overloading and covering
* First say overload (overload), which occurs in the same class. It has nothing to do with what parent subclass, inheritance.
* Identify a function In addition to the function name, there are parameters (number and type) of the function. In other words, there can be two or more functions in a class,
* Call the same name and their parameters are different.
* They have nothing to do with different functions, just maybe they function similarly, so just name it, add readability, that's all!
* Again overwrite (override), is occurring in the subclass! In other words, there must be inheritance in case the overwrite occurs.
* We know that inheriting a class, there is a parent class all the way, if you feel which method is uncomfortable, the function will change
* Then re-implement that function in the subclass.
* When this method is called again, the procedure in the subclass is executed.
* The functions in the parent class are overwritten. (Of course, the function name and parameters are exactly the same as in the parent class when overridden.)
* Otherwise your method has no effect on the methods in the parent class, because they are two functions, not related.
*

Package Learnbyoneself;import java.awt.CardLayout; Public classPolymorphism {/** * For each operation in Java, the data type of the operation must be valid.     The parent class has a method subclass that has * all the ability to use the parent class to generate objects can be used to generate objects using subclasses.     * Polymorphism * means that objects of different classes are allowed to respond to the same message. * Polymorphism consists of parameterized polymorphism and inclusion polymorphism.     Polymorphism language has the advantage of flexibility, abstraction, behavior sharing and code sharing, which solves the problem of application function with the same name. * There are two forms of polymorphism: overloading and overriding * First, overloading (overload), which occurs in the same class.         It has nothing to do with what parent subclass, inheritance. * Identify a function In addition to the function name, there are parameters (number and type) of the function.         In other words, a class can have two or more functions, * called the same name and their arguments are different.         * They have nothing to do with different functions, just maybe they function similarly, so just name it, add readability, that's all! * Again overwrite (override), is occurring in the subclass!     In other words, there must be inheritance in case the overwrite occurs.       * We know that inheriting a class, there is a parent class of all methods, if you feel that the method is not comfortable, the function will change * Then the function is re-implemented in the subclass.     * When this method is called again, the procedure in the subclass is executed. * The functions in the parent class are overwritten. (Of course, the function name and argument are exactly the same as in the parent class, * Otherwise your method has no effect on the methods in the parent class, because they are two functions, not related) * **/             Public Static voidMain (string[] args) {Owls ol=NewOwls (); Brid BR=NewBrid ();        Br.brideat ();        Ol.brideat ();        Br.bridfiy ();        Ol.bridfiy ();        Br.bridname ();        Ol.bridname (); Ol.bridname ("OWL"); Br.bridname ("Kalmaegi"); Ol. Owleat ("OWL"); Ol. Owlfly ("OWL"); }    }//define a base class Bridclassbrid{String name="Brid";  Public voidbridfiy () {//TODO auto-generated Method StubSystem. out. println ("The Brids can fly"); }    Public voidbrideat () {//TODO auto-generated Method StubSystem. out. println ("The Brid need to eat food"); }    Public voidbridname (String name) {System. out. println ("My name is"+name); }    Public voidBridname () {System. out. println ("My name is"+name); }     }//Define a subclassclassOwls extends brid{ Public voidowlfly (String name) {//TODO auto-generated Method StubSystem. out. println (name+"can fly!"); }      Public voidowleat (String name) {//TODO auto-generated Method StubSystem. out. println (name +"need to eat food!"); }     }

Results:

The Brid need to eat foodthe brid need to eat foodthe Brids can flythe Brids can flyowl need to eatfood! OWL can fly!   is was is Kalmaegi

As you can tell by comparison, there are subclasses of the parent class.

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.