As you maybe know, as Java language perspective all method in C# are final by default (also vice versa, all methods in Java are virtual as C# language perspective).
In C# we can replace final (non-virtual) methods by new
keyword ( please see this ). There is not anyway to replace final methods in Java?
Edit 1:
I just want to mention that method replacing is not same with method overriding. Please run this C# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ComapreOverrideWithNew
{
class Program
{
static void Main(string[] args)
{
var cat = new Cat();
cat.PrintNameByNew();
Console.WriteLine(cat.GetNameByNew());
cat.PrintNameByOverride();
Console.WriteLine(cat.GetNameByOverride());
Console.ReadKey();
}
}
class Animal
{
public String GetNameByNew()
{
return "Animal";
}
public void PrintNameByNew()
{
Console.WriteLine(GetNameByNew());
}
public virtual String GetNameByOverride()
{
return "Animal";
}
public void PrintNameByOverride()
{
Console.WriteLine(GetNameByOverride());
}
}
class Cat : Animal
{
new public String GetNameByNew()
{
return "Cat";
}
public override String GetNameByOverride()
{
return "Cat";
}
}
}
For example http://docs.oracle.com/javase/tutorial/java/IandI/final.html is just talking about overriding. Also this code have not any compiler warning on compilation.
您不能用Java替换final方法。
There's no such thing in Java . A final method cannot be overridden.
C#中的所有new
关键字都会抑制编译器警告。
The only way to create a "new" final method in java is if the method is marked private. The following is legal:
public class A
{
private final void go(){};
}
class B extends A
{
private final void go() {};
}
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.