About Question enthuware.ocajp.i.v7.2.1403 :

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

Moderator: admin

blackdraft
Posts: 3
Joined: Thu Sep 29, 2016 7:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by blackdraft »

Thanks, that makes it more clear.
I think you meant compile-time instead of the second runtime in your second sentence Paul?

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

Right, corrected. Sorry about that :(
If you like our products and services, please help us by posting your review here.

java_learner
Posts: 6
Joined: Sun Feb 05, 2017 3:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by java_learner »

So what would we need to do in order access the Donkeys location instance-variable?

I see that we could write a getter in the Donkey class. But I wonder if there is some other way - I mean what if we made Donkeys location public? Would the instance variable even then not be accessible? I assume you still would not be able to access it from the TestClass (except maybe via reflection?)

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

To access's Donkey's location instance variable, cast your reference to Donkey first. Like so:
((Donkey)m).location
If you like our products and services, please help us by posting your review here.

acarella610
Posts: 1
Joined: Sun Mar 19, 2017 1:24 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by acarella610 »

I'm curious. How would we then access Donkey's location variable, aside from making Donkey getter and setter methods?

EDIT: Didn't see the above answer. Reading and testing now.

rodtwo
Posts: 1
Joined: Sat Nov 18, 2017 10:18 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by rodtwo »

I have only one doubt, I tought the code woudn't compile because we are hiding a static field using an instance field... Is it allowed when hiding fields, it's only forbidden when overriding methods to change from static to instance?

Thanks and regards

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

Yes, it is ok to hide static field with an instance field (and vice-versa). You can't do that with methods. You should try writing small test programs to confirm your understanding of the concepts.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

kevin35
Posts: 8
Joined: Mon Dec 25, 2017 4:30 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by kevin35 »

Code: Select all

//assume the classes are in seperate files, same package
public interface I {	
	default void foo() {
		System.out.println("I");
	}
}

public class Base{
	public void foo() {
		System.out.println("Base");
	}
}

public class Sub extends Base implements I{
	public static void main(String[] args) {
		new Sub().foo();
	}
}

Hi Paul, can you plz explain why the code prints "Base" and not "I"?

if the class Base implements I, then Base overrides the foo method from the Interface. (that I understand)
I don't understand what happens when Sub implements I

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

Because Sub inherits the method foo from Base. The implementation of foo provided by I is only "default", which means, it is to be used only if the class implementing that interface does not provide any implementation for that method. But Sub does provide an implementation for foo (which it inherited from Base), so there is no need to use I's foo.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by OCAJO1 »

Comparing this code from another question,

interface Bozo{
int type = 0;
public void jump();
}
public class Type1Bozo implements Bozo{
public Type1Bozo(){
type = 1;
}
public void jump(){
System.out.println("jumping..."+type);
}
public static void main(String[] args){
Bozo b = new Type1Bozo();
b.jump();
}
}

Is the reason that in the above code, type = 1 will cause a compiler error while field location in class Donley does not, because
1. location has been re-initialized as int location, and therefore is not the same field as the one in interface movable?
2. or is it because it is in a different package?

Thanks

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

In 2.1403, Donkey has its own non-final location field. It is not trying to modify interface Movable's location field (which is static and final). Not sure what you mean by "re-initialize". There is no such technical term. But yes, it is a different field. So, ok.

In the code that you've posted, you are trying to modify the value of an interface's field. Interface's fields are always static and final. So, can't modify.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by OCAJO1 »

Don't know why I said re-initialize, I meant to say hide!
So based on your reply, being in the same or different packages does not effect the outcome?

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

>So based on your reply, being in the same or different packages does not effect the outcome?
It is difficult to answer such a vague question with 100% certainty.
You need to post exact code that shows what exactly are you trying the understand. Also, please compile and run the code, observe the output, and let us know if you have any trouble understanding the output.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by OCAJO1 »

I have run the program both with all the interface and classes being in the same package and different packages (like the question).

My question had to do with trying to understand that as a rule, when a variable defined in an interface is used again in a class that implements that interface (i.e. package p2, Class Donkey, int location = 200; in this blog's question), does the fact that a class (i.e. class Donkey) is in a different package than an interface (i.e. Movable interface), have any bearing on how that field in the class (i.e. location in class Donkey) is treated by the compiler and JVM?

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

No, there is no difference.
If you like our products and services, please help us by posting your review here.

ftejada
Posts: 5
Joined: Mon Jan 28, 2019 2:00 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by ftejada »

admin wrote:
Sun Feb 05, 2017 10:44 pm
To access's Donkey's location instance variable, cast your reference to Donkey first. Like so:
((Donkey)m).location
Since Donkey's location field has default access modifier, ((Donkey)m).location from TestClass will generate a compile-time error. To fix this we can:
  • Change Donkey's location access modifier to public
  • Use reflection:

Code: Select all

// in file TestClass.java
package px;
import p1.Movable;
import p2.Donkey;
import java.lang.reflect.Field;

public class TestClass {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        Movable m = new Donkey();
        m.move(10);
        m.moveBack(20);
	Field donkeyLocation = m.getClass().getDeclaredField("location"); // retrieves Donkey's location field
	donkeyLocation.setAccessible(true); // suppresses Java access control to make it readable
	System.out.println(donkeyLocation.get(m)); //prints 190
    }
}
  • Move TestClass.java file to package p2 or Donkey.java file to package px

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

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by admin »

Correct. Reflection is not on the ocp exam, so you can leave that though.
If you like our products and services, please help us by posting your review here.

baichen7788
Posts: 23
Joined: Fri Mar 26, 2021 7:25 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1403 :

Post by baichen7788 »

why it prints interface location=0 instead of location in object?

Post Reply

Who is online

Users browsing this forum: No registered users and 53 guests