-1
class A {}
class B extends A{}

A objectX = new B();

What does the last line mean? Is it a object of class A or Is it a object of class B.

Does instance and object have same meaning ? Is objectX instance of A or is it an instance of B?

When we run objectX.SomeMethod. What will compiler check? Or method will be checked at run time?

  class A{}
  class B extends A{}
  public class Main{
  public static void main(String[] args){  
  A objectX =  new B(); 
  System.out.println(objectX instanceof B);//line 1
  System.out.println(objectX instanceof A);//line 2
  }
  }

If i run the above code why is it giving me true for line 1 and line 2.objectX is pointing to B.How come objectX is an instance of A.?

Mahesh Gupta
  • 55
  • 1
  • 10
  • 2
    It's a reference of type A, referring to an object of type B. This means that you can only invoke methods and refer to fields defined in class A. – Andy Turner Oct 06 '17 at 09:50
  • 3
    It's much easier if you show your question in code, rather than trying to describe it in words. – Andy Turner Oct 06 '17 at 09:51
  • Visit: https://stackoverflow.com/questions/13077009/java-difference-between-a-x-new-a-and-a-x-new-b-when-b-extends-a – Vidhika Jain Oct 06 '17 at 10:12
  • Visit: https://stackoverflow.com/questions/13077009/java-difference-between-a-x-new-a-and-a-x-new-b-when-b-extends-a – Vidhika Jain Oct 06 '17 at 10:14

2 Answers2

2

Check this example:

Object aStringObject=new String();

In your scenario A is Object ( super class of all objects ) and B is a String. So to your question now aStringObject is a reference (or a pointer) to an object of a class String. It is defined as a reference with a type Object so it can point to different kind of objects and not just strings. So if you want to avoid compile errors in your code you can use it only as an Object.

If you know it is a String though (the instance you are pointing to is of type String) you can cast it to a String and it will be fine. In the example above you can do:

String aStringString = (String) aStringObject; 

but if that object isn't something that you just declared you cannot be sure you can do that and by its declaration it can be a String but it can also not be one.

In your case:

 A objectX = new B();

objectX is an instance of type B but the reference (pointer) is declared with a type A so you can use it safely (without casting) as a type A object and it will work because all objects of type B are also A :) And you can do ((B)objectX).bMethod() because you know it is actually B but it is not safe because it can be an A declared like that.

Veselin Davidov
  • 7,031
  • 1
  • 15
  • 23
  • you would never need to cast objectX to an "A" anyway - it's not that you can use it safely as an A, it is an A – pecks Oct 06 '17 at 10:00
  • yeah that's what I mean. You declare it as an A and it is A since B is an A. But if you want to use it as a B you need to cast it. And you cannot do it safely because the definition doesn't guarantee that this objectX is a B (you might know it because you created the instance). I wrote (without casting) just for clarification but i guess it doesn't make it more clear :) I got confused in the explanation – Veselin Davidov Oct 06 '17 at 10:04
  • you can of course check whether it is a B using instanceof – pecks Oct 06 '17 at 10:21
0
A objectX = new B();

here objectX is the reference of Class A(parent class), which can hold object of either class A or class B(child class).

and right side of = i.e new B(), is the object of B class(child class).

always remember in java objects are created by using new keyword. so left side part is reference and right side part is object.

when you call method like this

objectX.someMethod()

in this case, first compiler check method is present in reference class(A class) or not.if not present then show error.

If method found then method would be call from child class(B class).because method binding is done at runtime.

Mostch Romi
  • 531
  • 1
  • 6
  • 19