-1
    class A{
        void amethod(){
            System.out.println("amethod");
        }
        void overridenmethod(){
            System.out.println("in A");
        }
    }

    class B extends A{
        void bmethod(){
            System.out.println("bmethod");
        }
        void overridenmethod(){
            System.out.println("in B");
        }
    }
    class Test{
    public static void main(String args[]){
    A a1 = new A();
        if(a1 instanceof B){
            B b1 = (B) a1;
            b1.amethod();
            b1.overridenmethod();
        }
     }
    }

This code does not give any compile time or runtime error. But Output is blank. Nothing prints on console.I was expecting the below output:

amethod
in A

What am I doing wrong? Please help. Thanks in advance

shrishti
  • 95
  • 5
  • 3
    `a1` is _not_ an `instanceof` `B`. – GriffeyDog Jan 24 '18 at 20:23
  • 1
    Try `A a1 = new B();` – Oleksandr Pyrohov Jan 24 '18 at 20:23
  • What I intend to do is to downcast a supertype and then invoke methods on it.How am I supposed to do this? I need to understand what functionality is rendered after downcasting a supertype. – shrishti Jan 24 '18 at 20:26
  • @user7812612 Do what Oleksandr said, and you can downcast `a1` to `b1`. Downcasting is for matching the *declared* type to the *actual* type. It doesn't change the actual type of the object, so in order to downcast to `B`, the object must already *be* a `B`. – Andreas Jan 24 '18 at 20:27
  • @A Oleksandr statement is an upcast from B type object to A type object. I want to downcast not upcast – shrishti Jan 24 '18 at 20:29
  • @user7812612 You don't have any instances of `B` at all. There's no means of interacting with any `B` at all because you don't have any `B`s. – Gian Jan 24 '18 at 20:32
  • A class `A` object, created by `new A()`, will never be a `B`. Trying to downcast it to `B` will not work. It'll give you a `ClassCastException` instead. – Ralf Kleberhoff Jan 24 '18 at 20:32
  • @Andreas downcasting and upcasting are not just for matching type. They play an important role to decide which method will be called, based on object type at compile time(overloading) or object type at runtime(overriding) – shrishti Jan 24 '18 at 20:34
  • @RalfKleberhoff you can try to run this code. It is not throwing any classcastexception as I have used instanceof before downcast. – shrishti Jan 24 '18 at 20:37
  • @GriffeyDog how? I am storing a1 in B type reference – shrishti Jan 24 '18 at 20:38
  • @user7812612 The only point of downcasting is to get access to methods only declared on the subtype, i.e. matching the declared type with the actual type, so you can "see" the methods actually available on the object. Since your code only calls `amethod` and `overridenmethod`, both declared on `A`, downcasting makes no difference whatsoever. You only need to downcast to `B` if you need to call `bmethod`. It doesn't matter whether declared type is `A` or `B` when calling `overridenmethod`. Which version of `overridenmethod` gets called is defined by the *actual* type, not the declared type. – Andreas Jan 24 '18 at 20:43
  • @Andreas Got it. Thanks. – shrishti Jan 24 '18 at 20:46
  • @user7812612 Your `if(a1 instanceof B)` statement _never_ evaluates to `true`, because you've created `a1` as an `A`, not a `B`. So, the cast you are trying to perform _never_ takes place (i.e. the body of the `if` statement is never executed). – GriffeyDog Jan 24 '18 at 20:57

1 Answers1

2

What am I doing wrong?

A a1 = new A();
if(a1 instanceof B){
    B b1 = (B) a1;
    b1.amethod();
    b1.overridenmethod();
}

will not execute the body because a1 insn't an instance of B. The instanceof expression is always false.

If you create an instance with new A(...), the result can be used as an A or as any supertype or interface of A (in your case that's only Object).

B is a subtype of A, so you can treat any B as an A (with its overridden methods replacing the A ones), but NOT the other way around.

And it doesn't matter where you store a reference of your A instance - that changes nothing about the instance being an A.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7