0

I'm checking one code with below exact same logic but not able to understand how its happening. So please bear with me..! :)

class A implements I{}

class B extends A{}

class C extends B{}

interface I{}

interface I2 extends I{}

interface I3 extends I2{}

class Top{

    public static void main(String[] args){

      I3 ob = new A();   //Step I
    }
}

Its not possible at Step 1, but I'm reviewing one code where this kind of logic is happening without any error. May I know please if this is happening without error than what else could be looked to check its happening right ? What are the possibilities when Step 1 is right ?

Thanks

c0der
  • 18,467
  • 6
  • 33
  • 65
Andrea
  • 109
  • 2
  • 10

3 Answers3

1

Class A will have only the implementations of Interface I
A variable has two types: a declared (or static) type and a run time type.

I3 ob = new A();

So, This ob variable is declared as type I3 and the object it references is of type A. This will give you compile time exception. And ask you to cast to I3. But still you will get run time exception 'java.lang.ClassCastException'.

You can assign a reference to a variable of its super type only. Interface I is the super type of Class A. Interface I3 is not the super type of Class A.

If you had,

 class A implements I3{}

then it A would have had all the implementation of I, I2 and I3. Then,
I3 ob = new A();
would have been possible.

Possibilities when Class A implements I3:

You can use ob to call the methods of Interface I,I2 and I3. You can check if implementation in Class A is returned.

Supreetha
  • 11
  • 3
  • 1
    `Class A will have all the implementations of Interface 1, 2 and 3` I can't see that in the code posted. `A` only implements `I` (or 1) – c0der Jan 27 '19 at 06:54
  • Andrea, had posted a code which had Interfaces named 1, 2 and 3. Now he has changed the code. My answer was to the code which he had posted earlier. @Andrea please create a new thread if you want to change the question. – Supreetha Jan 27 '19 at 07:05
  • The question was not changed. Only the names changed to valid java names. – c0der Jan 27 '19 at 07:08
  • Not just the names, before Class A was implementing I3. Now you have changed it to implement I – Supreetha Jan 27 '19 at 07:20
  • Class A was implementing 1. – c0der Jan 27 '19 at 07:21
  • "Interface A,B and C" do you mean I , I2, I3 ? – c0der Jan 27 '19 at 07:47
1

See my elaboration in below code comments.

class A implements I{}

class B extends A{}

class C extends B{}

interface I{}

interface I2 extends I{}

interface I3 extends I2{}

class Top {

  public static void main(String[] args){

    I ob1 = new A(); // Compiles bcoz A "is an" I
    I2 ob2 = new A(); // Does not compile bcoz A "is not an" I2
    I3 ob3 = new A(); // Does not compile bcoz A "is not an" I3
  }                  
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
  • So I assume your answer implies that the question to "What are the possibilities when Step 1 is right" is none. – c0der Jan 27 '19 at 07:33
  • @But what if A implements I3, then I ob3 = new A(); will work ..! It think its like parent child relation. Child cannot swallow parent but parent can do ..! :) – Andrea Jan 27 '19 at 07:33
  • " what if A implements I3, then I ob3 = new A(); will work" show us a case that it does. – c0der Jan 27 '19 at 07:34
  • @c0der, It will compile ..I'm sure..! :) – Andrea Jan 27 '19 at 07:37
  • @c0der, But am still not able to get my answer nowhere ..! :( – Andrea Jan 27 '19 at 07:37
  • "But am still not able to get my answer nowhere" you got 2 good answers here. I am sorry that these are not the answers you want. "I'm sure" does not prove anything but that. – c0der Jan 27 '19 at 07:51
  • @Andrea, in the questions you said _"I'm reviewing one code where this kind of logic is happening without any error"_. May be you can post that code to understand what's going on there. – Prasad Karunagoda Jan 27 '19 at 08:28
0

I think it's up-casting in java. for example, when you define class C as extended of B and B as extended of A, then, you can use container A as a holder for object of C:

class A
class B extends A
class C extends B

then you can define an object of C like below:

A a = new C();

A good example in java is defining ArrayList:

List<E> list = new ArrayList<>();

you know that ArrayList definition is:

class ArrayList implements List { ... }
c0der
  • 18,467
  • 6
  • 33
  • 65