Section 15th (polymorphism, interface and abstract class differences, relationships between classes)

Source: Internet
Author: User

definition of polymorphism: 1 polymorphism is the ability of the same behavior to have many different manifestations or forms 2 polymorphism is a manifestation of a variety of objects such as we say " Pets " This object, 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 , "the waiter gives me a kitten, a puppy or a lizard, and we say " pet ."  This object is polymorphic. The condition of polymorphism is: there is inheritance or implementation, there are methods to overwrite or implement, the parent class object (interface) points to the child class object
 Public classanimal{//member Methods     Public voideat () {System. out. println ("the animals are eating! "); }}/* Kitten*/ Public classCat extends animal{ Public voideat () {System. out. println ("The cat eats the fish! "); }        //Cat-specific methods     Public voidMove () {System. out. println ("Cat Walking Cat step! "); }    }/*Little Dog*/ Public classDog extends animal{ Public voideat () {System. out. println ("Puppy Chew Bones! "); }        } Public classtest01{//Java Portal     Public Static voidMain (string[] args) {Animal a=NewAnimal ();                A.eat (); Cat C=NewCat ();        C.eat ();                C.move (); Dog D=NewDog ();                   D.eat (); }}
/*about the Java language to transform up and down 1. Transition up:--and parent Class 2. Transition down: Parent-to-child note: Whether you are transitioning up or down, there must be an inheritance relationship between the two classes Benefits of using polymorphism: 1. Using polymorphism can reduce the coupling between code by 2. The expansion capability of the project is enhanced by the condition that the polymorphism is: 1. There is an inheritance or implementation of 2. There are methods of overwriting or implementing 3. Parent object (interface) points to child class object*/ Public classtest02{ Public Static voidMain (string[] args) {//The upward transformation is also known as: Automatic type conversion//a reference to a parent type points to a child class object//The program is divided into two stages: compile stage, run phase//the program compilation phase only knows that A1 is a animal type//The actual object in the heap when the program is running is the cat typeAnimal A1 =NewCat (); //the program is A1 by the compiler as a animal type during the compilation phase//so the program A1 reference binding in the compile phase is the Eat method in the animal class//While the program is running, the objects in the heap are actually cat types, and cat has rewritten the Eat method//so the way the program binds objects at runtime is the Eat method in cat (dynamic binding)a1.eat (); //down transformation: forcing type conversionsAnimal A2 =NewCat (); Cat C1=(Cat) A2;                        C1.move (); //Downward Transformation:/*Animal a3 = new Dog (); Cat C2 = (cat) A3; Error Java.lang.ClassCastException*/        //at the time of coercion type conversion, there is a risk to the program! //to avoid classcastexception, use instanceof in Java                /*usage: 1. The result of the instanceof operator is Boolean type 2. (reference instanceof Type)---> True/fal Se*/Animal A4=NewDog (); //A4 instanceof Cat If the result is true: A4 references to Java objects in the heap are Cat typesSystem. out. println (A4 instanceof Dog);//false//Recommendation: Use the instanceof operator to determine when doing a downward transformation, avoiding classcastexception        if(A4 instanceof Dog) {System. out. println ("for Real! "); Dog C3=(Dog) A4; }            }}
What is the difference between an interface and an abstract class? A) The interface describes the characteristics of the method, does not give the implementation, on the one hand to solve the single inheritance of Java, the implementation of a strong pluggable B) abstract class provides a partial implementation, abstract class is not instantiated, the existence of abstract class is mainly can be the common code to be ported to abstract Class C) interface-oriented programming, Instead of targeting specific programming (for abstract programming, not for specific programming) d) prioritize interfaces (because after inheriting an abstract class, this class will no longer inherit, so this type of flexibility is lost)
the relationships between classes 1 . Generalized relationships:          inheritance between classes and classes and  inheritance between interfaces and interfaces 2. Implementing relationships:          Class-to-interface implementations   3. Association relationship:          A class is connected to a class, and one can know the properties and methods of another class, using member variables in the Java  language to represent 4. Aggregation relationship:          One of the relationships is a strong correlation, is the whole and part of the relationship, such as: automobiles and tires, and its relationship with the association of different classes at the same level, and the aggregation of the relationship of the class at an unequal level, a representative of the whole, a representative part, in the Java language using instance variables.   5 . Synthetic relationships:          is a relationship that is stronger than the aggregation relationship, such as: people and limbs, the whole object determines the life cycle of some objects, and some objects only have a composition relationship with one object at a time, using instance variables in the Java language to embody 6  . Dependencies: Dependencies are weaker than association relationships, and are reflected in the Java language as return values, parameters, local variables, and static method calls

Section 15th (polymorphism, interface and abstract class differences, relationships between classes)

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.