Ok, I am stupid, I am new to this, I get that....but what the heck?!
According to this article ( that I read over and over again ) https://codegym.cc/groups/posts/104-the-difference-between-abstract-classes-and-interfaces " An interface only describes behavior. It has no state."
Also:
But if we try to do the same with an interface, the picture is different. We can try to add variables to it:
public interface CanFly {
String species = new String();
int age = 10;
public void fly();
}
public interface CanFly {
private String species = new String(); // Error
private int age = 10; // Another error
public void fly();
}
We can't even declare private variables inside an interface. Why? Because the private modifier was created to hide the implementation from the user. And an interface has no implementation inside it: there isn't anything to hide.
An interface only describes behavior. Accordingly, we can't implement getters and setters inside an interface. This is the nature of interfaces: they are needed to work with behavior, not state.
Ok, Isn't the code in the WeatherType.java implementation? Once again, I know I'm stupid, but this is.... in the article you say "this cannot be done" and here I see it done...What?!package com.codegym.task.task13.task1317;
/*
Nice weather
*/
public class Solution {
public static void main(String[] args) {
System.out.println(new Today(WeatherType.CLOUDY));
System.out.println(new Today(WeatherType.FOGGY));
System.out.println(new Today(WeatherType.FREEZING));
}
static class Today implements Weather {
private String type;
Today(String type) {
this.type = type;
}
public String getWeatherType() {
return this.type;
}
@Override
public String toString() {
return String.format("Today it will be %s", this.getWeatherType());
}
}
}