I study a method call in a method (I do not know the exact term).
I wrote a mini-test (work) program in which I set some data to the object and output it. The program is working, but is it written correctly or can it be made better?
Interested in calling a method of one class through a method of another class. So, for example, I call the method of the class CarImp.java - getCountry using country.getCar() .
PS The question asked earlier on this topic in order to understand what I want to achieve - link to Stack
MainTest.java
public class MainTest{ public static void main(String[] args) { Country country = new CountryImpl(); country.getCar().setCountry("German"); System.out.println(country.getCar().getCountry()); System.out.println(country.getCar().getEngine()); } } Country.java
public interface Country { Car getCar(); } CountryImpl.java
public class CountryImpl implements Country{ public final Car car; public CountryImpl() { this.car = new CarImpl(); } @Override public Car getCar() { return car; } } Car.java
public interface Car { String getEngine(); void setCountry(String country); String getCountry(); } CarImpl.java
public class CarImpl implements Car { String country; String brand; @Override public String getEngine() { if(country.equals("Germany")) return "Good engine"; if(country.equals("Japan")) return "Many times better than German"; return "Bad engine"; } public void setCountry(String country) { this.country = country; } @Override public String getCountry() { return country; } }
country.getCar().getCountry(),country.getCar().setCountry("German")Why is the country first? And what if I write a house method? That will then be, for example,contry.getHouses.get(0).getHouseName- Antonio112009