Run-time polymorphism giving the wrong result

As far as I understand, the following code should print aaccording to my knowledge of runtime polymorphism.

However, when I run the following code, it prints b:

In JLS 8.4.8.1, B1.m1 does not cancel A1.m1, and therefore, when A1.m1 is called, B1.m1 should not be selected

package a;

public interface I1 {
    public Object m1();
}

public class A1 {
    Object m1() {
        return "a";
    }
}

public class C1 extends b.B1 implements I1 {
    public static void main(String[] args) {
        a.A1 a = new a.C1();
        System.out.println(a.m1());
    }
}

package b;

public class B1 extends a.A1 {
    public String m1() {
        return "b";
    }
}

Can someone help me understand this behavior.

+4
source share
6 answers

After adding packages, the question is much more complicated. I tried this and I changed your main program to

public class C1 extends b.B1 implements I1 {
    public static void main(String[] args) {
        a.A1 a = new a.C1();
        System.out.println(a.m1());
        a.A1 b = new b.B1();
        System.out.println(b.m1());
    }
}

(I actually used different package names to avoid conflicts with variable names. Thus, my code is slightly different from the above.)

, "b" "a". , B1, m1 A1. , b.m1(), b A1, , , A1, . , C1?

C1 m1 B1. m1 B1 A1, m1 C1, B1, A1, , 8.4.8.1:

mA , C, C mC, mA C.

C - C1 . mC - m1, B1. "C mC" , C1 m1, . , " ". , B1 , A1. B1 m1, , m1, m1 A1. , . , , mA C ( B1), , , 8.4.8.1 , , m1 C1 A1.

+2

b.

a A1, . , m1 String, , , Test1. Test1 extends B1, m1, m1, .

m1() B1.

EDIT: . OP , .

+2

A1 a = new Test1(); Test1 A1.

, Test1, / A1, overriden Test1.

.

JLS 8.4.8.1. ( )

mC, C, C mA, A, :

8.4.8.3.

(§6.6) , , :

  • , ; .
  • , ; .
  • , ; .

:

, .

C1 m1 (- ), A1 , B1, .

, ( super.m1 @Override B1.m1). a.m1() , .

+1

. @Override, . , , .

B#m1 , - A .

, A#m1 private, B , , .

static class A{

    private String get(){
        return "a";
    }

}

static class B extends A{

    public String get(){
        return "b";
    }
}
public static void main (String[] args) throws java.lang.Exception
{
    A b = new B();
    System.out.println(b.get());
    System.out.println(((B)b).get());
    // your code goes here
}

:

0

I1, A1 Class B1 extends A1 Class C1 extends B1 ( extends A1).

, C1 B1, A1 & I1, C1, , .

:

 I1 instance = new C1();
    String value = instance.m1();

m1, (C1), B1 "b".

0

. . , , , .

- , " -". , , C1 B1, C1 - B1. , A1, B1 C1. - . :

A1 =

B1 = Feline

C1 = Cat C2 = Lion ()

Cat, Feline, : Cat - Feline. , "". , . :

public interface IAnimal {
    public Object saySome();
}

public class Animal {
    Object saySome() {
        return "I am an animal";
    }
}

public class Feline extends Animal {
    public String saySome() {
        return "I am a feline";
    }

public class Cat extends Feline implements IAnimal {
    Object saySome() {
        return "meow";
    }
}

public class Lion extends Feline implements IAnimal {
    Object saySome() {
        return "roar";
    }
}

class Aplication {
    public static void main(String[] args) {
        Animal anAnimal = new Cat();
        Animal anotherAnimal = new Lion();
        System.out.println(anAnimal.saySome());
        System.out.println(anotherAnimal.saySome());
    }
}

, ,

, .

0
source

Source: https://habr.com/ru/post/1680279/


All Articles