The understanding of polymorphism in Java

Source: Internet
Author: User

Recent learning of the polymorphism of Java below is a personal arrangement:

3 Prerequisites for polymorphic presence :
  1. To have inheritance
  2. To have a method of rewriting
  3. The parent class reference points to the subclass object (for the method defined in the parent class, if the method is overridden in the subclass, then a reference to the parent class type will call this method in the subclass, which is the dynamic connection)


For polymorphism, it can be summed up as:
One, a reference to the child class using the parent class type;
Second, the reference can only invoke methods and variables defined in the parent class;
Thirdly, if a method in the parent class is overridden in a subclass, the method in the subclass is called when the method is called; (dynamic connection, dynamic invocation)
variables cannot be overridden (overwritten),the concept of "overriding" is only for methods, and if a variable in the parent class is "overridden" in a subclass, an error is made at compile time.
If an inherited subclass inherits a method of the parent class and then overloads it, the reference to the parent class cannot use that method after the overload.

 Public classTextduotai { Public Static voidMain (string[] args) {//TODO auto-generated Method stub
/* At this time polymorphism is very good embodiment, you want to feed your pet, only need to add the relative animals, and food classes of subclasses, such as you have a turtle, just to write a turtle class let him inherit the animal class, and it corresponding food
* Talk, you want to feed the fish to your cat eat only need to call the feed method, the result of your cat eat Fish: Program output My name is Xiao Bai, I like to eat fish,
*/Master Master =NewMaster (); Master.feedfood (NewCat (),NewFish ()); }}
Define animal classes, animals have two member properties name and ageclassanimal{String name; intAge ; accessor and modifier for member variables are get and set methods PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; }
Definition of animal two methods animals are called Public voidAnimalcay () {System.out.println (); }
Animals have to eat. Public voideat () { System.out.println (); }}
Define CAT class and let Cat class inherit animal classclassCatextendsanimal{String name; intAge ; PublicCat () {System.out.println ("Small white"); }
Overriding the parent class method Public voidAnimalcay () {System.out.println ("Meow, Meow."); } Public voideat () {System.out.print ("I like Fish"); }}//defines the dog class, and allows the dog class to inherit the method of the animal classclassDogextendsanimal{String name; intAge ; PublicDog () {System.out.println ("My name is little black."); }
Rewriting the method of the husband class Public voidAnimalcay () {System.out.println ("Wang Wang called"); } Public voideat () {System.out.print ("I like to eat bones."); }}//Defining Food Classesclassfood{String name;
Public voidShowName () {System.out.println (); }}classFishextendsfood{String name;
Overriding the parent class method Public voidShowName () {System.out.println ("I like to eat fish."); }}classBoneextendsfood{String name;
Overriding the parent class method
   Public voidShowName () {
System.out.println (Bones);
}
}

//Define host classes, such as feeding animalsclassMaster {String name; PublicMaster () {} PublicMaster (String name) { This. Name =name; } Public voidFeedfood (Animal an,food food) {System.out.print (name+" "); An.eat (); System.out.println ("Favorite Food:"); Food.showname (); }}

In the above code we want to implement the host to feed animals, we define animals, cats and dogs are inherited from the animal class, defined the food class, and its two sub-species of fish and bones, we can see that polymorphism is the same reference or type of multiple references is called polymorphic

The understanding of polymorphism in Java

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.