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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post 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?

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

cielke
Posts: 2
Joined: Thu May 16, 2019 3:54 am
Contact:

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

Post 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()
    }
}

admin
Site Admin
Posts: 10045
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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 .
If you like our products and services, please help us by posting your review here.

cielke
Posts: 2
Joined: Thu May 16, 2019 3:54 am
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 46 guests