Java Learning Notes (10)--polymorphic

Source: Internet
Author: User

One, polymorphic 1, the various forms of the object

(1) Reference polymorphism:

References to the parent class can point to objects of this class

A reference to the parent class can point to the object of the child class

(2) Method polymorphism:

When you create this class object, the method that is called is the method of this class:

When you create a subclass object, the method that is called is a subclass method, and if it is not overridden, the method that inherits the parent class is called

(3) A reference to a parent class is not a method that is unique to a subclass.

Conversion of reference types in polymorphic states

1, up-type conversion (implicit/automatic type conversion)

Conversion of small types to large types

2, down type conversion (forced type conversion)

Conversion of large types to small types

3, instanceof operator, to solve the type of reference object, avoid the security of type conversion.

4, a demo

5. Use the instanceof operator to resolve the type of the referenced object and avoid a demo of the security problem of type conversion

Iii. abstract Class 1, syntax definition

Classes that use the abstract adornment in front of a class are abstract classes

2. Application Scenario:

(1) In some cases, a parent class simply knows how its subclasses should be, but does not know exactly how these subclasses implement these methods.

(2) An abstract class is abstracted from multiple subclasses with the same characteristics, and the abstract class is used as the template of the subclass to avoid the arbitrariness of the sub-class design.

3. Function:

Restricts certain methods that a subclass must implement, but does not focus on implementation details.

4. Rules of Use:

(1) Defining abstract classes using abstract

(2) Abstract definition abstraction method, only declaration, do not need to implement

(3) classes that contain abstract methods are abstract classes

(4) An abstract class can contain ordinary methods, or there can be no abstract methods.

(5) Abstract classes cannot be created directly, you can define reference variables

5, a demo

(1) Set up a mobile phone abstract class

 Package com.cnblogs;  Public Abstract class Telphone {    publicabstractvoid call ();      Public Abstract void sendMessage ();}

(2) Create an old phone cellphone class

 package   com.cnblogs;  public  class  CellPhone extends   Telphone {@Override  
   
    public  
    void  
     call ( { 
    // 
     TODO auto-generated method Stub
    System.out.println ("Call through the keyboard"  public  void   SendMessage () { //  TODO A    uto-generated method Stub  System.out.println ("Texting via keyboard" 

(3) Same as (2), set up only mobile phone class smartphone

 Packagecom.cnblogs; Public classSmartPhoneextendsTelphone {@Override Public voidCall () {//TODO auto-generated Method StubSystem.out.println ("Call with Voice"); } @Override Public voidSendMessage () {//TODO auto-generated Method StubSystem.out.println ("Use voice to send text messages"); }}

(4) Create entry class Init

 Package com.cnblogs;  Public class Init {    publicstaticvoid  main (string[] args) {        // TODO auto-generated Method stub        New CellPhone ();        Tel1.call ();        Tel1.sendmessage ();         New SmartPhone ();        Tel2.call ();        Tel2.sendmessage ();    }}

(5) Operation Result:

Iv. interface 1, interface definition in Java

A class is a concrete implementation, and the interface defines the specification that a batch of classes needs to follow, the interface does not care about the internal data of these classes, and does not care about the implementation of these class methods, it only specifies certain methods that must be provided in these classes.

2, the definition method:

Use the interface keyword:

[Modifier]  interface Interface Name [extends  Parent Interface 1, parent Interface 2 ...] {    0 ~ Multiple Constants define    0 ~ multiple abstract Method definitions}
3. Interface Definition:

The method in the interface can only be an abstract method and is always used, even if the public abstract modifier is not added to the definition, and the system is added by default.

4, using the interface:

A class can implement one or more interfaces, implementing an interface using the Implements keyword, a class in Java can inherit only one parent class, but it is possible to inherit multiple interfaces.

Format fixed, can not change extends and implements order of exchange.

5, a demo

(1) Define an interface:

 Package com.cnblogs;  Public Interface Iplaygame {    publicvoid  playGame ();}

(2) Smartphone reference interface

 Packagecom.cnblogs; Public classSmartPhoneextendsTelphoneImplementsIplaygame {@Override Public voidCall () {//TODO auto-generated Method StubSystem.out.println ("Call with Voice"); } @Override Public voidSendMessage () {//TODO auto-generated Method StubSystem.out.println ("Use voice to send text messages"); }         Public voidPlayGame () {System.out.println ("Mobile phone can play games"); }}

(3) New PSP class, reference interface

 Package com.cnblogs;  Public class Implements iplaygame {    @Override    publicvoid  playGame () {        //  TODO auto-generated method stub        System.out.println ("PSP can play Game");}    }

(4) How to use:

Newnew  Psp ();        Ip1.playgame (); Ip2.playgame ();

6. Using the interface

(1) Interfaces are often used in conjunction with anonymous internal classes during use

(2) Anonymous inner class definition:

An inner class without a name, much more focused on implementation than on the name of the implementation class

(3) syntax format:

New Interface (); {    publicvoid  method ()    {        System.out.println ("How the anonymous inner class implements the interface") ;    }}

(4) a demo

Iplaygame IP3 =NewIplaygame () {@Override Public voidPlayGame () {//TODO auto-generated Method StubSystem.out.println ("Implementing an interface using anonymous internal classes");                }        };                Ip3.playgame (); NewIplaygame () {@Override Public voidPlayGame () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Implementing interface 2 using anonymous internal classes"); }}.playgame ();

V. Introduction to UML 1, UML concepts

Java Learning Notes (10)--polymorphic

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.