943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 353
  • Java RSS
Jul 4th, 2009
0

doubt on object reference

Expand Post »
class A
{
void show()
{
System.out.println("show");
}
}
class B extends A
{
void disp()
{
System.out.println("disp");
}
}
class Prac2
{
public static void main(String args[])
{
A ob = new B();
ob.disp();
}
}


/*this thing shows an error */


now my question is :i thought we can use any object reference which must be above hierchy than the object which we are giving.but then here it shows an error...can some one pl explain object reference in detail because till now i use to think that they are only something like pointers but here they are acting differently
Reputation Points: 10
Solved Threads: 5
Light Poster
purijatin is offline Offline
41 posts
since Jun 2009
Jul 4th, 2009
0

Re: doubt on object reference

The reference variable ob is a ref to type A, which means it can refer to any object of type A or of any sub-type of A.
However, because it is declared as A it can only be used to access members that are defined in A, and there is no disp method in A, so its a compile error.
If you override show in B then you can call ob.show, and the implementation of show from B will be used if ob is actually a reference to a type B, but that's only because show is defined in A.
Featured Poster
Reputation Points: 1907
Solved Threads: 950
Posting Expert
JamesCherrill is offline Offline
5,768 posts
since Apr 2008
Jul 4th, 2009
0

Re: doubt on object reference

ok i get it now..thanks a lot James
Reputation Points: 10
Solved Threads: 5
Light Poster
purijatin is offline Offline
41 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: null pointer exception error while finding longest word in line
Next Thread in Java Forum Timeline: server client send files string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC