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.
and protect,interface front use abstract,abstract, public static final can omitPublic Abstract interface Iplaygame {Public static final int a = 8;Public abstract void PlayGame (); Can't ... playGame () {};}Note: Methods in interfaces cannot have method bodies such as: Wrong-void GG () {} to-void GG ();Define interface: Public abstract interface Iplaygame {public static final int a = 6; public abstract void PlayGame ();} Inherited interface: public class PSP implements Iplaygame {public void Pla
Polymorphism is an important feature of object-oriented, it is simple to say: "An interface, a variety of implementations," is the same thing shows a variety of forms.Here's a quick overview of the code:Three conditions must be met to achieve polymorphism1 must have inheritance2 must be rewritten3 Child class object must be referenced by parent classpublic class Wine {public void Fun1 () {System.out.println
one Java polymorphic Polymorphism is the ability of the same behavior to have many different manifestations or forms. Polymorphism is the embodiment of multiple forms of objects. Like we said, "PetObject, it has a lot of different expressions or implementations, such as kittens, puppies, lizards and so on. So I went to the pet shop and said, "Please give me a pet
of differences, so that the code to write common code to achieve universal programming to adapt to the different needs of the change,Improved Code ExtensibilityII. Multi-State transformation1. Upward transformationWhen a subclass object is assigned to a parent class reference, it is an upward transformation, and polymorphism itself is the process of upward transformation.2. Downward transitionA child class
classMansextendsperson{98 Booleansmoking; About intID = 1002; - PublicMan () {101 Super();102 }103 104 PublicMans (Booleansmoking) { the Super();106 This. Smoking =smoking;107 }108 109 Public Booleanissmoking () { the returnsmoking;111 } the 113 Public voidSetsmoking (Booleansmoking) { the This. Smoking =smoking; the } the 117 Public voidWalk () {118System.out.println ("Men walking in a straight w
) {Return ("A and D");}Public String Show (A obj) {Return ("A and a");}}//Package homework;public class B extends a{Public String Show (B obj) {Return ("B and B");}Public String Show (A obj) {Return ("B and A");}}//Package homework;public class C extends b{}//Package homework;public class D extends b{}//Package homework;Import Org.omg.Messaging.SyncScopeHelper;Public class Lei {Public static void Main (string[] args) {a a1 = new A ();A a2 = new B ();b b = new B ();c C = new C ();D d = new D ();S
you want to invoke. So, if you find that the argument list of two or more methods is consistent during overloading, the compiler will make an error, because it cannot tell which method you want. Any method overloads are allowed in Java. Also note that if the names of the two methods in the overloaded two methods are the same as the argument list, but the method return type is different, this can also compile an error, because this cannot distinguish
("Computer find () be"); System.out.println ("Construction Method" +i); Find ();//here the call is actually a subclass of the method, but at this time the subclass of data (that is, I) is not instantiated, so in the printed data bits 0SYSTEM.OUT.PRINTLN ("Computer find () af")); } } PackageOrg.duotai; Public classCpuextendscomputer{Private intI=5; PublicCPU (inti) { This. i=i; System.out.println ("Construction Method Cpu.i=" +i); } Public voidfind () {System.out.println
In Java, polymorphism means the same behavior, different implementations.Among them, polymorphism is divided into static polymorphism and dynamic polymorphism. The difference between the two is that the compiler can determine the final result of the run time, that is, compil
Polymorphism in the thinking in JAVA3
People are often confused by, non-object-oriented features to Java, like method overloading, which are sometimes pre sented as object-oriented. Don ' t be fooled:if it isn ' t late binding, it isn ' t polymorphism
Translation by text
People are always confused by another object-oriented feature of
different methods of the same parameter name by different parameters between different methods of the same class.override : Occurs between a subclass and a parent class.B,this: The invocation takes place inside a class.Super: Occurs between a child class and a parent class, and the subclass invokes the initialization code, property, or method of the parent class through this keyword.Note: Since this and super can only be placed on the first line of the method, it is not possible to appear at th
Object-oriented this thing, in fact, we have not felt that they are in use, I must use in the future. I used to learn C # when seriously looked at once, has been studying Java. Just take a look.
Polymorphic problem:
Polymorphism in Java is divided into (1) compile-time polymorphism and (2) Run-time
will the print result be?CHILD.FUNC1 (68);}} The above program is a very typical polymorphic example. Subclass Child inherits the parent class father and overloads the Func1 () method of the parent class, overriding the Func2 () method of the parent class. The overloaded func1 (int i) and func1 () are no longer the same method, and because there is no func1 (int i) in the parent class, the reference child of the parent class type cannot call the func1 (int i) method. When the subclass overrides
reference to a subclass object in polymorphism
⒉ Down Transformation: Parent class to subclass, castPerson () p=new person (); Chinese c//ClassCastException class conversion exception, because P is not an instantiated object of Chinese, strong transfer causes class conversion exceptionPerson () p=new 中文版 (); Chinese c//ClassCastException class conversion exception, because P is not an instantiated object of Chinese, strong transfer causes class
Since polymorphism is one of the three essential characteristics of object-oriented (the other two are data abstraction and inheritance), why does C ++ not set the default method of method calling to dynamic binding, but should we mark it with the keyword virtual? Bruce Eckel mentioned in Thinking in C ++ that this is due to historical reasons. C ++ is developed from C, and C programmers are most concerned with performance issues, because dynamic bind
Java polymorphism first-time experience this article is intended for beginners of Java. We often see that Java is polymorphism in teaching materials, but general textbooks only stay in theory and there are few practical operations. Now I will briefly describe the concept of
of C is b,b and there is no match, continue to convert upward to get A , b in the incoming argument list is converted to A, match Show (a), call Show (a).Returns a and a3.a1.show (d)Ditto.Returns a and a.4.a2.show (b)The declaration is referred to as type A, the actual instance type is B, the method called is show (b), first we look at Show,show (D) in A, show (a), and show (b) in B, Show (a) where show (a) override show (a) in a , Show (B) method does not exist in a, exclude. So the alternativ
understand, generally will not be wrong. ④⑤ is a bit confused, why is not the output of "B and B" it?! Let's go back to polymorphism.Runtime polymorphism is one of the most powerful mechanisms for object-oriented programming code reuse, and the concept of dynamic can also be said to be "one interface, multiple methods". Java's implementation of runtime polymorphism is based on dynamic method scheduling, wh
Implementation principle of dynamic call polymorphism (override) in java method calling -- method table (III)
In the last two blog posts, we discussed java's overload and override, static assignment, and dynamic assignment. This blog post discusses how to implement dynamic assignment, that is, the implementation principle of multi-state override.Calling principles of overloading and rewriting for
authorized operations that are wrapped outside of the data. That is, the user does not need to know the implementation details of an object's internal methods, but can access the object based on the external interface (object name and parameters) provided by the object.Benefits:(1) Achieve a professional division of labor. Once the code that implements a particular function is encapsulated into a separate entity, the programmer can invoke it when nee
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.