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.
voidUsepci (PCI p)//PCI p = new netcard ()//an interface reference points to its own subclass object. + { A if(p!=NULL) at { - P.open (); - p.close (); - } - } - } in - //Nic class to classNetcardImplementsPCI + { - Public voidOpen () the { *System.out.println ("Netcard open"); $ }Panax Notoginseng Public voidClose () - { theSystem.out.println ("Netcard close"); + method (); A } the } + - //sound card class $ classSoundcardImplementsP
This. Age =Age ; - } to PublicString GetName () { + returnname; - } the Public voidsetName (String name) { * This. Name =name; $ }Panax Notoginseng - Public voidTest () { theSystem.out.println ("The method in the parent class has executed!!! "); + } A the Public voidfn () { +System.out.println ("Subclass does not have this method, but it also executes because of inheritance.")); - } $ } $ - classChildextendsParent { -
Polymorphism is the third feature of object-oriented language after data abstraction and inheritance.
Binding (binding) (looks like a transliteration word): Connecting a method's call to the method itself is called a binding, when the binding occurs before the program is run, is called a earlybinding, and when the program is run, the binding method is determined to be bound by the type of the object, It is also called run-time binding (Run-time bi
Intera;A = new B ();A. Fun ();A = new C ();A. Fun ();}}
Output result:
This is B
This is CIn the preceding example, class B and class C are two classes that implement interface intera, respectively implementing the interface method fun (), by assigning instances of class B and class C to interface reference A, the dynamic binding of methods during runtime is realized, making full use of "one interface, multiple methods "demonstrate the dynamic po
Java polymorphism (Dynamic binding)@author IxenosBinding
The invocation of a method is associated with the body of a method, called a (method call) Binding:1. Pre-binding: Binding before program execution (implemented by compiler and linker);2. late binding : At run time based on the type of object binding (also known as dynamic binding or runtime binding );A) Implementation conditions: Can judge the
multiple interfaces at the same timeWhy should I use an interface?1, to complement the single inheritance structure.2, the interface enriches the polymorphism of the object.3, interface-oriented programming (programming or architectural thinking)The goal of software development:A center, two basic points.Everything is centered on user needs,Ensure the software has good scalability and maintainabilityCohesion-poly, low-couplingCohesion: The ability of
1. polymorphic ConceptPolymorphism is the embodiment of multiple forms of objects. For example, we say "pet" This object, it has a lot of different expression or implementation, such as a kitten, puppies, lizards and so on. So I went to the pet shop and said, "Please give me a pet," the waiter gives me a kitten, a puppy or a lizard, and we say that the object of "pet" is polymorphic.Polymorphism in Java refers to the multiple states of a reference (ty
Object-Oriented: class: A concrete object abstract that can be abstracted to represent a class of object objects with the same characteristics and behavior: is instantiated by the class, the object is an instance of the class Java in the understanding of polymorphism to make an analogy of the father person has behavior this method, which includes several actions: Eat, sleep, walk father has three sons, thre
polymorphicEncapsulation creates a new data type by merging features and behaviors.The function of polymorphism is to eliminate the coupling relationship between types.method Call BindingDefinition: Associating a method invocation with the same method topic is called a binding.Binding (implemented by the compiler and linker, if any) before the program executes is called early binding. Binding based on the type of the object at run time is called late
Intera A;A= new B ();A.fun ();A = new C ();A.fun ();}}
The output results are:
This is B
This is CIn the example above, Class B and Class C are two classes that implement Interface Intera. Implementing the Interface Method Fun (), by assigning the instance of Class B and Class C to interface reference A, we implement the dynamic binding of the method at runtime, and make full use of "one interface, multiple methods" to show the dynamic polymorphi
Java Implementation of Polymorphism
Basic knowledge Review
Polymorphism, that is, overloading and rewriting. the overload occurs in a class. override occurs in the subclass, which means that the subclass overrides the method with the same name as the parent class. just learning the language doesn't have to be so clear. You only need to know how to use it. If you
Public Static voidMain (string[] args) { at NewRoundglyph (5); - } -}Output:Glyph () before draw ()Roundglyph.draw (). Radius = 0Glyph () after draw ()Roundglyph.roundglyph (). RADIUS = 5Why is this output? This requires a clear understanding of the order in which constructors are called in Java?(1) initialize the storage space allocated to the object into binary 0 before anything else occurs;(2) call the base class constructor. Recu
Reference and excerpt from: http://www.runoob.com/java/java-polymorphism.htmlPolymorphicPolymorphism is the ability of a behavior to have many different forms of expression.Polymorphism is the same interface that uses different instances to perform different operations.Three prerequisites for polymorphic presence:1. Inheritance 2. Rewrite 3. Parent class reference to child class objectsuch as parent a = new
Override after Java5 onward { return new Dog ();}}Dog is a sub type of animal, therefore different return type are OK heredifference between overloading and overriding
Method Overloading
Method overriding
Parameter must is different and name must be same.
Both name and parameter must be same.
Compile time polymorphism.
Runtime polymorphism
program does not need to retrieve the method table at run time, can improve the efficiency of code execution. In Java, methods that are modified by static or private are implicitly declared final, because dynamic binding has no meaning.Because dynamic binding consumes resources and is not necessary in many cases, some programmers think that unless there is sufficient reason to use polymorphism, all methods
alsoIt is the carding of their own knowledge to supplement. That's why I'm here. (Mamma Mia, it seems to run away ...) )
Inheritance of ClassesSyntax rules:Class Subclass extends Parent classNote: 1, the parent class type is public, protected, and default when the tragic inheritance.2. In Java, subclasses can inherit only one parent class.3. All Java classes are subclasses of object.4. The Super keyw
subclass, which I will summarize (upward transformation ): 1. If a field with the same name is a parent class.2. If the subclass overwrites the method of the parent class, the subclass method is called.3. If the parent class does not have a subclass-defined method, the compilation will be incorrect.Ah, the result is clear.Can you help me understand this sentence: the object type is determined by the class that creates it, rather than the variable that declares it. If so, isn't the Son class obj
Java object-oriented polymorphismPolymorphism Performance in Java:1, overloading and rewriting of methods (overwrite);2, the parent class can be used to point to the specific implementation of subclasses, and can be changed at any time to other subclasses of the specific implementation;Let's start with a parent class animal:1 Package Com.java1234.chap03.sec13; 2 3 Public class Animal {4 5 Public v
Polymorphism:---The various morphological essence of an object: the reference of the parent class points to the subclass object---Premise: there must be an inheritance or implement class Animal{}class dog extends Animal{}main{dog d = new Dog ();//Native Animal dog = new Dog (); Polymorphic} Note: objects created by polymorphic forms can only access member methods that are common to the child parent class, and the result is a subclass-specific member m
1. Using inheritance to develop superclass (superclass)2. Use super keyword to evoke super class construction method3. Overriding methods in Super-class4. Distinguish between override and overload5. Explore the ToString () class in the object class6. Discovery of polymorphism and dynamic binding7. Describe why it is necessary to interpret the downward transition8. Explore the Equals method in the object class9. Storage, retrieval, implementation of Ar
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.