Page 1 of 1

About Question enthuware.ocajp.i.v8.2.888 :

Posted: Sat Jul 29, 2017 1:22 pm
by Sergey

Code: Select all

class A {
}

class AA extends A { 
}


public class TestClass {
    public static void main(String[] args) throws Exception {
        A a = new A();
        AA aa = new AA();
        a = aa; <<<<<<----- here
        System.out.println("a = "+a.getClass());
        System.out.println("aa = "+aa.getClass());
    }
}
Well. Maybe i dont understand something but how can a = aa.
If A is Animal and AA is dog, how can we say that Animal is certainly dog?

Re: About Question enthuware.ocajp.i.v8.2.888 :

Posted: Sat Jul 29, 2017 4:52 pm
by Sergey
Oh, my poor brain.
There is another example.

Code: Select all

class Animal {  }
class Dog extends Animal {  }
public class TestClass{
    public static void main(String[] args){
        Animal animal = new Animal(); //1
        Dog dog = new Dog();     //2
        animal =  dog;        //3
        dog = animal; <<--- error

    }
}
If i try to read it from RIGHT to LEFT it is ok. Dog is ALWAYS animal but Animal is NOT ALWAYS Dog.
But Java is the first language where i have to read from RIGHT to LEFT.
I will go to hang myself.

Re: About Question enthuware.ocajp.i.v8.2.888 :

Posted: Sat Jul 29, 2017 10:27 pm
by admin
You are getting confused because = is not "equal to". = is an assignment operator. When you write a = aa; you should not read it as a is equal to aa. You should read it as a is being assigned the value contained in aa.

This is not Math, this is programming :)
Paul.

Re: About Question enthuware.ocajp.i.v8.2.888 :

Posted: Thu May 16, 2019 4:13 am
by cielke
I got confused as well. I added a method and a property in AA class.
so we assign aa to a and have:
  • a is of class AA,
  • a isinstanceof AA,
  • a does not have a.a :?
  • a does not have a.aa() :?
a seams to be a proper AA member, why can't it access AA method nor has it fields ?

Code: Select all

class A {
}

class AA extends A {
    int a = 10;
    void aa(){
    System.out.println("just doing aa thing");
    }
}

public class TestClass {
    public static void main(String[] args) throws Exception {
        A a = new A();
        AA aa = new AA();
        a = aa;
        System.out.println("a.a = "+a.a);        // <---- can not access a.a
        a.aa();                                                 // <---- can not access a.aa()
    }
}

Re: About Question enthuware.ocajp.i.v8.2.888 :

Posted: Thu May 16, 2019 5:14 am
by admin
You have asked a very fundamental question, which is answered by pretty much all Java books. Which book are you following?
The compiler does not know that 'a' really points to an object of type AA. While validating code, it only goes by the declared type of a reference, which is A, in this case. A does not have the property a or the method aa().

To make the compiler understand that a points to an object of type AA, you have to cast it and then it will allow you to access properties of AA. i.e. ((AA)a).a and ((AA)a).aa().

This is explained nicely in Section 11.3 "Determine when casting is necessary" of OCAJP Fundamentals .

Re: About Question enthuware.ocajp.i.v8.2.888 :

Posted: Thu May 16, 2019 1:38 pm
by cielke
Busted! I went through a few... but it seams I didn't pay enough attention.
I've found the issue been risen in greater detail here.
Thanks for the recommendation. Will check that one out before another go.