For Java, the main function is a virtual function and a little understanding of polymorphism

Source: Internet
Author: User

Question:

1. In Java, since the main function is defined as: public static void Main (string[] args), you must define static if you want to invoke the rest of the functions in the main function. In addition, when you call a member function written by another class, you do not need the member function to be static, which is why?

Analysis:

A static method is owned by a class, not a static method that belongs to the object of a class. In other words, to invoke a non-static method, you must first call new to get the object of a class, and the system allocates memory for it before accessing the corresponding non-static member function through the object. While static methods are owned by a class, static methods do not necessarily require classes to operate, or they can be invoked by using the class name, which defines the static method in which class is called.

Since the main function is the entry point of the program, the main function cannot wait for which object to create, allocate memory, and then call him. A static method can allow the method to be called before the object is created. Naturally, the main function must be static.

Instead, in the main function, by using new to create an object of the other class, the system allocates a piece of memory to it, and then it can call the other member functions of the class.

All in all, static methods do not have access to non-static methods, primarily memory relationships. Because the static method obtains the memory before the non-static method, accesses the non-static method in the static method, because at this time the non-static method has not obtained the memory address, of course or error. What if, in a static method, the address of a class is obtained by using new, and then the non-static method of accessing the object is performed? You can test it:

 Public Static void Showresult (fatherclass father)    {        father.first_function ();        Father.second_function ();        Father.third_function ();                Fatherclass fathertemp;                 New Fatherclass ();        Fathertemp.first_function ();    }

As you can see, in this static method, you create an object for another class Fatherclass call new , and then you can access the member functions in that class through the object, even if he is not a static method and does not give an error.

The entire program code is:

Package Multiple;public class Multiple {public static void Showresult (Fatherclass father) {Father.first_fun        Ction ();        Father.second_function ();                Father.third_function ();                Fatherclass fathertemp;        Fathertemp = new Fatherclass ();    Fathertemp.first_function (); } public static void Main (string[] args) {//TODO code application logic here System.out.println        ("First place text:");        Fatherclass father = new Fatherclass ();                Sonclass son = new Sonclass ();                Showresult (father);        System.out.println ("Second Place Text:");    Showresult (son);    }}class fatherclass{public void First_function () {System.out.println ("fatherclass::first_function");    } public void Second_function () {System.out.println ("fatherclass::second_function"); } public void Third_function () {System.out.println ("fatherclass::third_function"); }}class Sonclass extends fatherclass{public void first_function () {System.out.println ("Sonclass::first_fun    Ction ");    } public void Second_function () {System.out.println ("sonclass::second_function");    } public void Third_function () {System.out.println ("sonclass::third_function"); }}

< polymorphism >

In the C + + language, because its methods are not dynamically bound by default, that is, not late binding. To achieve the flexibility of a method with late-bound properties, you need to add the virtual keyword to it before the method. For the Java language, it is not necessary because, in Java, dynamic binding is its default behavior and does not need to add additional keywords to implement.

C + + code:

#include <iostream>using namespace std;classcfatherclass{ Public: Cfatherclass () {}~Cfatherclass () {} Public: Virtualvoidfirst_function () {cout<< "Cfatherclass::first_function" <<Endl; } Virtualvoidsecond_function () {cout<< "Cfatherclass::second_function" <<Endl; } Virtualvoidthird_function () {cout<< "Cfatherclass::third_function" <<Endl; }};classCsonclass: Publiccfatherclass{ Public: Csonclass () {}~Csonclass () {} Public: Virtualvoidfirst_function () {cout<< "Csonclass::first_function" <<Endl; } Virtualvoidsecond_function () {cout<< "Csonclass::second_function" <<Endl; } Virtualvoidthird_function () {cout<< "Csonclass::third_function" <<Endl; }};voidShowresult (Cfatherclass *father) {Father-first_function (); Father-second_function (); Father-third_function ();}intMainvoid) {Cfatherclass father;    Csonclass Son; Showresult (&father); Showresult (&son); return0;}

A summary of the implementation principles of the two for polymorphism:

<C++>

In C + +, if the virtual keyword is added before a function in the class, indicating that the function is a virtual function, the compiler creates a table (virtual function table) for each class that contains virtual functions. In a virtual function table, the compiler places a virtual function address for a particular class. In each class with a virtual function, the compiler secretly places a pointer to the virtual function table of the object. When a virtual function call is made through a base-class pointer, the compiler's static inserts can take this pointer and find the code of the function address in the virtual function table, so that the correct function can be called and the late binding will occur.

<Java>

In Java, in order to perform late binding, Java uses a small piece of special code instead of an absolute address call. This code uses the information stored in the object to calculate the code for the method body. Typically, a subclass is searched first to see if there is a calling method, if there is a call, if not, to look up, to see if the method exists for the parent class, or to have the call.

The main function in Java is a virtual function and a little understanding of polymorphism

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.