简体   繁体   中英

Class that is extended from an abstract class toString method doesn't work :/

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM