Java Fifth: The polymorphism of the three major features of object-oriented (OOP)

Source: Internet
Author: User
Tags access properties

Polymorphic (write code to live, improve code extensibility and maintainability)

Polymorphism in life: the same substance that exhibits different forms for different environments (water: gaseous, liquid, solid/carbon 12: diamond, graphite)

Polymorphism in the program: the same "interface" for different implementations, while performing different operations (that is, in the method to pass the parent class as a formal parameter, when called, passing in the instance of the subclass, and finally executing the method of overriding the parent class in the subclass)

The multi-state general Union method overrides, inherits the use, the parent class reference points to the subclass;

Multi-State Implementation steps:

1. Implementing Inheritance

2. At compile time, the parent class is passed in as a formal parameter type (the range is a little larger, a little blurry)

3. The runtime passes the instance of the subclass new out and finally executes the method that overrides the parent class in the subclass.

Types of conversions

1. Subclass conversion to parent class, called auto-transform, also called upward transformation

Eg:animal Animal = new Dog ();

Note: After the transition up, animal can only access properties and methods in the parent class, the properties and methods in the subclass cannot be accessed, and the calling is the method after overriding the parent class in the subclass;

2. Parent class converted to subclass, called cast, also called down transformation

Eg:animal Animal = new Dog ();

Dog dog = (dog) animal; At this point, the dog can only invoke properties and methods in the subclass and cannot access the properties and methods in the parent class.

Final keyword, the ultimate meaning

1. Modifier variable, the variable is a constant, cannot be re-assigned value

2. Modification method, the method cannot be overridden (in subclasses)

3. Modifier class, which cannot be inherited (the math class and the string class in the Eg:java.lang package)

Abstract keyword, the meaning of abstraction

Abstract methods: The most notable feature of using Abstact-modified methods is that there is no method body, which is a specification provided to subclasses for rewriting.

Abstract class: A class that uses the abstract modifier, an abstract class in which an abstract method can have attributes and a constructor method. But cannot be instantiated (not new), born to inherit animal animal = new Dog ();

Classes that have abstract methods, must be abstract classes, have to be abstract classes, or they will get an error.

The role of abstract classes: to prevent the writing of arbitrary procedures, to provide a specification;

Java design pattern (GOF), 23 kinds, is the predecessor to the classical problem summarizes the solution

Singleton mode: A class can only create one instance object, the implementation method: The construction method Privatization, provides a common static method to obtain the instance.

Implementation mode: 1. A hungry man style 2. Lazy type

Simple Factory mode (static Factory method): Can produce products that can reduce the coupling caused by the production of products is too strong (decoupling);

Implementation: 1. Writing the parent class and subclasses or interfaces and implementation classes

2. Write a static factory method that returns a value type of parent class or interface-to-polymorphic;

3. Dynamically create a subclass object based on the user's needs, and return

Package cn.zzsxt.demo7.factory;//creating a parent class Public Abstract classAXE { Public Abstract voidcut ();} Package cn.zzsxt.demo7.factory;//Create Stone Axe class Public classStoneaxe extends AXE {@Override Public voidCut () {System. out. println ("I am a stone axe, cut Japanese blunt! "); }}package cn.zzsxt.demo7.factory.copy;//creating the Axe class Public classSteelaxe extends axe{@Override Public voidCut () {System. out. println ("I am iron axe, cut the Japanese sharp! "); }}package cn.zzsxt.demo7.factory;//To create a static factory method Public classAxefactory { Public StaticAXE getinstance (String str) {AXE AXE=NULL; if(Str.equals ("Stone") ) {Axe=NewStoneaxe (); }Else if(Str.equals ("Steel") ) {Axe=NewSteelaxe (); }        returnAxe; }         Public Static voidMain (string[] args) {AXE AXE= Axefactory.getinstance ("Stone");        Axe.cut (); System. out. println ("========================="); AXE Axe2= Axefactory.getinstance ("Steel");    Axe2.cut (); }}

    

Java Fifth: The polymorphism of the three major features of object-oriented (OOP)

Related Article

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.