I was following a tutorial on the Java website about object oriented programming, particularly classes and interfaces.
I had defined a class (oldTelevision) and then defined an interface (Television). I then used 'class oldTelevision implements Television' to try and tell oldTelevision how to act but was faced with the error message : "oldTelevision is already defined in unamed package".
Now, I get why it's saying this. I've already defined the states and behaviours of the oldTelevision class so my programme is confused as to why I'm trying to define these things again with an interface. But because of this I'm not really sure what the point of an interface is if I can write an objects behaviour right into it's class. The only exception I can think of would be if you were using the interface to define the behaviour of subclasses.
This has confused me though, because as far as I can see you have to define a class before you can tell it to implement anything, but by defining it you're removing the need for an interface.
I'll include my script below for people to check out but I hope you guys can help as this feels a bit like a brick wall.
Thanks in advance,
Dev.
class oldTelevision {
int On = 0;
int Off = 0;
int Volume = 0;
int Brightness = 0;
void turnOn (int newValue) {
On = newValue;
Off = newValue - newValue;
}
void turnOff (int newValue) {
Off = newValue;
On = newValue - newValue;
}
void upVol (int increment) {
Volume = Volume + increment;
}
void downVol (int decrement) {
Volume = Volume - decrement;
}
void upBright (int increment) {
Brightness = Brightness + increment;
}
void downBright (int decrement) {
Brightness = Brightness - decrement;
}
interface Television {
// image on screen
void turnOn (int newValue);
void turnOff (int newValue);
void upVol (int increment);
void downVol (int decrement);
void upBright (int increment);
void downBright (int decrement);
}
class oldTelevision implements Television {
}


Sign In
Create Account


Back to top









