doubt on object reference

Thread Solved
Reply

Join Date: Jun 2009
Posts: 3
Reputation: purijatin is an unknown quantity at this point 
Solved Threads: 0
purijatin purijatin is offline Offline
Newbie Poster

doubt on object reference

 
0
  #1
Jul 4th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 964
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 143
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: doubt on object reference

 
0
  #2
Jul 4th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: purijatin is an unknown quantity at this point 
Solved Threads: 0
purijatin purijatin is offline Offline
Newbie Poster

Re: doubt on object reference

 
0
  #3
Jul 4th, 2009
ok i get it now..thanks a lot James
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC