ef2011
ef2011

Reputation: 10631

Is there a Java quivalent to C#'s "new static" or "static new"?

Is there a Java quivalent to C#'s new Modifier?

Upvotes: 5

Views: 179

Answers (5)

user166390
user166390

Reputation:

There is no similar construct in Java.

(Do not confuse new with the opposite of @Override. It is not.)

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 run in LINQPad this generates:

1
1
2
---
1
2
2

Note how the compile time type determines which method is called and this is influenced by using either the new or override modifier. The new modifier essentially introduces a fork determined by compile-time type for the given member. It can be used for great power ... and with great power comes more clichés.

Happy coding.

Upvotes: 2

alphazero
alphazero

Reputation: 27224

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

You could do this with an APT-based toolchain. Define an annotation, and detect cases of field hiding without annotation.

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

and then in your code

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 - Note that while the mentioned @Override has close semantics to new, it can not be uniformly applied to class members.

2 - Regarding suggestion above, it is arguably more correct to reduced the scope of the retention to Class or perhaps even Source.

3 - Finally, APT based approaches should be supported by your IDE. Eclipse supports it in a fashion.

Upvotes: 1

meriton
meriton

Reputation: 70564

There isn't.

In Java, a subclass will either override or hide superclass members (fields, methods, types) of the same name. Hiding never emits a warning, so there is no need for a modifier to suppress the warning.

Upvotes: 0

Jim
Jim

Reputation: 3684

I think the equivalent you're looking for might be the @Override annotation. It provides the compiler with a hint that you intended to override a method from the parent. All methods in Java objects are "virtual" like you have to define in C++ and can be overridden for polymorphic behavior.

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

and

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

With @Override, the compiler will then give errors if you change the Car.start() signature and don't change the Ferrari.start() to match.

Upvotes: 0

Steve
Steve

Reputation: 8819

There is not. In the case of static methods, they're not inherited in Java, so you don't need an equivalent of the new modifier.

Upvotes: 2

Related Questions