File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes extending a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "extending a method" Watch "extending a method" New topic
Author

extending a method

Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Could someone please advise me on the correct way to extend a method? I have a Class Sequence

public abstract class Sequence {

/** Class variables*/
private String description;
private String content;


/**Class methods*/
public Sequence( String description, String content){
this.description = description;
this.content = content;
validate();
}
}

I have 2 other Classes which extend this Class ProteinSequence and DnaSequence
public class DnaSequence extends Sequence{

/** Class variables*/
private String description;
private String content;



/**Class methods*/
public DnaSequence (String description, String content) {
super(description, content);
}

There is a method called in the constructor of the super Class Sequence
which is validate() and this needs to get valid characters to test a sequence against. One of the methods necessary for this is validLetters.

public abstract char[] validLetters();

This method is abstract and is extended by 2 methods

this one is in the ProteinSequence Class
public char[] ProteinValidLetters extends validLetters(){
char[] letters = {'G', 'A', 'L', 'M', 'F', 'E'};
return letters;}

this one is in the DnaSequence Class
public char[] DnaValidLetters extends validLetters() {
char[] letters = {'A', 'C', 'G', 'T'};
return letters; }

I am told that this will work by dynamic binding to send the correct letters depending upon which type of Sequence is being used. This I am a bit vague on, so any advice here would be most helpful!!

I have tried to call the result of these methods in the validate method using

char[] theseletters = this.validLetters();
System.out.println(letters);

However I get very curious error messages

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" ProteinSequence.java
ProteinSequence.java:23: ';' expected
public char[] ProteinValidLetters extends validLetters(){
^
1 error

> Terminated with exit code 1.

Does anyone know what this may refer to??? Is this the wrong way to extend a method???

Many thanks
Dianne Gerrelli

PS many thanks to those that have helped on other topics, I am still just about on target to learn Java and pass this course.
Adam Richards
Ranch Hand

Joined: Nov 03, 2005
Posts: 135
It's a little difficult to tell the precise problem without complete code, but I can make some general comments:

1. You can extend classes, but not methods.

2. You can override (i.e., redefine) a super class's method with one of your own. If needed, your overriding implementation can even call the super class version. For example:

public void myMethod(int x, String y)
{
super.myMethod(x);
...
}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You cannot "extend" methods. Instead, you "override" methods by providing different implementations in the subclasses.

In this case, validLetters() is defined as an abstract method, so you would provide implementation in the subclasses.

The dynamic binding will call the appropriate method based on runtime type (ProteinSequence or DnaSequence).


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
Thanks for the replies. I have removed the bit about extending the method now I know that this is wrong. There is still a problem with the compiler accepting the abstract method and I do not understand the error message. Can anyone let me know what might be wrong?

When I compile the Sequence class

I get the following which is a little strange as the ProteinSequence class is identicle apart from the names but the compliler does not complain about this class.

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" Sequence.java
.\DnaSequence.java:2: DnaSequence is not abstract and does not override abstract method validLetters() in Sequence
public class DnaSequence extends Sequence{
^
1 error

I get the identicle message when I try to compile the DnaSequence class

But when I try to complie the ProteinSequence class I once again get a similar compilation error message but this time it is refering to ProteinSequence.

---------- Capture Output ----------
> "C:\Program Files\Java\jdk1.5.0\bin\javac.exe" ProteinSequence.java
ProteinSequence.java:2: ProteinSequence is not abstract and does not override abstract method validLetters() in Sequence
public class ProteinSequence extends Sequence{
^
1 error

> Terminated with exit code 1.

I am not sure why the when trying to compile the Sequence class I only get errors about DnaSequence class and not ProteinSequence if there is something wrong with this also as it appears from above.

I have pasted all three classes just incase anyone can spot what I am doing wrong.

Best wishes
Dianne Gerrelli

import java.io.*;
import java.util.*;

public abstract class Sequence {

/** Class variables*/
private String description;
private String content;


/**Class methods*/
public Sequence( String description, String content){
this.description = description;
this.content = content;
validate();
}

public String getDescription(){
System.out.println("Description");
return description;}

public String getContent (){
/**if (this.content.startsWith("A"))*/
System.out.println("content");
return content;}

public abstract char[] validLetters();

public void validate (){

/**String content = getContent();
System.out.println(content has been passed to validate);

if (content.startsWith("A"))
System.out.println("This may be a fasta file");

char[] theseletters = this.validLetters();
System.out.println(theseletters);*/
}

public String toString(){
return "the description is " + this.getDescription() + "the content is "
+this.getContent();
}

public static void main (String[] args){

Sequence dna1 = new DnaSequence(">Test1", "ACGT");
{System.out.println(dna1);}

}
}



public class ProteinSequence extends Sequence{

/** Class variables*/
private String description;
private String content;

/**Class methods*/
public ProteinSequence (String description, String content) {
super(description, content);
this.description = description;
this.content = content;
}

public String getdescription(){
return description;
}

public String getcontent (){
return content;
}

public char[] ProteinValidLetters (){
char[] letters = {'G', 'A', 'L', 'M', 'F', 'E'};
return letters;}

public String toString(){
return "the protein description is "+this.getdescription()
+" the protein content is "+this.getcontent();
}

public static void main (String[] args){
ProteinSequence proteinSequence1 = new ProteinSequence ("albumin", ">ilmnop");
System.out.println (proteinSequence1);

}
}


public class DnaSequence extends Sequence{

/** Class variables*/
private String description;
private String content;


/**Class methods*/
public DnaSequence (String description, String content){
super(description, content);
this.description = description;
this.content = content;
}

public String getdescription(){
return description;
}

public String getcontent (){
return content;
}

public char[] DnaValidLetters() {
char[] letters = {'A', 'C', 'G', 'T'};
return letters; }

public String toString(){
return "the DNA description is " + this.getdescription() + " the DNA content is "
+this.getcontent();
}

public static void main (String[] args){

DnaSequence dnaSequence1 = new DnaSequence ("Tbx2 ", ">actgacct");
System.out.println (dnaSequence1);

}
}
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Your Sequence class has a method called validLetters() that is declared abstract. This means that the Sequence class expects subclasses to implement this method. The error message is telling you that the subclass called DnaSequence did not implement this method. It also tells you that the possible solutions are to make this class abstract with

or to implement ths validLetters() method in this class. It looks like this is the crux of your original question. To do this, you simply add a method to the DnaSequence class that has an identical "signature" to the one that is declared in the Sequence class. In other words, you need to add the following code to the DnaSequence class:


Notice that the difference in the first line and the declaration in the Sequence class is the missing "abstract" keyword. You will need to fill in the correct details inside the method's body.

I hope this helps. Keep Coding!

Layne
[ January 21, 2006: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
Dianne Gerrelli
Ranch Hand

Joined: Dec 21, 2005
Posts: 45
I changed the method signature and it works!!!

I have managed to make progress,thanks very much.

Best wishes
Dianne Gerrelli
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Dianne Gerrelli:
I changed the method signature and it works!!!

I have managed to make progress,thanks very much.

Best wishes
Dianne Gerrelli


Great!

Let us know if you need any more help. Good luck with your project.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extending a method
 
Similar Threads
test for valid characters
validLetters()
toString
a problem at run time with a call to a method
Exceptions