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

joshua.vw :

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?

Arun Sudhakaran :

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=109777&siteId=1