how to achieve polymorphism in java

Alibabacloud.com offers a wide variety of articles about how to achieve polymorphism in java, easily find your how to achieve polymorphism in java information here online.

Understanding of Java Polymorphism

Java polymorphism, how to understand a parent class reference to a subclass object to understand polymorphism, first of all, you need to know what is "upward transformation". I defined a subclass cat, which inherits the animal class, and the latter is the parent class. I can pass Cat C=NewCat (); Instantiate a Cat object, this is not difficult to understand. But

Java polymorphism (polymorphic), dynamic bind (dynamically binding), late binding (late binding)

Today, we are talking about the most important thing in Java object-oriented, polymorphic. Polymorphism allows us to maximize the reusability of our programs, which is why we learn polymorphism."Polymorphic" (polymorphic) is also called "dynamic binding", also called "Late binding" (late binding).Dynamic binding refers to "the actual type of the referenced object

Java object-oriented polymorphism and interfaces

One, polymorphic1. By polymorphism, you can reduce the amount of code in a class to improve the scalability of your code. Inheritance is the basis of polymorphism, and there is no polymorphism without inheritance.2. Conversion of the handle class to the parent class is called upward transformation, and the type conversion is automatic. Converting a parent class t

Polymorphism in Java, conversion of reference types

Static voidMain (string[] args) {Bridge bri=NewBridge (); Animal Animal=bri;//Upward Transformation//Bridge bri2=animal; conversion down, error requires use of castBridge bri1= (bridge) animal;//forcing//Tiger tig= (Tiger) animal;//Although the compiler did not error, the runtime will be wrong, we know that animal is briger conversion, it can not be converted to Tiger//Tiger tiger= (Tiger) BRI; //cannot cast from Bridge to Tiger//use instanceof to judge if(AnimalinstanceofTiger) {Tiger T

Thinking in Java Reading Note (8. Polymorphism)

1. Polymorphism OverviewPolymorphism does what and how by separating , separating the interface from the implementation from another angle . Polymorphism not only improves the organization and readability of code, it also creates extensible programs---programs that can "grow" whenever a project is initially created or when new functionality is needed.  Encapsulation Creates a new data type by merging featur

Java Learning Diary Week3 Object-oriented 2: encapsulation, inheritance, polymorphism

One, pack (package)1.package:Why is the package required?To resolve the problem of duplicate names between classes.For ease of management class: The right class is located in the appropriate package!How does the package work?is usually the first non-annotative statement of a class.Package Name: The domain name is written backwards, plus the module name, and the internal management class.In fact, internal implementation is by the directory structure to do.Com.sun.testCom.oracle.test (COM.ORACLE.T

Java polymorphism from the JVM point of view

Three prerequisites for Java polymorphism:1. Inheritance2. Subclasses overriding parent class methods3. Parent class reference to child class objectAnd then look at an exampleThe output is:It is concluded that when the three conditions of the Java polymorphism are full, it is found that the c.eat () call is actually ea

Java polymorphism (Dynamic binding)

method of the class. The other method is to create the object first and then invoke it through the object. Package jin.feng1; Class constructor11{ void Draw () {System.out.println ("Constructor11 Draw ()");} Public Constructor11 () { System.out.println ("Constructor11:before Draw ()"); Draw (); System.out.println ("Constructor11:after Draw ()"); } } class Constructor12 extends constructor11{ private int radius=2; Public Constructor12 () { System.out.println ("Constructor12:before

Java-polymorphism, Method overloading

method, and at least one parameter is better than the other method, it is the best feasible method, here the difference and good is that the exact match is better than the extended conversion, but also the extension of the conversion, there is still good and bad problem, the extension of the transformation has two pathsByte-short-int-long-float-doubleChar-int-long-float-doubleThe types on the left side of these two paths can be converted to the right type, but the closer the source type is to t

Java Object-oriented polymorphism

parent type to a subtype. For down-type conversions, you must explicitly specify (mandatory type must be used)Packagecom.yuanzijian01;publicclasspolytest2{publicstaticvoidmain ( String[]args) {//TODO Auto-generated method stub/*animala=newdog ();D Ogdog = (Dog) a;dog.sing (); Animalb=newcat (); catc= (Cat) b;c.sing (); *//*animalanimal1=newcat (); Animalanimal2=newanimal (); animal2=animal1;animal2.sing (); *//*animalanimal1=newcat (); Animalanimal2=newanimal (); animal1=animal2;animal1.sing ()

Knowledge points about inheritance, classes, polymorphism, and interfaces in Java

behavior depending on the optional features and behavior of the outside, with an object-oriented perspective, so that the class is as simple as possibleInterface Benefits: Separating the design from the implementation, hiding the implementation from the external (caller) (and usually the caller does not need to be concerned about implementation); interface-oriented programming is the core of OOPInterface inheritance Define a Hockeylistener interface to inherit the Sportslistener interface: Publ

Java-interface and polymorphism-

# #枚举只要switch (XX) inside the xx is an enumeration type, then the inside of the case only a few are already in the enumeration predefined variables can be selected,Subclass to Parent class (upward transformation)Parent class rotor class (downward transition) The parent class is the rotor class, sometimes it's OK, sometimes not, so you have to cast.# #自己设置的实验My doubts are: this hero address value and my sub-class Adhero address value is exactly the same, then why this hero can't see its sub-categ

Java abstract class, interface, polymorphism, abstract method, one enumeration

HEAD first this series of books, really let people produce the pleasure of reading ~ ~:)The same as the science of tomorrow.InterfaceNose { Public intIMethod ();}Abstract classPicassoImplementsNose { Public intIMethod () {return7; }}classClownsextendsPicasso {//Pass}classActsextendsPicasso { Public intIMethod () {return5; }} Public classDotcombustextendsClowns { Public Static voidmain (String [] args) {Nose [] I=NewNose [3]; i[0] =NewActs (); i[1] =Newclowns (); i[2] =NewDotcombust (); for(intx

Java Dynamic Binding/polymorphism

class object's move () pointer to the parent class point () method, because there is inheritance, there is a parent class reference to the subclass object, there is a rewrite, Java will perform a dynamic binding mechanism, so that the source code in P.move (1,-1) called the subclass of the Move () method. Move () change the black line to the blue-pointing Move () method.4, the parent class refers to the child class object, can only call methods of th

Java Polymorphism (ii)

ASystem.out.println (A2.show (c));//B and ASystem.out.println (A2.show (d));//A and D /*** b b = new B (); Class B Creates a new normal sample object, and B.show (b) is obviously the result of B and b; * B.show (c) The parent and subclass do not have the method (b is not found, instead of the superclass a in B to find), so go to the third excellent * First class, because C is a subclass of B, so to Find Show (b obj) in class B, end result B and b; * B.show (d) not found in class

Some problems needing attention in Java polymorphism

reference refers to a child class object.A a3 = new A ();b B3 = new B ();B3 = (B) a3;//error, parent class refers to the parent class object and cannot be cast to a subclass reference.A a4 = new B ();b B4 = new B ();B4 = (B) a4;//is correct, at which point the parent class refers to the subclass object. You can cast to a subclass reference, but you must use a castMost of the blog from the network, if there is a copyright issue please contact me to delete or modify, blog content mainly for their

Three characteristics of Java-----------------------polymorphism

The polymorphism of three major features of JavaPolymorphic definition: means that objects of different classes are allowed to respond to the same message, that is, the same message can be used in various ways depending on the sending object.As far as I am concerned, it is still a bit difficult for a new person to understand the definition at the outset (perhaps I am a bit stupid). I think the Java way to u

Java Basic learning-polymorphism

Off-topic: summed up many years of learning experience, have to say, sleep is a learner's necessities! The so-called "early to ruin the day" is not unreasonable, especially for coders, a few days is not overtime to Night. well, I admit to beginners java, I yesterday, the head of the road, Java inheritance and polymorphism, see I was a little confused force. In fa

6.Java polymorphism and dynamic binding

, people also has the default parent class object (which will be explained later), which inherits the method of object, so the methods listed above are not complete.Assuming the Teacher class overrides the GetName () method in the People class, and a new method Raisesalary (double) is added, its argument list is:Say (String), People.say (String)GetName (), Teacher.getname ()Getage (), People.getage ()Raisesalary (double), teacher.raisesalary (double)At run time, the process of calling the Obj.ge

Java Getting started the second quarter the fourth chapter polymorphism

This is my lesson Web Java course notes, the original video link is: http://www.imooc.com/learn/1244-1 polymorphism in Javadefinition: Multiple forms of an object1. Quoting polymorphicReferences to the parent class can point to objects of this class;a reference to the parent class can also point to the object of the child classinheritance is the basis of polymorphism

Total Pages: 15 1 .... 11 12 13 14 15 Go to: Go

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.

not found

404! Not Found!

Sorry, you’ve landed on an unexplored planet!

Return Home
phone Contact Us
not found

404! Not Found!

Sorry, you’ve landed on an unexplored planet!

Return Home
phone Contact Us

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.