简体   繁体   中英

Polymorphism and overriding confusion

Recently My teacher was teaching Polymorphism and he taught that there are two aspects in Polymorphism as

  1. Run-time Polymorphism

  2. Compile-time Polymorphism

And then He showed us this code

class A {
    void ab(){
        System.out.println("ab() in class A");
    }
}

class B extends A {
    void ab(){
        System.out.println("ab() in class B");
    }
}

class ObCo {
    public static void main(String args[]){
        A a1 = new B();
        a1.ab();
    }
}

The output is ab() in class B

And when I try the following code,

class A {
    int i = 10;
}

class B extends A {
    int i = 100;
}

class ObCo {
    public static void main(String args[]){
        A a1 = new B();
        System.out.println(a1.i);
    }
}

According to the previous code, the output should be 100 But the output is 10 Which rises up these questions in my mind.

  1. Is Polymorphism a concept only related to Overriding methods?
  2. Is the first code block relevant to Polymorphism?
  3. If yes, what is run-time Polymorphism and compile-time Polymorphism?
  4. If no, please give me an example where Polymorphism is seen.

Detailed answers on those questions will be highly appreciated.

Polymorphism applies to non-static non-final methods only. The code you tried is not polymorphism but field hiding (emphasis mine):

Within a class, a field that has the same name as a field in the superclass hides the superclass's field, even if their types are different. Within the subclass, the field in the superclass cannot be referenced by its simple name. Instead, the field must be accessed through super, which is covered in the next section. Generally speaking, we don't recommend hiding fields as it makes code difficult to read.

With this in mind, answers to your questions:

  1. Is Polymorphism a concept only related to Overriding methods?

Yes.

  1. Is the first code block is relevant to Polymorphism?

Yes.

  1. If yes, What is run-time Polymorphism and compile-time Polymorphism?

In Java, all non- static non- final methods are virtual methods by default, so if a subclass has a method with the same signature (same input types, same output type) that the super class, this method is being overridden even if that wasn't your intention. The compiler will even raise a warning stating that the method in the subclass must use the @Override annotation to inform other programmers that this method is being overridden.

In Java, according to this tutorial, it's called compile time polymorphism to method overloading (such an odd name).

4.If no, Please give me an example where Polymorphism is seen.

You already have such example for method overriding (dynamic).

Here's another example for method overloading (static):

public class Foo {
    public void print(int x) {
        System.out.println("Printing number: " + x);
    }
    public void print(String x) {
        System.out.println("Printing string: " + x);
    }
    public static void main(String[] args) {
        Foo foo = new Foo();
        foo.print(5);
        foo.print("5");
    }
}

Output:

Printing number: 5
Printing string: 5

  1. Is Polymorphism a concept only related to Overriding methods?

Polymorphism applies to METHODS only. To non-static and non-final METHODS .

  1. Is the first code block is relevant to Polymorphism?

Not really, in first block you don't override, you hide the ab() method. It's the rules in section 8.4.8.3 of the JLS , "Requirements in Overriding and Hiding":

  1. If yes, What is run-time Polymorphism and compile-time Polymorphism?

  1. If no, Please give me an example where Polymorphism is seen.

Better than writting a fast code, check this tutorial

Your questions:

Is Polymorphism a concept only related to Overriding methods?

Yes - Java only implements polymorphism on methods of classes. The way you tried it on a data type doesn't work.

Is the first code block is relevant to Polymorphism?

Yes, it's a pretty typical example of run-time polymorphism,

If yes, What is run-time Polymorphism and compile-time Polymorphism?

Your working example is a typical run time situation. We call this run-time because only when the code is actually running can we figure out that a1 is actually a instance of B not an instance of A

Compile-time polymorphism is the situation when one method is chosen over another method with the same name when the code is compiled because of the types associated with it.

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