Results 1 to 3 of 3
  1. #1
    azalea is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default Pass derived object into method wanting superclass ...

    Hey All,

    I have a little problem with inheritance ...

    I have an interface
    Java Code:
    public interface Distance {
    	public double distance(Seq x, Seq y);
    }
    and a class DistributionalDistance which implements Distance:
    Java Code:
    public class DistributionalDistance implements Distance{
    	public double distance(QSeq x, QSeq y)
    	{
    		//calculates the distributional distance
    	}
    }
    The distance function in the Distance class needs objects of type Seq:
    Java Code:
    public class Seq
    whereas that in the DistributionalDistance class takes objects of type QSeq:
    Java Code:
    public class QSeq extends Seq
    Given than QSeq is a child class of Seq, is there a way to work things out here?
    Eclipse suggested to either add unimplemented method,
    Java Code:
    @Override
    	public double distance(Seq x, Seq y) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    (I'm not sure I even understand this piece of code.)
    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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default 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.

  3. #3
    azalea is offline Member
    Join Date
    Oct 2011
    Posts
    12
    Rep Power
    0

    Default 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

  1. Wanting to change from a single object to an array.
    By david522 in forum New To Java
    Replies: 6
    Last Post: 05-24-2011, 02:24 PM
  2. Using superclass fields in subclass method
    By lonegreyride in forum New To Java
    Replies: 12
    Last Post: 11-17-2010, 01:21 PM
  3. Replies: 1
    Last Post: 03-04-2009, 06:14 PM
  4. Replies: 1
    Last Post: 07-22-2008, 04:27 PM
  5. SuperClass of an Object
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •