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

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

Moderator: admin

Post Reply
ETS User

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

Post by ETS User »

I tried executing this program so just wanted to make sure whether class A and B are in the class file C. If all of them are in the same class file then i think option1 will work.

Please advise!

Guest

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

Post by Guest »

I am sorry the answer was correct... i wrote program wrongly.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

Is it Not possible if the variable of class A is private so b.i refers to variable of class B?

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

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

Post by admin »

No, explanation to option 1 explains it.
If you like our products and services, please help us by posting your review here.

trovanfloyd
Posts: 1
Joined: Wed May 28, 2014 7:20 am
Contact:

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

Post by trovanfloyd »

The answer that question is wrong. I'm sure that option 3 - System.out.println(b.i); will print 20 - is correct. I've tested that situation and I saw that "b.i" works perfectly

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

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

Post by admin »

trovanfloyd wrote:The answer that question is wrong. I'm sure that option 3 - System.out.println(b.i); will print 20 - is correct. I've tested that situation and I saw that "b.i" works perfectly
Please try the code that is given in the question exactly as given.

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

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Compiler says cannot make a static reference to a non-static field.

class A{
private int i = 10;
public void f(){}
public void g(){}
}

class B extends A{
public int i = 20;
public void g(){}
}

public class C{
A a = new A();//1
A b = new B();//2
public static void main(String[] args) {
System.out.println(b.i);
}
}

Also, how will we be able to access "i" in class B?

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

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

Post by admin »

The code that you are trying to compiler is probably different from what you've pasted here.
If you like our products and services, please help us by posting your review here.

subhamsdalmia
Posts: 32
Joined: Sat May 02, 2015 11:57 pm
Contact:

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

Post by subhamsdalmia »

Sir,
I have pasted the program as is,
My question is that since "i" is public in class B, how can we access it?

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

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

Post by admin »

subhamsdalmia wrote:Sir,
I have pasted the program as is,
My question is that since "i" is public in class B, how can we access it?
Variables and static methods are not overridden and so access to variables and static methods is determined at compile time based on the type of the variable (instead of type of the object referred to by the variable, as is the case with instance methods.)
So if you declare b to be of type B, you can access b.i.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

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

Post by Javier »

Hi!

I would like to know why is compiling and running without problem the statement:

b.f();// A has got f(), but the object is of type B.
If there is not method f() in the B class...

Why is running without problem??

Thank you!!

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

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

Post by admin »

If B is a subclass of A and if A has non private f() then B will inherit f(). Thus, there is no issue in calling b.f().
If you like our products and services, please help us by posting your review here.

smahale
Posts: 4
Joined: Sun Oct 29, 2017 7:30 pm
Contact:

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

Post by smahale »

In the given code,

public class C{
A a = new A();//1
A b = new B();//2
}

if we were to do System.out.println(a.i);
Would this now print 10, or would it be a compile error?

Thanks!

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

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

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

burlacu.valeri
Posts: 8
Joined: Tue Jan 26, 2021 7:55 am
Contact:

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

Post by burlacu.valeri »

Hi,

public void f(){}

why b.f( ) is compile error?

For me, it works without errors ... :/

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

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

Post by admin »

burlacu.valeri wrote:
Thu Feb 25, 2021 2:24 am
Hi,

public void f(){}

why b.f( ) is compile error?

For me, it works without errors ... :/
It is not a compile error that is why this option is not correct. Option 5, "None of the above statements is correct", is the correct option.
If you like our products and services, please help us by posting your review here.

burlacu.valeri
Posts: 8
Joined: Tue Jan 26, 2021 7:55 am
Contact:

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

Post by burlacu.valeri »

Hi,

ok, now I have understood. ;)

Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests