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

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

Moderator: admin

Post Reply
Vermeulen
Posts: 12
Joined: Wed Jul 15, 2015 4:05 pm
Contact:

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

Post 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!

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

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

Post by admin »

Toughness is fairly subjective but I agree that it isn't too easy.
-Paul.
If you like our products and services, please help us by posting your review here.

wuqing1450
Posts: 8
Joined: Thu Mar 24, 2016 12:02 pm
Contact:

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

Post 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
       } 
}  

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post 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 ?

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

shambhavi
Posts: 25
Joined: Fri Aug 04, 2017 12:21 am
Contact:

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

Post by shambhavi »

thanks

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post 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?

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

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

Post by admin »

Yes, it will. Please try it out.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

Hi, forgive my poorly English reading ability, are "commented" and "commented out" same thing here?

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

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

Post by admin »

Yes, same thing in this case.
If you like our products and services, please help us by posting your review here.

crazymind
Posts: 85
Joined: Mon Dec 24, 2018 6:24 pm
Contact:

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

Post by crazymind »

Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 119 guests