473,508 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it the right way to pass the object in reference of parent class??

20 New Member
One.java
------------

public class One{
int x;
char y;
One(){x=0;y='a';}
public void disp(){System.out.println("x ="+x+", y ="+y);}
}


OneX.java
---------------
public class OneX extends One{
double z;
OneX(){z=1.05;}
public void dispX(){System.out.println("z ="+z);}
public static void main(String[] aa){
One obj1;
obj1=new OneX();
obj1.dispX();
}
}

When I compile the OneX.java file
then
OneX.java: cannot resolve symbol
symbol : method dispX ()
location: class One
obj1.dispX();
May 22 '08 #1
5 1450
Laharl
849 Recognized Expert Contributor
Your issue here is that obj1 is defined to be an object of type One, not OneX. obj1 only knows about the methods of One, not those of OneX, because even though it's set to a OneX object, it's still treated by the compiler as a One object. I know that explanation's not all that great, if you don't get it just let me know and I'll try again...
May 22 '08 #2
devgupta01
20 New Member
Your issue here is that obj1 is defined to be an object of type One, not OneX. obj1 only knows about the methods of One, not those of OneX, because even though it's set to a OneX object, it's still treated by the compiler as a One object. I know that explanation's not all that great, if you don't get it just let me know and I'll try again...
I think that obj1 has memory of object of OneX. therefore it should has methods and fields of object of OneX.

Thanks for answer, Please clerify me more. this is very helpful me.
Jun 1 '08 #3
JosAH
11,448 Recognized Expert MVP
You told your compiler that your obj1 is a One object. Such an object doesn't
have a dispX() method. The fact that you sneakily assigned an ObjX object to
obj1 doesn't count for the compiler, i.e. your obj1 is a One object.

kind regards,

Jos
Jun 1 '08 #4
Kid Programmer
176 New Member
Please put your code in code tags. It makes it much easier to read. To put your code in code tags when you are creating a new messsge click on the # sign. Type or paste your cod then click the # sign again.
Jun 1 '08 #5
JosAH
11,448 Recognized Expert MVP
Please put your code in code tags. It makes it much easier to read. To put your code in code tags when you are creating a new messsge click on the # sign. Type or paste your cod then click the # sign again.
# that doesn't seem to work for me ... #

kind regards,

Jos
Jun 1 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
9478
by: Jan Pieter Kunst | last post by:
(apologies if this message is a duplicate -- my news server seems to have problems) Greetings, When using PHP 4, this: // ex. 1 class A { function A(&$obj) {
9
2057
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
2
1141
by: ad | last post by:
I think we always new a class like below:ClassName1 OjbectName=new ClassName1();But I find some example codes which new a class with different classClassName1 OjbectName=new ClassName2();What is...
2
1254
by: M Shafaat | last post by:
Hi! Consider the following: Object A is a member of Object B. Object A has a known identifier. Object B has an unknown identifier. How do we reference Object B using Object A? I have tried...
5
1022
by: Crirus | last post by:
I have this classes tree GameManager 1 object Game Multiple objects Map 1 object Player Multiple objects Squads Multiple objects Units ...
1
4870
by: LCAdeveloper | last post by:
Can anyone help me with this? I'm trying to get an MDIChild form to lookup the value of a control on another open MDIChild form, but can't seem to get the right syntax. Let's say the two child...
10
2487
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++)...
12
2996
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
12
11007
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
4
3459
by: HoangTuanSu | last post by:
I have just got a javascript code from my friend. I've modified it for my purpose but a problem happens. First, here's my code html <body> <table align="center" width="60%" height="100%"...
0
7332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7393
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7058
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.