I will take the java silver exam next week, so I decided to eliminate the ambiguity.
Let's sort out the interface
and the abstract class
, which were a bit confusing. That is the purpose of this article.
"Abstract method is declared with abstract. If not, compile error" is written or It is written that "if omitted, public abstract is implicitly given" ...
I was confused as "that? Misunderstanding?" So I wanted to organize it once and get out of the mess. When I think about it now, I think the abstract methods, abstract classes, and interfaces were messed up. I'm thinking.
We have compiled a list of what can be defined in interfaces and abstract classes. Organize. I've organized them into methods and field variables.
First from the method.
I've put them together in a table for each of the interface
and the abstract class
.
What can be defined | Access modifier | Can it be omitted? | Other |
---|---|---|---|
Abstract method | public | Public even if omitted | If it's an interface, you can omit abstract. |
default method | public | Can be omitted | * It can be defined after SE8. |
static method | public or private | If omitted, public | * It can be defined after SE8. |
--It seems that the abstract method declared in the case of the interface can also omit the ʻabstract modifier`. ――Is it a common rule that if you do not describe the access modifier, it will be public? --For static methods, it's a little annoying that you can specify private access modifiers as well as public ones.
This is a guess I personally arrived at ... Basically, only abstract methods can be defined in the interface (up to SE8), so even if you don't explicitly declare abstract, the compiler will read it in the air and add abstract. I decided to think.
What can be defined | Access modifier | Can it be omitted? | Other |
---|---|---|---|
Abstract method | Anyway | abstract is required. | Access modifiers are optional. Even if you omit it, it will not be public |
Concrete method | Anyway | Can be omitted | Access modifiers are optional. Even if you omit it, it will not be public |
--Unlike interfaces, access modifiers can be freely defined. --If you omit the access modifier, it will be treated as a method without modifier together with the concrete class. --The abstract method ʻabstract modifier` is ** required !!!**
Since concrete methods and abstract methods are mixed in the abstract class, I decided to think that it is necessary to explicitly add abstract so as not to be confused. (This is also a guess I personally arrived at.)
Field type | Can you define it? Can not? | Can static be omitted? | Other |
---|---|---|---|
Non-static variable (instance variable)) | Cannot be defined | - | Interfaces can't be instantiated. |
static variable | It can be defined, but it will be a constant | Can be omitted | Even if omittedpublic static final Will become. |
--Instance variables cannot be defined
--Static variables become public stati final
whatever
You can't instantiate an interface, so you don't have to think about variables when you instantiate it. That's why it can't be defined. Yup. Convinced. It can't be instantiated, but it can be handled as a type, so this is another complicated point.
Field type | Can you define it? Can not? | Can static be omitted? | Other |
---|---|---|---|
Non-static variable (instance variable)) | Can be defined | I don't need it in the first place | You can define any access modifier. |
static variable | Can be defined | Cannot be omitted | If you omit static, it will be an instance variable. You can define any access modifier. |
――It's no different from the concrete method!
--Interface --Abstract method → abstract is ** optional ** --default method --static method → ** Only static methods can be handled private ** --static variables → static ** Variables can only be used as constants **
--Abstract class --Abstract class → ** Abstract should be written ** --Concrete method --static variables --Non-static variables
It's quite a memo for me, but I still need time to organize it like this. If the interpretation is different or incorrect, we apologize for the inconvenience, but we would appreciate it if you could point out m (_ _) m.
Recommended Posts