Polymorphic, abstract classes

Source: Internet
Author: User

Polymorphic:

Polymorphism: multiple forms;

Polymorphism refers to the ability of an object to have many forms;

Polymorphic Description: The same behavior; different implementations;

Polymorphic classification:

Static polymorphism: At compile time, the system can decide which method to call, so it is called compile-time polymorphism, the way of implementation is the overloading of the method, and the calling rule is the overloaded method in calling the corresponding class according to the type of the object when it is defined.

Dynamic polymorphism: In the running system can dynamically determine the object that the method refers to, so called run-time polymorphism, the way is to override the parent class with the same name member method, dynamic polymorphism is mainly through dynamic binding and rewriting mechanism to implement.

Technical basis:

1, Upward transformation technology: A reference variable of a parent class can point to a different subclass object, or a subclass object can be treated as a parent class type;

2. instanceof Keyword: instanceof keyword is used to judge the real type of object at run time;

3. Dynamic binding technology: The runtime executes the corresponding sub-method according to the time type of the object referred to by the parent class, and realizes polymorphism;

  Transformation of basic data types:

1, automatic type conversion, implicit conversion, small range of data types to a wide range of data type conversion

Risk-free/positive success;

int a = ' B ';d ouble f = 254;

2. Forced type conversion, explicit conversion, conversion from a wide range of data to a small range of data types

Risk/loss accuracy/no meaning

A = (int) 3.14;char C = (char) 12345670;

  conversions of reference data types --only classes that have inheritance relationships can use type conversions

public class Petbean {private string name;private int age;public String getName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

public class Dogbean extends Petbean {private int bonenum;public int getbonenum () {return bonenum;} public void Setbonenum (int bonenum) {this.bonenum = Bonenum;}}
public class Testmain {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stub

1, up type conversion, automatic type conversion, implicit type conversion, small range data type conversion to a wide range of data types

Risk-free \ Positive Success

Petbean p = new Petbean ();//The reference to the parent class can point to the object of the child class

2. Down type conversion, forced type conversion, explicit conversion, data type conversion from a wide range of data to a small range

Risk/May throw an exception, terminate the program's operation

Only after it is run, does this class reference point to the class object or to the parent class reference to the subclass object to succeed.

Dogbean dog = (Dogbean) p;

polymorphic Applications-polymorphic parameters:

A polymorphic parameter is when a form parameter of a method is a reference, and any object compatible with that reference can be passed to the method, allowing the method to accept formal parameters of different data types.

Polymorphic Applications-Heterogeneous collections:

Polymorphic Another common application is to wear a piece of data that is not of the same type, but has a common parent class, and a collection of different objects is called a heterogeneous collection.

 

Multi-State Summary:

Use a reference to the parent class type to point to the object of the child class;

The reference can only call methods defined in the parent class and cannot invoke a method unique to the subclass;

If a method in the parent class is overridden in a subclass, the method in the subclass is called when the method is called;

In polymorphism, subclasses can invoke all methods in the parent class;

Abstract class:

Declaring a method as an abstract method has two results:

1, the class must also be declared as abstract class, if a class contains an abstract method, then the class must also be abstract class;

2. Any subclass must override the abstract method, unless the subclass itself is an abstract class;

Keyword: abstract; When you use this keyword to modify a class, it means that the class is abstract class;

Note: An abstract class cannot produce an object, it can only act as a parent class;

1, the class with abstract method must be abstract class;

2, abstract classes do not necessarily have abstract methods;

3, abstract class, in addition to the abstract modifier, it is the same as the ordinary class, can have: attributes, constructs, the method has been implemented;

Cases:

public class Boy {private Girl mygirl;public Girl Getmygirl () {return mygirl;} public void Setmygirl (Girl mygirl) {this.mygirl = Mygirl;} public void Kiss () {System.out.println ("Give me a Kiss ~ ~ ~ ~ ~ ~ ~ ~"); This.myGirl.response ();}}
Public abstract class Girl {//abstract keyword means abstract//when it modifies a method, it indicates that the class has this method, but cannot determine the implementation of this method, it should be determined by its subclass public abstract void Respo NSE ();}
public class Puregirl extends Girl {

Subclasses must override abstract methods of abstract classes when inheriting abstract classes.
Otherwise, this subclass must also be an abstract class.

@Overridepublic void Response () {//TODO auto-generated method StubSystem.out.println ("Mmm ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~)}}
public class Boldgirl extends Girl {

Subclasses must override abstract methods of abstract classes when inheriting abstract classes.
Otherwise, this subclass must also be an abstract class.

@Overridepublic void Response () {//TODO auto-generated method StubSystem.out.println ("The mother flies up one foot ~ ~ ~ ~ ~");}
public class Testlove {/** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubboy B Oy = new Boy (); SYSTEM.OUT.PRINTLN ("Please choose the type of girl you like: 1, Innocent, 2, Savage.") ") int choice = new Scanner (system.in). Nextint (); Girl Girl = null;switch (choice) {Case 1:girl = new Puregirl (); Break;case 2:girl = new Boldgirl (); Boy.setmygirl (girl); Boy.kiss ();}}

  

  

Polymorphic, abstract 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.