About Question enthuware.ocajp.i.v7.2.1015 :
Moderator: admin
-
- Posts: 12
- Joined: Wed Jul 15, 2015 4:05 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1015 :
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!
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!
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Toughness is fairly subjective but I agree that it isn't too easy.
-Paul.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 8
- Joined: Thu Mar 24, 2016 12:02 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
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 30Vermeulen 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!
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
}
}
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
CODE 1 :
CODE 2 :
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 ?
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
} }
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 ?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
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.
Try the specification to see the specific rule.
If you like our products and services, please help us by posting your review here.
-
- Posts: 25
- Joined: Fri Aug 04, 2017 12:21 am
- Contact:
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
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?
variable hiding:
Parent class:
final double a = 1;
Child class:
int a = 3;
Will this a hide a from parents class?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Yes, it will. Please try it out.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Hi, forgive my poorly English reading ability, are "commented" and "commented out" same thing here?
-
- Site Admin
- Posts: 10065
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1015 :
Yes, same thing in this case.
If you like our products and services, please help us by posting your review here.
-
- Posts: 85
- Joined: Mon Dec 24, 2018 6:24 pm
- Contact:
Who is online
Users browsing this forum: No registered users and 119 guests