Java 是否可以重载最终方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1988077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:08:49  来源:igfitidea点击:

is it possible to overload a final method

java

提问by GuruKulki

In java we cannot override a final method but is it possible to overload ?

在 Java 中,我们不能覆盖 final 方法,但是可以重载吗?

采纳答案by Taylor Leese

Yes, overloading a final method is perfectly legitimate.

是的,重载 final 方法是完全合法的。

For example:

例如:

public final void doStuff(int x) { ... }
public final void doStuff(double x) { ... }

回答by Upul Bandara

Yes It is possible.

对的,这是可能的。

You can examine this kind of things by writing small Java classes.

您可以通过编写小型 Java 类来检查这类事情。

class TestClass{
   final public  void testMethod(){

   }

   final public  void testMethod(int i){

   }
 }

回答by Prasoon Saurav

YES, why not? It is as legal as overloading non-final methods.

是的,为什么不呢?它与重载非最终方法一样合法。

But you cannot override them(you already know it).

但是你不能覆盖它们(你已经知道了)。

Eg :

例如:

public final void func(String x) {/* code */}
public final void func(double x) { /* more code */ }
public final void func(int x) { /* yeah I have still more code */ }

回答by giri

yes overloading final method is possible in java.As final methods are restricted not to override the methods.

是的,java 中可以重载 final 方法。因为 final 方法被限制为不能覆盖这些方法。

while overloading argument list must be different type of the overloading method.

而重载参数列表必须是不同类型的重载方法。

回答by gameover

Yes, very much possible.

是的,很有可能。

A small program to demonstrate it:

一个小程序来演示它:

class A{
    final void foo(){ System.out.println("foo ver 1 from class A"); }
    final void foo(int a){ System.out.println("foo ver 2 from class A"); }
    }


class B extends  A{
    final void foo(long l){ System.out.println("foo ver 3 from class B"); }
    // final void foo(){ System.out.println("foo ver 1 from class A"); } NOT ALLOWED
}

public class Test {    
    public static void main(String [] args){
        B obj = new B();

        obj.foo();
        obj.foo(1);
        obj.foo(1L);
    }   
}

Output:

输出:

foo ver 1 from class A
foo ver 2 from class A
foo ver 3 from class B

回答by ZoFreX

Yes, but be aware that dynamic dispatch might not do what you are expecting! Quick example:

是的,但请注意,动态调度可能无法达到您的预期!快速示例:

class Base {
    public final void doSomething(Object o) {
        System.out.println("Object");
    }
}

class Derived extends Base {
    public void doSomething(Integer i) {
        System.out.println("Int");
    }
}

public static void main(String[] args) {
    Base b = new Base();
    Base d = new Derived();
    b.doSomething(new Integer(0));
    d.doSomething(new Integer(0));
}

This will print:

这将打印:

Object
Object

回答by user241924

yes we can overload the final methods

是的,我们可以重载最终方法

回答by san

Yes:

是的:

class a {
  final void method1(int a,int b) {
    System.out.println("super class");
    int d = a+ b;
    System.out.println(d);
  }
}
class b extends a{
  final void method1(int a) {
    System.out.println("sub class");
    System.out.println(a);
  }
}
public class c {
  public static void main(String ar[]) {
    b m = new b();
    m.method1(10);
  }
}

回答by Chandan Gupta

Yes, You can overload but not override.

是的,您可以重载但不能覆盖。