Results 1 to 3 of 3
- 11-03-2011, 09:48 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Pass derived object into method wanting superclass ...
Hey All,
I have a little problem with inheritance ...
I have an interface
and a class DistributionalDistance which implements Distance:Java Code:public interface Distance { public double distance(Seq x, Seq y); }
The distance function in the Distance class needs objects of type Seq:Java Code:public class DistributionalDistance implements Distance{ public double distance(QSeq x, QSeq y) { //calculates the distributional distance } }
whereas that in the DistributionalDistance class takes objects of type QSeq:Java Code:public class Seq
Given than QSeq is a child class of Seq, is there a way to work things out here?Java Code:public class QSeq extends Seq
Eclipse suggested to either add unimplemented method,
(I'm not sure I even understand this piece of code.)Java Code:@Override public double distance(Seq x, Seq y) { // TODO Auto-generated method stub return 0; }
or to make DistributionalDistance abstract.
I don't understand why making it abstract would make the problem disappear.
Thanks for ur help,
-Azal.Last edited by azalea; 11-03-2011 at 10:34 PM.
- 11-03-2011, 10:00 PM #2
Re: Pass derived object into method wanting superclass ...
If you want to implement an interface then you MUST implement the abstract methods of that interface. So your distance method in the DistributionalDistance class must have parameters of type Seq. Elsewhere in your code where you call the distance method and you pass it QSeq objects all will be fine if Qseq class extends Seq.
- 11-03-2011, 11:49 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 12
- Rep Power
- 0
Re: Pass derived object into method wanting superclass ...
So here's what I did:
Java Code:public double distance(Seq x, Seq y) throws IllegalArgumentException { if(x instanceof QSeq && y instanceof QSeq){ double d = //calculate distance using (QSeq)x and (QSeq)y return d; } String e = "Arguments must be of type QSeq."; throw new IllegalArgumentException(e); }
Similar Threads
-
Wanting to change from a single object to an array.
By david522 in forum New To JavaReplies: 6Last Post: 05-24-2011, 02:24 PM -
Using superclass fields in subclass method
By lonegreyride in forum New To JavaReplies: 12Last Post: 11-17-2010, 01:21 PM -
how to pass an arraylist from an object back to the parent object that it was created
By george_a in forum New To JavaReplies: 1Last Post: 03-04-2009, 06:14 PM -
Parsing a superclass object to subclass object dynamicly
By Andrefs in forum Advanced JavaReplies: 1Last Post: 07-22-2008, 04:27 PM -
SuperClass of an Object
By Java Tip in forum Java TipReplies: 0Last Post: 12-06-2007, 02:51 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks