[Java] [javaSliver] The interface and abstract classes are complicated, so I will summarize them.

Purpose of this article

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.

Why I was confused

"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.

Rule comparison

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.

About the method

First from the method. I've put them together in a table for each of the interface and the abstract class.

** Interface methods **

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.

point

--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.

Why can I omit the abstract modifier in an interface?

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.

** Abstract class methods **

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

point

--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 !!!**

Why can't I omit the abstract modifier in an abstract class?

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.)

Then the variables

interface

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 finalWill become.

point

--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.

** Abstract class **

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.

point

――It's no different from the concrete method!

Summary

--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

at the end

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

[Java] [javaSliver] The interface and abstract classes are complicated, so I will summarize them.
I stumbled on the Java version in Android Studio, so I will summarize it
[JAVA] What is the difference between interface and abstract? ?? ??
Java abstract methods and classes
[Java] I thought about the merits and uses of "interface"
I passed the Java test level 2 so I will leave a note
[JAVA] Difference between abstract and interface
Ruby arrays, hashes, and symbols are confusing, so I summarized them.
I tried to summarize the methods of Java String and StringBuilder
Specify the order in which configuration files and classes are loaded in Java
A note on the differences between interfaces and abstract classes in Java
JAVA learning history abstract classes and methods
I didn't know that inner classes could be defined in the [Java] interface
Understand the difference between abstract classes and interfaces!
I compared the characteristics of Java and .NET
Summarize the differences between C # and Java writing
The handling of Java Future is too dull, so I will try various ideas.
interface and abstract
I passed Ruby Silver (June 2020), so I will summarize it.
Java classes and instances to understand in the figure
I summarized the types and basics of Java exceptions
Use of Abstract Class and Interface properly in Java
I noticed that I am developing Java FW in the mono repo, so I will consider the operation.
[Java] Method call error when inheritance and interface implementation are performed at the same time