1

consider the code below :

class B
{
  int j =100;
}

class A extends B
{
    int i=10;
}

public class Test
{
    public static void main(String[] args)
    {
       A obj =new A();
       System.out.println(obj);
       B obj1 =obj;

       System.out.println(obj1); // here i am confused

       if( obj1 instanceof A )
       {
                System.out.println("yes"); //here i am confused
       }
    }
}

here the output is :

A@35186a
A@35186a
yes

Now obj1 is an instance of class B which is superclass , so why does the toString() show it to be object of A ? Also how can instanceof operator show it to be instance of class A ?

2
  • Obj1 is still the object Obj, which is generated by class A Commented Apr 13, 2014 at 8:45
  • What you should ask yourself is: what would be the purpose of instanceof if it evaluated to false in this scenario? Commented Apr 13, 2014 at 8:48

4 Answers 4

5

You have to distinguish between the reference and the actual object.

B obj1 = obj;

Here you create a new reference of type B named obj1. The object referenced is still an A object though, this can never be changed.

Sign up to request clarification or add additional context in comments.

Comments

1

obj1 is pointing at obj which is an instance of A (see the new A()) you have got there. On the left hand side you are just referencing the super class. Your obj1 will only be able to see methods in B. obj will be able to see all methods in B and A (subject to correct access)

From the java trail

Declaration: The code left of the = associates a variable name with an object type.
Instantiation: The new keyword is a Java operator that creates the object.
Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

Comments

1
  • There isn't any toString method in your code, you print the memory place holders (Java don't allow you to see the exact location of the object in the memory).

When you do "extends" you say "A is a son of B",then in the lines:

A obj =new A();
System.out.println(obj);

You create an A object and print is memory place holder (remember, A is son of B, so if you want to describe it, imagine a box called A and a box called B connected to her (on top of her, because it's her father)).

Now in the next lines:

B obj1 =obj;
System.out.println(obj1); // here i am confused

You create another object called obj1 and you assign him the memory place holder of A so, it's the first object that you created, called obj. how can you assign A object to a B? Polymorphism!1 and again you print it's memory place holder.

Next you do if:

if( obj1 instanceof A ){
   System.out.println("yes"); //here i am confused
}

So, obj1 is an instanceof of A (it's a box of A and on top of her is a box of B (it's father, polymorphism)), and you print "Yes".

  • In polymorphism you print the lowest method, so you print the A methods if there is any, and if there isn't you "climbing up" to the father and check there and so on..

Polymorphism (computer science)

Comments

0

Now obj1 is an instance of class B which is superclass

That is not true, obj1 is still an instance of A. You just happen to assign a pointer of the super class to an already existing pointer of class A.


If you want an instance of class B just do this:

B b = new B();

If you want an instance of class A you can do this:

A a1 = new A();

or

B a2 = new A();

The later, a2, is correct because A is of type B. However, a2 is still an instance of A, because we used new A().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.