Page 1 of 1
About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Sun Jul 19, 2015 11:07 am
by Vermeulen
Why would this question be marked "Very easy"?
I found it a bit hard because I didn't know that the compiler would stop looking for i when it found the private int B.i. Instead I thought the compiler might skip it because it's private, and use A.i instead.
Of course once you know the compiler does this, the question is not hard to answer.
In several years of programming I've never seen this situation. The reason is that it's not a good (therefore not common) practice to use public variables or (arguably) even protected ones, therefore I have never encountered this situation!
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Sun Jul 19, 2015 11:17 am
by admin
Toughness is fairly subjective but I agree that it isn't too easy.
-Paul.
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Mon Jun 20, 2016 9:19 am
by wuqing1450
Vermeulen wrote:Why would this question be marked "Very easy"?
I found it a bit hard because I didn't know that the compiler would stop looking for i when it found the private int B.i. Instead I thought the compiler might skip it because it's private, and use A.i instead.
Of course once you know the compiler does this, the question is not hard to answer.
In several years of programming I've never seen this situation. The reason is that it's not a good (therefore not common) practice to use public variables or (arguably) even protected ones, therefore I have never encountered this situation!
If the modifiers of A.i and B.i are changed to private and public respectively, then B.i can be used by the compiler and printed 30
Code: Select all
class A {
private int i = 10;
private int j = 20;
}
class B extends A {
public int i = 30; //1
public int k = 40;
}
class C extends B { }
public class TestClass {
public static void main(String args[]) {
C c = new C();
System.out.println(c.i); //2 will print 30
System.out.println(c.j); //3 compile error
System.out.println(c.k); // will print 40
}
}
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Fri Aug 25, 2017 3:23 pm
by shambhavi
CODE 1 :
Code: Select all
interface A
{
int i = 10;
int j = 20;
}
interface B extends A
{
int i = 30;
public int k = 40;
}
interface C extends B{ }
class scrap implements B,A { // LINE 1
public static void main(String[] args) {
scrap c = new scrap();
System.out.println(c.i); // compiler error - reference to ‘ i ’ is ambiguous
} }
CODE 2 :
Code: Select all
interface A
{
int i = 10;
int j = 20;
}
interface B extends A
{
int i = 30;
public int k = 40;
}
interface C extends B{ }
class scrap implements B{ // LINE 1
public static void main(String[] args) {
scrap c = new scrap();
System.out.println(c.i); // COMPILES FINE
} }
LINE 1 in both the codes are producing different behaviours . in code 1, scrap implements B,A actually means that it implements only B right, since A is redundant , and implementing B will automatically mean implementing A too. but yet , it shows an ambiguity error. why doesn't B's ' i ' hide A here ?
isn't code 2 and and code 1 same ? I mean isn't the LINE 1 in code 1 and 2 actually same i.e. in code 1 saying it implements A is redundant ?
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Fri Aug 25, 2017 11:21 pm
by admin
That is the just a rule of resolving ambiguity. In the second code, Since scrap implements B explicitly, the compiler is able to make the decision in favor of B's i by giving more weightage to the explicitly implemented interface.
Try the specification to see the specific rule.
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Sat Aug 26, 2017 1:33 am
by shambhavi
thanks
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Mon Dec 31, 2018 6:30 pm
by crazymind
Just need to double confirm that: variable hiding(btw Parent class and Child class) and variable shadowing happens (btw instance variable and local variable) only if two variable name are same (nothing to do with identifier or type?)
variable hiding:
Parent class:
final double a = 1;
Child class:
int a = 3;
Will this a hide a from parents class?
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Mon Dec 31, 2018 9:50 pm
by admin
Yes, it will. Please try it out.
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Wed Jan 09, 2019 5:23 pm
by crazymind
Hi, forgive my poorly English reading ability, are "commented" and "commented out" same thing here?
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Wed Jan 09, 2019 7:38 pm
by admin
Yes, same thing in this case.
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Posted: Wed Jan 09, 2019 10:12 pm
by crazymind
Thanks