im currently reading a book about programming Android and there is a nice little reference guide on Java in the beginning chapters. However, I stumpled upon something about implicit parameters that I did not quite understand.
He defines the class Car:
public class Car {
public void drive() {
System.out.println("Going down the road!");
}
}
Then he continues on with this:
public class JoyRide {
private Car myCar;
public void park(Car auto) {
myCar = auto;
}
public Car whatsInTheGarage() {
return myCar;
}
public void letsGo() {
park(new Ragtop()); // Ragtop is a subclass of Car, but nevermind this.
whatsInTheGarage().drive(); // This is the core of the question.
}
}
I just want to know how we can call drive() from the class Car when JoyRide is not an extension of Car. Is it because the method whatsInTheGarage() is of return type Car, and thus it “somehow” inherits from that class?
Thanks.
Think about this piece of code:
as a shorthand for this:
Is it clear now? All
C-likelanguages with c-like syntax behave like this.UPDATE: