• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

An Odd Problem

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All.
I am having a wierd pblm & though, I have a feeling that the answer lies in front of me, I can't see it.
My files are as follows...
BaseObj.java
------------
package com.mydev.commonarea;
public Interface BaseObj {
}
C1.java
-------
package com.mydev.commonarea.dataobj.SimpleObj;
import com.mydev.commonarea.*;
public class C1 implements BaseObj{}
C2.java
-------
package com.mydev.commonarea.dataobj.xml.XMLObj;
import com.mydev.commonarea.dataobj.*;
public class C2 extends C1;
AppClass.java
--------
package com.mydev.apparea;
import com.mydev.commonarea.*;
import com.mydev.commonarea.dataobj.*;
import com.mydev.commonarea.dataobj.xml.*;
public class AppClass {
public void process(BaseObj bobj) {
.....
..... Some processing
.....
}

public static void main(String[] args) {
XMLObj xobj = new XMLObj(); // Assume there is a valid constructor.
process(xobj);
}
}
------------------------------------------
This code is giving me a compiler error saying -
Incompatible types -
found : com.mydev.commonarea.BaseObj
required : com.mydev.commonarea.dataobj.xml.XMLObj
-------------------------------------------
Now, I know there's nothing wrong with the concept of passing an inherited object (XMLObj) to a reference of type BaseObj, since it should be automatically converted...
I think its something to do with my imports... can someone see anything...
I have posted this here since it pertains to a real life example of Conversion & Casting... if the moderators feel this question belongs to some other forum, pls. move it there.
Thanks
Himanshu.
[ June 14, 2002: Message edited by: Himanshu Jhamb ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Himanshu Jhamb:
I have posted this here since it pertains to a real life example of Conversion & Casting... if the moderators feel this question belongs to some other forum, pls. move it there.


Indeed, I'm going to move this to JiG (Beginner)
Corey
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the XMLObj implements BaseObj, there shouldn't be any problem passing the object to the process method. However, it looks to me like the error is occuring somewhere within that method.
It is important to remember that, even though the run-time type of an input parameter might be XMLObj, it will still be treated as a BaseObj object within that method because that's what the method takes as a paremeter. Therefore, if you're going to perform some operation on that object that is specific to an XMLObj object, rather than a BaseObj object, you'll have to cast it as an XMLObj first.
What is the actual line of code that is causing the compiler error?
Corey
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From C2.java it seems as if XmlObj is a package name, not a class name...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!