[Solved] error compilation in print method [duplicate]


You have written the following code:

private void print(String string) {
        return string; 
    }

But void here is the return type. It thus means you cannot return anything. If you write String instead of void then it will mean that the return type will be a string which is the case and the error will go away.

It should be like this:

private String print(String string) {
        return string; 
    }

Or as it is a print function if you want to keep it void then print the string there itself.

As requested in comment here is how the code should be. The main
function should be inside Player class. Then we define an object of
the Player class in the main function to call its methods.

abstract class Motor{
        int fuel;
        int getFuel(){
            return this.fuel;
        }
        abstract void run();
    }

   public class Player extends Motor {
       public void run(){
           print("wroooooom");//calling print method to print passed string
        }

    public void print(String string) {
        System.out.print(string);
    }

  public static void main(String []args){
    Player p1 = new Player();//creating a object of Player class to access its methods
    p1.run();//calling the run method
    }
}

8

solved error compilation in print method [duplicate]