Re: void type not allowed in println(). Was Re: Inheritance and lousy
Frame
NickName wrote:
Now, a new question arises,
It is usually a good idea to start a new thread for a new question
rather than adding your question to an existing discussion.
I have changed the subject to indicate the new question.
public void speed() {
System.out.println("30 mph");
}
public static void main(String[] args) {
Dog pet = new Dog();
System.out.println(pet.speed());
println() takes an argument that should be printable, usually that means
a string. Your speed() method soes not return a String value or any
other type of value that can be printed.
};
The above attempt failed, err msg:
"MammalClass.java": 'void' type not allowed here at line 24, column 33
Println cannot print nothingness.
You can fix this in one of two ways ...
1)
Change speed() to
public String speed() {
return "30 mph";
}
OR
2)
Change
System.out.println(pet.speed());
to
pet.speed();
Don't do both changes :-)
A note on naming ...
I'd name your existing speed() as printSpeed()
I'd name the revised speed() in (1) as getSpeed()
Designwise, maybe `public int getMPH()` would be better?