I don't know of this error and how to fix it. I'm getting this error with all of my child classes. Included the Error Message and the abstract method that needs to be over written. Added class and what is inside of class.
Error:
MyMath3 is not abstract and does not overide abstract method compareTo(Homework3) in java.lang.Compareable
Code:
public abstract class Homework3 implements Comparable<Homework3>
public class MyMath3 extends Homework3
private int page;
private String typeHomework;
/**
* Constructor for objects of class MyMath
*/
public MyMath3(int p)
{
}
public void createAssignment(int p)
{
typeHomework="Math";
page=p;
}
public String getHomework()
{
return typeHomework;
}
public int getPage()
{
return page;
}
public String toString()
{
return typeHomework+"--- The number of pages needed to read:"+page;
}
}
public int compareTo(Homework3 obj,Homework3 obj2 )
{
int compareResult= obj.compareTo(obj2);
return compareResult;
}
The signature of your compareTo method should be:
#Override
public int compareTo(final Homework3 other)
You can access your current object using... this!
By the way, you might run into some StackOverflowException in a few minutes/seconds...
That's because your compareTo method recursively calls itself. You probably meant to compare the number of pages needed to be read. If so, this could be the body of your compareTo implementation:
return this.getPage() - other.getPage();
Cheers ;)
Related
This is the main demo class
public class Ongoing{
public static void main(String[] args){
ExtendsAbstract josh = new ExtendsAbstract(5, "donkey");
System.out.println(josh.toString());
}
}
this is the class extended from the abstract class, the one who's tostring method won't work.
public class ExtendsAbstract extends Abstract{
private String t;
public ExtendsAbstract(int y, String t){
super(y);
this.t = t;
}
public String getString(){
return this.t;
}
public int getInt(){
return super.getInt();
}
public String toString(int y){
return(/*super.toString(y)+*/"The integer is "+ y) ;
}
}
This is the abstract class
public abstract class Abstract{
private int y;
public Abstract(int y){
this.y = y;
}
public int getInt(){
return y;
}
public String toString(int y){
return("The integer is :"+y);
}
}
Every time i try and access the toString method from the extended class it just prints out what i think is a memory address. I even didn't mess with the abstract class and it still did that, does anyone know why? Also another question about abstract classes, what advantages do they bring, is it just memory? Because you can't access private members from it so isn't it the same as a normal class, just more restrictions?
Say that you have classes Dad and Son which are defined like this
public class OverLoadedToString {
public static void main(String[] args) {
Son son = new Son();
son.test();
son.test(90);
}
}
class Dad {
void test() {
System.out.println("Dad - test");
}
}
class Son extends Dad {
void test(int testTime) {
System.out.println("Son - test1" + testTime);
}
}
The Son class is extending Dad so the test() with no arguments is inheriting to Son, just like your ExtendsAbstract class is having toString() with no arguments inheriting from Object class (every class in Java inherits Object class).
Then in Son class I added new method test(int testTime), which has got an argument, that makes test() and test(int testTime) different which is called method overloading. In you ExtendsAbstract class there are two toString methods one is the no-arg inherited and the other you defined.
Now let me show you the inheritance flow
Object--->Abstract--->ExtendsAbstract
Object class toString() methods prints the memory address, we can override it in our classes to change its definition, you can return any string that you want. But you haven't overridden it anywhere in either Abstract class or ExtendsAbstract class, so in both classes it will print the memory address.
Now in your Ongoing class you are calling that Object class toString() method which always prints memory address. Your confusion is that you think you have overridden the toString() method but actually you have just overloaded it and you are calling the wrong method for your expected output.
Reference : Java overloading and overriding
Currently, what happens is toString() of object is getting called which goes like this: ClassName#HashCode.
You can do this to print: (Add an #Override at toString()). Also, no need to call josh.toString(). You can simply call System.out.println(josh); (magic will happen and it will call toString)..
public class ExtendsAbstract extends Abstract{
private String t;
public ExtendsAbstract(int y, String t){
super(y);
this.t = t;
}
public String getString(){
return this.t;
}
public int getInt(){
return super.getInt();
}
#Override
public String toString(int y){
return(/*super.toString(y)+*/"The integer is "+ y) ;
}
}
The "toString()" method must be like this:
#Override
public String toString(){
return("The integer is "+ y) ;
}
The "#Override" annotations is used for compiler checking, is not mandatory.
More info about the annotation here: https://docs.oracle.com/javase/7/docs/api/java/lang/Override.html
I'm working on a simple project using Interfaces, but I am having an issue making my class conform to the interface.
My thought process is that since Article implements IDedObject, I should be able to pass an Article as a parameter in my overridden functions within my Article Class definition. Unfortunately this throws the error "The type Article must implement the inherited abstract method IDedObject.getID()"
Interface
public interface IDedObject{
public int getID(IDedObject object);
public void printID(IDedObject object);
}
Class
public class Article implements IDedObject{
private int articleID;
private String articleName;
private String authorName;
#Override
public int getID(Article article){
return article.articleID;
}
#Override
public void printID(Article article){
System.out.println(article.articleID);
}
}
What is missing or incorrect?
Only a guess since we don't have your requirements, but I think that your interface is broken, that your methods shouldn't require parameters much less parameters of its own type. Consider changing:
public interface IDedObject{
public int getID(IDedObject object);
public void printID(IDedObject object);
}
to:
public interface IDedObject{
public int getID();
public void printID();
}
Then the implementation would be trivial
public class Article implements IDedObject{
private int articleID;
private String articleName;
private String authorName;
// constructor and other getter and setter methods here
#Override
public int getID(){
return articleID;
}
#Override
public void printID(){
System.out.println("" + articleID);
}
}
As for your compiler error -- the signature of any overridden methods must match those of the interface methods. So for instance in your Rectangle example in your link, if you extend that class or interface, then the method parameter must take the interface parameter as declared in the interface.
For example, say you had the following interface:
public interface FooInterface {
int getValue();
void printValue();
int difference(FooInterface fi);
}
The concrete class that implements this interface must use a FooInterface parameter for the difference method. For example:
class FooClass implements FooInterface {
private int value;
#Override
public int getValue() {
return this.value;
}
#Override
public void printValue() {
System.out.println(String.valueOf(value));
}
#Override // can't use FooClass for parameter here
public int difference(FooInterface fi) {
return value - fi.getValue();
}
}
The getID and putID methods are not being overriden in Article class. They are being overloaded.
When you change the parameter type, it is an overload and not an override. This may seem a bit confusing at first but the key thing to understand is that Article inherits the two methods from the interface. When you change the parameter type, you are actually overloading these inherited methods rather than overriding them.
That said, the purpose of a getter method is to return the value of an instance variable and optionally perform some operations on this value before returning it.
Your override
public int getID(Article article)
Doesn't override the method of the interface because of a mismatch in the parameter - it should be IDedObject.
You could use a generic parameter to IDedObject, and use a wildcard constraint to make sure it implements IDedObject, but as far as I know, there is no way to tell Java that you want to inherit with the same type as the wildcard.
You need to cast to the implementation of the interface (Article). The method signatures in the interface and the class need to be the same.
public interface IDedObject{
public int getID(IDedObject object);
public void printID(IDedObject object);
}
public class Article implements IDedObject{
private int articleID;
private String articleName;
private String authorName;
#Override
public int getID(IDedObject object) {
Article article = (Article) object;
return article.articleID;
}
#Override
public void printID(IDedObject object) {
Article article = (Article) object;
System.out.println(article.articleID);
}
}
so my goal is to create a method for both the pagesRead() instance variable and the typeHomework instance variable, but when i try this, I get an error saying that they are missing method or to declare abstract. If someone could help me figure this out or send me to another question similar that would be wonderful, thank you.
public abstract class homework {
public int pagesRead();
public String typeHomework();
public abstract void createAssignment(int p) {
return null;
}
}
What you are looking for should be:
public abstract class homework {
abstract int pagesRead();
abstract String typeHomework();
public void createAssignment(int p) {
//
}
}
You inverted everything.
Try this:
public abstract class homework
{
public abstract int pagesRead();//abstract
public abstract String typeHomework();//abstract
public abstract void createAssignment(int p);//since it is an abstract you can't create a body
}
First an abstract method cannot have implementation.
Your code should be
public abstract class homework {
public abstract int pagesRead();
public abstract String typeHomework();
public void createAssignment(int p){
}
}
Do you mean to create class members? That is pagesRead and typeHomework should be elements of class Homework and not methods? If so, then following is the proper way
public class Homework{
public int pagesRead;
public String typeHomework;
public void createAssignment(int p)
{ return null; }
}
What you have declared in the abstract class is reverse .
1) Declared non abstract methods
2) given implementation for abstract methods
You should
1) Declared abstract methods
2) given implementation for non abstract methods
I am a little confused on how to set up the TestHomework method so that it prints properly when using the toString() method. Right now when I run the main method it prints "null - 0" but what I would like it to say is "Math - 6". This program is supposed to extend an abstract class. It is supposed to say how many pages there are for homework and for what subject.
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
{
// initialise instance variables
pagesToRead = 0;
typeHomework = "none";
}
public Homework(int pages, String hw) {
this.pagesToRead = pages;
this.typeHomework = hw;
}
public abstract void createAssignment(int p);
public int getPages() {
return pagesToRead;
}
public void setPagesToRead(int p) {
pagesToRead = p;
}
public String getTypeHomework() {
return typeHomework;
}
public void setTypeHomework(String hw) {
typeHomework = hw;
}
}
public class MyMath extends Homework {
private int pagesRead;
private String typeHomework;
public MyMath(int pages, String hw) {
super(pages,hw);
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
public String toString() {
return typeHomework + " - " + pagesRead;
}
}
public class TestHomework {
public static void main(String[] args) {
MyMath one = new MyMath(6, "Math");
one.createAssignment(6);
System.out.println(one);
}
}
That's because you are defining the 2 properties (that one of them happen to have the same name as one of the abstract class's) but you are not initializing them, you are initializing those of the abstract class. (So their values is always set to their type's default)
You need to drop those from the MyMath class, & define the toString method in your abstract class: it's the one to be used by default by its inheriting classes.
public abstract class Homework {
private int pagesToRead;
private String typeHomework;
// Same code
// Define the toString here
#Override
public String toString() {
return typeHomework + " - " + pagesToRead;
}
}
public class MyMath extends Homework {
// You don't need to define any extra attributes
public MyMath(int pages, String hw) {
super(pages,hw);
}
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
}
public static void main(String[] args) {
// Calls the constructor of the MyMath class, which in turn
// invokes the constructor of its superclass, the 'Homework' class
MyMath one = new MyMath(6, "Math");
one.createAssignment(6);
// Invokes the toString of the MyMath class. Since it does not have one,
// The toString method of its superclass (Homework) is called.
System.out.println(one);
}
Your derived class has its own typeHomework and pagesRead fields, which are never set (even though the base class happens to have fields with the same names). Therefore, they stay null and 0.
You should delete those fields and use the data from the base class, via the public getter methods.
Why it doesn't work:
Be careful you redeclared the attribute typeHomework of you parent class. Attributes are automatically added to your extending class so you don't have to write them again.
By redeclaring it you confused the compiler, viewing your code in debug shows, that your one object contains your typeHomework twice:
typeHomework = null // The one from the super class
typeHomework = "Math" // The one from your child class
Your method now uses the typeHomework from your super-class therefor the output is null!
pagesRead is 0 because you are setting the pagesToRead of your super-class to 6(not pagesRead!) when calling setPagesToRead(p);.
Some style tips
Use the #Override annotation when overriding methods like this:
#Override
public void createAssignment(int p) {
setTypeHomework("Math");
setPagesToRead(p);
}
It's not really needed but it's good practice (readers of your code know that it overrides something).
When referring to attributes of your class it's also good practice to use the this statement so it's clear, that you're referring to an attribute and not a local variable:
#Override
public String toString() {
return this.typeHomework + " - " + this.pagesRead;
}
I would like to ask you some tips about this java scenario:
I have a simple interface called Sequence that performs some basic operation. Now I would like to implement some additional methods in a separate class, called SequenceWrapper, that implements the Sequence defined above. Here is some example code that looks like my real code:
public interface Sequence {
public void methodOne();
public int methodTwo();
}
public abstract class SequenceWrapper implements Sequence {
private wrappedSequence = null;
public SequenceWrapper(Sequence sequence){
this.wrappedSequence = sequence;
}
public void methodOne(){
wrappedSequence.methodOne();
}
public int methodTwo(){
return wrappedSequence.methodTwo();
}
}
public class ConcreteWrapper extends SequenceWrapper {
public ConcreteWrapper(Sequence sequence){
super(sequence);
}
// Just an example
public int addMethodOne(){
int a = super.methodTwo();
return a + 3;
}
}
Now if I want to implements a class with another method (say 'addMethodTwo()') I can simply extends the 'ConcreteWrapper' class and add only the new method:
public class ConcreteWrapperTwo extends ConcreteWrapper {
public ConcreteWrapperTwo(Sequence sequence){
super(sequence);
}
public int addMethodTwo(){
int a = super.methodTwo();
return a + 30;
}
}
What do you think? Is this code correct or it's preferable another strategy??
Thanks in advance
First, your private wrappedSequence = null; has no type.
I suppose you meant private Sequence wrappedSequence = null;
Second, in your example you will never be able to instantiate any of the classes, since all of them receive another Sequence in the constructor and there is no way of create the first instance of Sequence.
Third, composition over inheritance is a good approach, if you really need it. Usually you wrap an object when you need to hide or protect the access to the wrapped object. In your case, within the wrapper you are exposing all of the methods of the wrapped object. You then create new methods that will affect the wrapper object, but not the wrapped one.
What you probably need is just a normal inheritance scenario:
I would like to walk you through you a breakdown for this Java scenario:
I have a simple interface called Sequence that performs some basic operation. Now I would like to implement some additional methods in a separate class, called SequenceWrapper that implements the Sequence as defined above. Here is some example code to explain what I mean:
public interface Sequence {
public void methodOne();
public int methodTwo();
}
public abstract class AbstractSequence implements Sequence {
public SequenceWrapper( ){ }
public void methodOne(){
//basic behavior here
}
public int methodTwo(){
//basic behavior here
}
}
public class ConcreteSequence extends AbstractSequence {
public ConcreteSequence ( ){
super( );
}
// Just an example
public int addMethodOne(){
int a = methodTwo();
return a + 3;
}
}
public class ConcreteSequenceTwo extends AbstractSequence {
public ConcreteSequenceTwo( ){
super( );
}
public int addMethodTwo(){
int a = methodTwo();
return a + 30;
}
}