简体   繁体   中英

Should I use static binding?

I am learning late binding and static binding. Now I am confused about these code.

Here is my analysis:

  1. hello() is non-static method, so we should use the dynamic binding, that is Child.
  2. But there is no hello() method in child class, so go to its super class. Find hello() and print the first line "Hello from parent call calssMethod".
  3. Since the classMethod() is static, so we should use static binding of c, that is also Child. So the output is "classMethod in Child".

So the out put should be

Hello from parent call calssMethod

classMethod in Child

class Parent{
    public static void classMethod() {
        System.out.println("classMethod in Parent");
    }
    public void instanceMethod() {
        System.out.println("InstanceMethod in Parent");
    }
    
    public void hello() {
        System.out.println("Hello from parent call calssMethod");
        classMethod();
    }
}

class Child extends Parent{
    public static void classMethod() {
        System.out.println("classMethod in Child");
    }
    public void instanceMethod() {
        System.out.println("InstanceMethod in Child");
    }
}
public class AA {
    public static void main(String[] args) {
        Child c = new Child();
        c.hello();
    }
}

Now, here is the problem. The IDE shows that the output is:

Hello from parent call calssMethod

classMethod in Parent

What's the right analysis process?

What if I make hello() method static?

Whenever we call child class's object, firstly it always find parent class and execute it. As you have static classMethod in both classes so it always run parent's classMethod not child's. You can achieve required answer only by overriding it.

If you make hello() method static even then it will give you same output.

Class (static) methods aren't overridden as instance methods. When you call the method 'hello()', it will use the method of the parent. When you refer to the class method there, you're referring to the method defined in the class 'Parent'.

Besides that, you should declare your Child instance as 'Parent c = new Child()'. Because you aren't adding new methods to your subclass but rather changing the implementation, you don't lose access to the methods of the subclass. If you were to have to method that returns a Parent object but you return a Child object declared as you did, you'd get problems.

EDIT: To add to this, usually there are 2 reasons to use inheritance: specialisation and extension.

For specialisation, you use define your methods in your superclass and your subclasses differ in how they implement those methods. For example a superclass Animal with subclasses Cat and Dog. 'Animal' has a method makeSound() . You can imagine that both subclasses are going to have a different implementation.

For extension, you use a superclass as a base class containing everything that overlaps. Other than that, the subclasses might have very different implementations and uses. A lot of interfaces have this kind of use.

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