0

I do not understand how the casting is done on var2 The following line does not Compile as methodA1() not present in interface reference I1

String var = i1.methodA1();

Hence I1 requires a cast to invoke methodA1 I do not understand how the casting is done as A1 is not the child class of I1. Child c = (Child)p; According to the syntax of downcasting A1 should be the Child class and i1 should be the parent object but neither is A1 the child class nor is i1 it's parent object I also do not understand how I am able to call to call toString() from i1 and i2 because toString() is defined in class A1 and toString() is not declared in I1 and I2.


interface I1 {
    void methodI1(); //public static by default
}

interface I2 extends I1 {
    void methodI2(); //public static by default
}

class A1 {

    public String methodA1() {
        String strA1 = "I am in methodC1 of class A1";
        return strA1;
    }
    
    public String toString() {
        return "toString() method of class A1";
    }
}

class B1 extends A1 implements I2 {
    public void methodI1() {
        System.out.println("I am in methodI1 of class B1");
    }
    public void methodI2() {
        System.out.println("I am in methodI2 of class B1");
    }
}

public class InterFaceEx {
    public static void main(String[] args) {
        I1 i1 = new B1();
        I2 i2 = new B1();
        String var2 = ((A1) i1).methodA1();// I1 requires a cast to invoke methodA1. How is this casting happening?
        System.out.println("var2 : " + var2);
        String var4 = i1.toString();// How I am able to call toString()?
        System.out.println("var4 : " + var4);
        String var5 = i2.toString();// How I am able to call toString()?
        System.out.println("var5 : " + var5);
    }
}
Anshul Gupta
  • 265
  • 2
  • 12
  • The first cast is because you have declared `i1` to be of type `I1`, and that is not any type of `A1`. If you declared it as its actual type `B1` you wouldn't need to cast. – markspace Jun 30 '20 at 20:41
  • A1 is not the child class of I1 so how I am able to do this cast – Anshul Gupta Jun 30 '20 at 20:50
  • Weak title. Rewrite to summarize your specific technical issue. – Basil Bourque Jun 30 '20 at 20:54
  • There is no synatx error. I am unable to understand how the casting is done. – Anshul Gupta Jun 30 '20 at 20:57
  • You can call any of `Object`'s methods via an interface reference, `toString()` included, because Java knows that every class extends `Object`. – John Kugelman Jun 30 '20 at 21:03
  • toString() is a user defined method in class A1. So how can I call it with i1 and i2. – Anshul Gupta Jun 30 '20 at 21:08
  • Because `A1.toString` is an override of `Object.toString`. – John Kugelman Jun 30 '20 at 21:11
  • How am I able to cast i1 to A1 as A1 is not the child of I1 – Anshul Gupta Jun 30 '20 at 21:23
  • You can cast `i1` to `A1` because `i1` happens to reference an object of the class `B1` that implements `I1` **and** extends `A1` – Thomas Kläger Jun 30 '20 at 21:32
  • Child c = (Child)p; According to the syntax of downcasting A1 should be the Child class and i1 should be the parent object but neither is A1 the child class nor is i1 it's parent object – Anshul Gupta Jun 30 '20 at 22:06
  • Well, as you correctly noticed, `(A1) i1` is not a "downcast", but the compiler still allows it: [JLS-5.5.1](https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.5.1): _the cast is always legal at compile time (because even if T does not implement S, a subclass of T might)_. Call this "sidecasting" if that helps you understand it better. – Thomas Kläger Jun 30 '20 at 22:25
  • How am I able to call toString() from i1 and i2 because toString() is defined in class A1 and toString() is not declared in I1 and I2. – Anshul Gupta Jul 01 '20 at 12:24
  • Did you read the four linked questions? Your questions are covered in those links, please read them. – John Kugelman Jul 01 '20 at 12:34

0 Answers0