Overloading a Java data field type with another field type

Is it possible to overload a field type into another field type?

If so, can you give a few examples?

+5
source share
4 answers

You cannot overload fields (only methods can be overloaded), you can be confused with overriding fields - which is impossible in any case, you end up hiding fields from superclasses. Take a look at this post .

+5
source

I believe that Java supports interfaces, and interfaces should help you achieve what you are trying to achieve.

here is an example i found quickly

here is the tutorial

just make sure you do not overload public members in this way.

0
source

class A<T> {
    protected T field1;
}

class B extends A<String> {
    public char field1;

    public static void main(String[] args) {
        A a = new B();
        a.field1 = 12442;
    }
}

- , 1 , ,

- .

class A<T> {
    protected T field1;
}

class B extends A<Character> {
    public char field1;

    public static void main(String[] args) {
        A a = new B();
        a.field1 = 12442;
    }
}

0

Java (JLS) , Java-- (JVMS)

Oracle JDK 1.8.0_45 assert. :

public class Assert {
    static final int[] $assertionsDisabled = new int[0];
    public static void main(String[] args) {
        System.out.println(int.length);
        assert System.currentTimeMillis() == 0L;
    }
}

Oracle JDK 1.8.0_45, (int[]) (bool), . :

static final boolean $assertionsDisabled = false;

:

the symbol $assertionsDisabled conflicts with a compile synthesized symbo

. fooobar.com/questions/16763/....

,

, . :

void f(int i) { System.out.println("int"); }
void f(float i) { System.out.println("float"); }
int i;
float i;
// Which overridden method is called?
void m() { f(i); }
0

All Articles