Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Fri Jan 04, 2013 4:35 pm
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!
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Fri Jan 04, 2013 4:39 pm
by Guest
I am sorry the answer was correct... i wrote program wrongly.
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Sun Mar 02, 2014 11:35 pm
by fasty23
Is it Not possible if the variable of class A is private so b.i refers to variable of class B?
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Mon Mar 03, 2014 12:17 am
by admin
No, explanation to option 1 explains it.
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Wed May 28, 2014 7:27 am
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
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Wed May 28, 2014 8:01 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Tue May 19, 2015 1:12 am
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?
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Tue May 19, 2015 4:14 am
by admin
The code that you are trying to compiler is probably different from what you've pasted here.
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Tue May 19, 2015 6:03 am
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?
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Tue May 19, 2015 8:18 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Thu Oct 12, 2017 11:26 am
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!!
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Thu Oct 12, 2017 9:10 pm
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().
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Fri Nov 03, 2017 9:59 pm
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!
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Fri Nov 03, 2017 11:22 pm
by admin
What happened when you tried it out?
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Thu Feb 25, 2021 2:24 am
by burlacu.valeri
Hi,
public void f(){}
why b.f( ) is compile error?
For me, it works without errors ... :/
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Thu Feb 25, 2021 7:50 am
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.
Re: About Question enthuware.ocajp.i.v7.2.1220 :
Posted: Thu Feb 25, 2021 9:26 am
by burlacu.valeri
Hi,
ok, now I have understood.
