Is Java unambiguous for C # "new static" or "static new"?

Is there a new modifier for the Java key symbol for C # ?

+5
source share
5 answers

No. In the case of static methods, they are not inherited in Java, so you do not need the equivalent of a new modifier.

+2
source

There is no similar construction in Java.

(Do not confuse newwith the opposite @Override. This is not so.)

Consider this C # code:

class A {
    virtual public int x() { return 1; }
    virtual public int y() { return 1; }
}

class B : A {
    new public int x() { return 2; }
    override public int y() { return 2; }
}

void Main()
{
    A aa = new A();
    A ba = new B(); // compile time type of ba is A
    B bb = new B(); // compile time type of bb is B

    aa.x().Dump();
    ba.x().Dump(); // look how this is really A.x!!
    bb.x().Dump();

    "---".Dump();

    aa.y().Dump();
    ba.y().Dump(); // this is B.y!
    bb.y().Dump();
}

When launched in LINQPad, this generates:

1
1
2
---
1
2
2

, , , new override. new , . ... .

.

+2

. http://download.oracle.com/javase/tutorial/java/IandI/hidevariables.html

APT . .

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
public @interface HideSuper { }

public class Parent {
   Object x;
}

public class GoodChild extends Parent {
   @HideSuper Object x; 
}

public class TroublingChild extends Parent {
   Object x; // your plugin should raise warnings here
}

[post answer edit]:

1 - , @Override new, .

2 - , , , Class , , Source.

3 - , , APT, IDE. Eclipse .

+1

, , , @Override. , . Java "", ++ .

public class Car {
    public void start() { ...
    }
}

public class Ferrari {
    @Override
    public void start() {
    }
}

@Override , Car.start() Ferrari.start().

0

.

In Java, a subclass will either override or hide the members of the superclass (fields, methods, types) with the same name. Hiding never issues a warning, so the modifier does not need to suppress the warning.

0
source

All Articles