Example & Tutorial understanding programming in easy ways.

What is output of following ?explain why?

class A {

String c = "A";

}

interface B {

String c = "B";

}

interface C {

String c = "C";

}

class R4R extends A implements B, C {

String c = "R4R";

}

class R4RExp extends A implements B, C {

//String c = "R4R";

}

public class R4RTest {

public static void main(String str[]) {

R4RExp r4rExp=new R4RExp();

//System.out.println("C:" + r4rExp.c);//ambiguous due to same variable name into class A ,interface B and C

A r4rA=new R4RExp();

System.out.println("C:" + r4rA.c);

B r4rB=new R4RExp();

System.out.println("C:" + r4rB.c);

C r4rC=new R4RExp();

System.out.println("C:" + r4rC.c);

R4R r4r = new R4R();

System.out.println("C:" + r4r.c );//no ambiguous as we had defined variable String c = "R4R"; into class R4R .It will used by r4r object

}

}

OutPut

   
 C:A

C:B

C:C

C:R4R

 

Read More →