public interface Person {
class User implements Person {
void live() {
System.out.println("I usually just live.");
}
}
class Loser implements Person {
void doNothing() {
System.out.println("I usually do nothing.");
}
}
class Coder implements Person {
void writeCode() {
System.out.println("I usually write code.");
}
}
class Programmer implements Person {
void enjoy() {
System.out.println("It's a wonderful life!");
}
}
}
Let's see in Line 1 : declare interface Person
Line: 2,8,14 and 20 : it class implement Person
Question
Why we must implement Person when user Class(line2) , Loser class(line8), Coder class(line14) , and Programmer class(line20) stayed in Person interface?Why in Person.java has implement by itself?
Resolved
Comments (8)
- Popular
- New
- Old
You must be signed in to leave a comment
Khongpak Phupkdee
28 August 2021, 06:28
Thank you 🥳🥳
0
Gellert Varga
27 August 2021, 08:34useful
If the interface has not only methods but also some data fields (variables), they are static by default. You don't have to put the 'static' word in front of it and it's still static. (You can't create object instances of the interface, so it doesn't make sense for it to have instance variables.)
So:
these classes within the interface are static.
So I think it's almost like they're not even in it.
It's almost the same as writing it in another file.
It's easier to understand if you write it this way:
But for the JVM, this means exactly the same if I write this:
In this second version, the interface remains the same. The core of the interface that the User class implements is lines 1 and 2; the rest (between line4 and line8) I treat as if it were not the interface.
The difference is that in the second version, you can't access the class and create an instance of it this way:
new User() ; // Error
This is the only way to create an object from it:
new Person.User() ; +2
Thomas
27 August 2021, 09:11
That's not entirely correct. An interface can have both, (static) nested an inner classes. So the static modifier does matter (for classes - fields are always public static final as you already mentioned).
To see that create in the same package:
You could change the interface keyword to class and see that it behaves now exactly the same as when being an interface. +3
Gellert Varga
27 August 2021, 10:05
1) The program you outlined was only compilable for me in the following form.
I wrote it in single file, exactly like this:
User pu = new User(); // this way: "cannot find symbol" !
2) What does it mean or what would be the difference if an interface has an internal class is static or non-static? Unfortunately, I don't know anything about that.
((In my passed solution the classes inside the Person interface are not declared static.)) +3
Thomas
27 August 2021, 10:52
ouch... sometimes I should test things before posting :) You're right, this is not working. When I tested this now there's no difference when static or non static classes are inside an interface.
To create an object of an inner class (non static) you'd need a reference to the outer class - so to instanciate an object you first need to create an object of the outer class and then you can create an instance of the inner one (passing the outer objects reference to the inner objects constructor)
+2
Thomas
27 August 2021, 11:06
And now that I understood the initial question I can try to answer it: I do not know use cases for interfaces containing classes that implement the outer interface despite testing implementations whilst developing like
+2
Gellert Varga
27 August 2021, 11:14
OK:)
+1
Thomas
27 August 2021, 08:27
You don't need to implement the Person class. You should simply create a
a) Person object (User, Loser or Coder) and pass it to the
b) doWork method (which you should implement).
0