简体   繁体   中英

how to check a method override or not

i have two question 1 overriding 2 compile time binding
hi i want to know that how i can check that sh() become override

is method argument play any role in method overriding ?

why we say that static method bind at compile time but actually static method allocate memory at the class loading time ? when i use javac tool that means i use compiler and i compile a java file ,so that moment static memory not allocate ,static memory allocate a class loading time then why say that static method use compile time binding

is class loading time same as compile time ? i am confuse

i know here method signature is different so no override here than what actually happens here explain

class A
    {
        void sh(char x){
        System.out.println("value of x :  "+x);
        }
    }
    class B extends A
    {
        public void sh(int x)
        {
            System.out.println("value of x"+x);
        }
    }
    class C
    {
        public static void main(String...Aa) /* ??? */
        {
            A a1=new B();
            //a1.show();
            a1.sh('a');
            a1.sh(10);
        }
    } 

The Java Language Spec states

An instance method m1, declared in class C, overrides another instance method m2, declared in class A iff all of the following are true:

  • C is a subclass of A.

  • The signature of m1 is a subsignature (§8.4.2) of the signature of m2.

  • Either:

    • m2 is public, protected, or declared with default access in the same package as C, or

    • m1 overrides a method m3 (m3 distinct from m1, m3 distinct from m2), such that m3 overrides m2.

  • Moreover, if m1 is not abstract, then m1 is said to implement any and all declarations of abstract methods that it overrides.

The definition for subsignature is here . You ask

is method argument play any role in method overriding ?

According to the above, yes very much so. You signatures have to match. In other words

public void sh(int x)

is not overriding

void sh(char x){

why we say that static method bind at compile time but actually static method allocate memory at the class loading time ?

At compile time, a method call is resolved on the static or declared type of the reference. In other words, the program won't compile if the type doesn't declare such a method. For static methods. If the method is static , then the method is immediately resolved and bound to the type it is called on. If it is an instance method, binding is resolve dynamically (late-binding) with polymorphism.

None of this has anything to do with class loading or allocating memory.

I'm not clear on what you're asking. However, when B extends A , B will also inherit the sh(char x) method. The sh(int x) method does not override this, since the argument type is different. So an object of class B will have two different methods named sh .

In your code, though, you declared a1 to be of type A . Even though it will (at run time) refer to an object of type B , as far as the compiler knows it is still type A . Therefore, the methods you can apply to this object are the ones declared in A (and its superclasses , if it had any, but it doesn't, other than Object ). The only method you have (besides the Object methods) is sh(char x) .

So when you say

a1.sh('a');
a1.sh(10);

the compiler will treat this as if the argument is a char , since the only method it will look at is the one that takes a char argument. This means that a1.sh(10) will call the sh in A with "character 10" as an argument--EDIT: no it won't; I tried it, and the compiler won't let me convert 10 to a char automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM