| | |
doubt on object reference
Thread Solved
![]() |
•
•
Join Date: Jun 2009
Posts: 3
Reputation:
Solved Threads: 0
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
{
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
•
•
Join Date: Apr 2008
Posts: 964
Reputation:
Solved Threads: 143
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.
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.
![]() |
Similar Threads
- Object reference not set to an instance of an object. in a setup Project in ASP.NET (ASP.NET)
- Object reference not set to an instance of an object. (C#)
- value of string is cannot conerted into system.array (VB.NET)
- Converting to and storing object reference as a string. (Python)
- object reference not set to instance of object+axis2 (Java)
- Object reference not set to an instance of an object. (VB.NET)
- Object reference not set to an instance of an object (ASP.NET)
- sql data pull with no result gives Object reference not set exception (VB.NET)
Other Threads in the Java Forum
- Previous Thread: null pointer exception error while finding longest word in line
- Next Thread: server client send files string
| Thread Tools | Search this Thread |
addball android applet application apps array automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) button card class classes client code collision columns component constructor crashcourse css database designadrawingapplicationusingjavajslider draw eclipse ee error eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image integration intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia jvm linux loan loop method migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle phone physics plazmic print problem program programming project radio reporting scanner server service set sharepoint smart sms smsspam software sql subclass support swing textfield threads tree trolltech unlimited utility windows





