Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Why does method need to return a String rather than leave it as void?

  1. #1
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Question Why does method need to return a String rather than leave it as void?

    Hello, I was practicing polymorphism messing around with a Car analogy when I came across a problem. In my main method within my for loop, I tried to access my starteEngine(), accelerate(), and brake() methods yet I would get an error. I have since fixed it and found out that it worked once I switched it from "void" to String and returned the string. But I want to know why it wouldn't work as it is below. I thought that with void since it's not returning anything, it would simply execute the output line, and by accessing it as I tried in the for loop within my main method, it would simply output as well. That wasn't the case, so my question is if anyone could explain this to me? Thank you.

    //	06.03.2021
    //Polymorphism
     
    class CarP {
     
    	//Fields
    	private boolean engine;
    	private int cylinders;
    	private int wheels;
    	private int doors;
    	private String color;
    	private String name;
     
    	//Constructor
    	public CarP(int cylinders, String name, int doors, String color) {
    		this.wheels = 4;
    		this.engine = true;
    		this.cylinders = cylinders;
    		this.name = name;
    		this.doors = doors;
    		this.color = color;
    	}
     
    	//Methods
    	public void startEngine() {
    		System.out.println("Starting engine.");
    	}
     
    	public void accelerate() {
    		System.out.println("Accelerating.");
    	}
     
    	public void brake() {
    		System.out.println("Breaking.");
    	}
     
    	//Getters and Setters
    	public int getCylinders() {
    		return cylinders;
    	}
     
    	public void setCylinders(int cylinders) {
    		this.cylinders = cylinders;
    	}
     
    	public int getDoors() {
    		return doors;
    	}
     
    	public void setDoors(int doors) {
    		this.doors = doors;
    	}
     
    	public String getColor() {
    		return color;
    	}
     
    	public void setColor(String color) {
    		this.color = color;
    	}
     
    	public String getName() {
    		return name;
    	}
     
    	public void setName(String name) {
    		this.name = name;
    	}
     
    }//end Car
     
    class DodgeCharger extends CarP{
     
    	public DodgeCharger() {
    		super(8, "Dodge Charger", 4, "Mute black");
    	}
     
    }
     
    class DodgeChallenger extends CarP{
     
    	public DodgeChallenger() {
    		super(8, "Dodge Challenger", 2, "Mute black");
    	}
     
    }
     
    class ToyotaSupra extends CarP{
     
    	public ToyotaSupra() {
    		super(6, "Toyota Supra", 2, "White");
    	}
     
    }
     
    public class CarV2 {
     
    	public static void main(String[] args) {
     
    		for(int i = 1; i < 6; i++) {
    			CarP car = randomCar();
    			System.out.println("Car #" + i + " : " + car.getName() + "\nCar Color: " + car.getColor() + "\nCylinders: " + car.getCylinders() + "\nNumber of doors: " + car.getDoors() + "\n" + car.startEngine() + "\n "
    					+ car.accelerate() + "\n" + car.brake() + "\n");
    		}
     
    	}//end main
     
    public static CarP randomCar() {
     
    		int randomNumber = (int)(Math.random() * 3) +1;
    		System.out.println("Random number generated was: " + randomNumber);
    		switch(randomNumber) {
    		case 1:
    			return new DodgeCharger();
    		case 2:
    			return new DodgeChallenger();
    		case 3:
    			return new ToyotaSupra();
    		}
     
    		return null;
     
    	}//end randomMovie()
     
    }//end class
    Last edited by HyperRei; June 3rd, 2021 at 04:19 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why does method need to return a String rather than leave it as void?

    Whether a method returns an value or void depends on what the requirements are for the method.

    Concatenating a call to a method in a concatenation of Strings to build a String requires that the method returns a value that can be a String.
    void is not allowed. All the methods called in the argument to println method where a String is being built by concatenation requires that all the methods return a String.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    HyperRei (June 3rd, 2021)

  4. #3
    Member
    Join Date
    Aug 2020
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Why does method need to return a String rather than leave it as void?

    Ohhh okay I see, that makes sense. Thank you so much.

Similar Threads

  1. incompatible types: void cannot be converted to String
    By Nomi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 28th, 2014, 04:43 AM
  2. Using a String[] in a Void or INT???
    By Sidonius in forum What's Wrong With My Code?
    Replies: 11
    Last Post: April 27th, 2014, 04:04 PM
  3. Public static void (String[] args)
    By Ganjasaur in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 18th, 2013, 02:06 PM
  4. Need a non void method to return the first three characters of a string
    By fallout87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 9th, 2011, 10:00 PM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM

Tags for this Thread