Overloading, overriding, polymorphism, static binding, dynamic binding in Java

Source: Internet
Author: User


Overloading, English name is overload, is defined in a class more than one method with the same name , the number of parameters, parameter types and order cannot be the same. The return type can be the same or different.


public class Tstaticoverload {static int height; Tstaticoverload () {System.out.println ("planting a seedling"); height = 0;} Tstaticoverload (int initialheight) {height = initialheight; System.out.println ("Creating new Tree is" + height + "feet tall");} static void info () {System.out.println ("Tree is" + height + "feet tall");} static void info (String s) {System.out.println (S + ": Tree is" + height + "feet tall");}}

public class Testso {public static void main (String [] args) {tstaticoverload t = new tstaticoverload (5); Tstaticoverload.info (); Tstaticoverload.info ("overloading Method"); new Tstaticoverload ();}}


Out:creating new Tree is 5 feet talltree are 5 feet talloverloading Method:tree is 5 feet tallplanting a seedling


Rewrite, English name is overrid, refers to in the case of inheritance, the subclass defines a method in its base class that has the same name, the same return type, and the same parameters, and is called a subclass to rewrite the method of the base class. This is the necessary step to achieve polymorphism.


Polymorphism: Polymorphism is the ability of the same behavior to have many different manifestations or forms. Polymorphism has three major characteristics: Subclass inherits Parent class, subclass overrides parent class, parent class points to subclass.


public class Staticsupper {public static String Staticget () {return "Base staticget ()";} Public String Dynamicget () {return ' Base dynamicget () ';}} Class Staticsub extends Staticsupper {public static String Staticget () {return "Sub staticget ()";} Public String Dynamicget () {return "Sub dynamicget ()";}} Class Staticmub extends Staticsupper {public static String Staticget () {return "Mub staticget ()";} Public String Dynamicget () {return ' Mub dynamicget () ';}}

public class Staticpolymorphism {public static void main (String [] args) {Staticsupper sup1 = new Staticsub (); System.out.println (Sup1.staticget ()); System.out.println (Sup1.dynamicget ()); Staticsupper sup2 = new Staticmub (); System.out.println (Sup2.staticget ()); System.out.println (Sup2.dynamicget ());}}

Out:base staticget () Sub dynamicget () Base staticget () Mub dynamicget ()


Concept of program binding:
Binding refers to the invocation of a method associated with the class (method body) in which the method resides. For Java, bindings are divided into static and dynamic bindings, or pre-and late-binding.


Static bindings:
The method is already bound before the program executes, and is implemented by the compiler or other linker. For example: C.
For Java simple can be understood as the program compile time of the binding; In particular, the Java method is only final,static,private and the constructor method is pre-binding.


Dynamic binding:
Late binding: Dynamic binding means that the compiler does not know at compile time which method to invoke until the runtime binds to the type of the object.
If a language implements late binding, it must also provide mechanisms to determine the type of the object during run time and invoke the appropriate method, respectively. That is, the compiler still does not know the type of the object at this point, but the method invocation mechanism can investigate itself and find the correct method body. Different languages have a difference in how late binding is implemented. But at least we can think of it: they all have special types of information in the object.


Method overloads have static method overloads and common method overloads. Static method overloads are static bindings, which are passed by: Class name. method. The common method overload is dynamic binding, which is a method call through: an instance object reference. Method. Constructors can be overloaded, but cannot be overridden.


Static methods cannot be rewritten, and they are not polymorphic. Overloading is just a language feature, a syntax rule that is independent of polymorphism and is not related to object-oriented.



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Overloading, overriding, polymorphism, static binding, dynamic binding in Java

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.