Confusion of confusion and desolation in java

Good. Therefore, if ...

int x=3;
int y=5;


x=y;

It will do x=5, right?

Ok, so if it Bis a subclass A...

A a=new A();
B b=new B();

a=b;

^^^ Why is this considered a promotion?

Shouldn't "a" have to become "b", and not vice versa? Can someone explain all this to me?

+4
source share
4 answers

Instead of Aand Bs go to a specific example.

class Person {
    public void greet() {
        System.out.println("Hello there!");
    }
}

class ComputerScientist extends Person {    // Does he, really?
    @Override
    public void greet() {
        System.out.println("Hello there! I work at the Informatics Department.");
    }

    public void validateAlgorithm(Algorithm a)
            throws InvalidAlgorithmException {
        // ...
    }
}

If you have ComputerScientistlike

ComputerScientist cs = new ComputerScientist();

greet, validateAlgorithm. , Person, greet / . ComputerScientist.

Person, , , : " , ComputerScientist. , Person".

Person p = cs;

Person p = (Person) cs;

, p, , validateAlgorithm, , . p , greet this Person, .

upcasting, , , / . ComputerScientist Person.

+8

a = b; a ( a) B. , upcast: a = (A)b;, Java B B a. .

+7
A a = new A();
B b = new B();

:

  • A new A() ASSIGNED a Object B new B() ASSIGNED refbence b
  • , , .

, a=b - UPCASTING

  • , b, a. , , -, , , UPCASTING.

https://www.youtube.com/watch?v=Wh-WZXCAarY

, .

+1

x = 5, ?

Right.

, , B A...

^^^ ?

, .

Shouldn't "a" have to become "b", and not vice versa?

Yes, and so. Link to B rises to link to "A".

0
source

All Articles