本文介绍了Java多态性概念的困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java中的多态性概念有些困惑,因为不同的作者写的方式不同.

情况-1

动态(运行时)多态---方法重载和使用实例方法进行方法重载是动态多态的示例.

静态(编译时)多态性---方法重载和使用静态方法的方法覆盖;使用私有或最终方法进行方法重载是静态多态性的示例

案例2

方法重载是编译时/静态多态的一个例子

方法重载是运行时/动态多态性的一个示例

那么哪种情况是正确的?Java支持静态或动态多态吗?

解决方案

编译时间 Runtime 多态性与呼叫被解析的时间直接相关.

编译时多态中,调用在编译时解析.方法重载是编译时多态的一个示例. 重载是 ,无论是实例级还是类级

示例:

 public class Sample {
    static void doSomething(InputStream is) {
        System.out.println("is");
    }

    static void doSomething(OutputStream os) {
        System.out.println("os");
    }

    public static void main(String args[]) {
        System.out.println(doSomething(null)); // "compilation" error . Ambiguous call. Both InputStream and OutputStream can take `null` argument.
    }

}

接下来,运行时多态性:这里会在编译时检查调用/方法签名是否存在,但在运行时会解析实际调用.示例:

class ParentOfSample {
    void testingOverriding(String s) {
        System.out.println("Parent..");
    }
}

public class Sample extends ParentOfSample {
    static void doSomething(InputStream is) {
        System.out.println("is");
    }

    static void doSomething(OutputStream os) {
        System.out.println("os");
    }

    void testingOverriding(String s) {
        System.out.println("Sample..");
    }

    public static void main(String args[]) {
        ParentOfSample s = new Sample();
        s.testingOverriding(null);
    }
}

O/P:样本.请注意,在覆盖期间,方法签名是相同的.

因此,最重要的是:第二种情况是正确的. Java支持静态动态多态.

i am bit confused over polymorphism concept in java because of different authors writing it differently.

Case -1

Dynamic (run time) polymorphism---Method overloading and method overriding using instance methods are the examples for dynamic polymorphism.

Static (compile time) polymorphism ---Method overloading and method overriding using static methods; method overriding using private or final methods are examples for static polymorphism

Case 2

method overloading is an example of compile time/static polymorphism

method overriding is an example of run time/dynamic polymorphism

so which case is correct ???and java supports static or dynamic polymorphism ?

解决方案

Compile Time and Runtime Polymorphism is directly related to when the calls are resolved.

In compile Time Polymorphism, the calls are resolved during compile time. Method over loading is an example of Compile time Polymorphism. Overloading is irrespective of whether it is at instance level or class level

Example :

 public class Sample {
    static void doSomething(InputStream is) {
        System.out.println("is");
    }

    static void doSomething(OutputStream os) {
        System.out.println("os");
    }

    public static void main(String args[]) {
        System.out.println(doSomething(null)); // "compilation" error . Ambiguous call. Both InputStream and OutputStream can take `null` argument.
    }

}

Next, Runtime Polymorphism : Here the calls / method signatures are checked for existence during compile time but the actual calls are resolved during runtime.Example :

class ParentOfSample {
    void testingOverriding(String s) {
        System.out.println("Parent..");
    }
}

public class Sample extends ParentOfSample {
    static void doSomething(InputStream is) {
        System.out.println("is");
    }

    static void doSomething(OutputStream os) {
        System.out.println("os");
    }

    void testingOverriding(String s) {
        System.out.println("Sample..");
    }

    public static void main(String args[]) {
        ParentOfSample s = new Sample();
        s.testingOverriding(null);
    }
}

O/P :Sample. Note that during Overriding the method signatures are the same.

So, the bottom line is : The second case is right. Java supports both static and dynamic polymorphism.

这篇关于Java多态性概念的困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-27 19:12