Java, interface to start from beginner

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

Interface

There is a mechanism called ** interface ** that is used in a similar way to the ** abstract class ** we dealt with last time. An abstract class was a type of class, and interfaces are created for similar purposes, but their declaration and implementation methods are different.

declareInterface.java


interface Felidae
{
  String type = cats;
  void meetsCats();
}

The usual class declaration is gone and is replaced by ʻinterface. Like a class, it can have fields and methods (** it can't have a constructor **), ** Objects such as Felidae cat = new Felidae ();cannot be created **. Also, sincevoid meets Cats ()is an abstract method, it is necessary to ** prepare a concrete method ** by the class that implements theFelidae` interface (override).

String type = cats; is an interface field, but this is a ** constant ** and cannot be changed. That's because the fields in the interface are in the same state as with public static final (I don't yet know why).

inplementedFlidae.java


class Housecat inplements Felidae
{
  void meetsCats()
  {
    System.out.println("I found a cat. I think it's a domestic cat somewhere.");
  }
}

Instead of the extension ʻextends, it is ʻimplements, but the shape is the same as before.

Principle: Only one superclass

Why do we need an abstract method interface? I'd like to do everything with class extension, but Java ** doesn't allow multiple inheritance by multiple classes **. The parent superclass is limited to only one. It seems that you should not think in the same way as the system diagram that humans think. I've seen a few reasons for this, but it seems that there is some depth here, such as the call when the method name is worn and the diamond problem. Another language, C ++, allows this multiple inheritance.

However, it is difficult to use what you have prepared once. Therefore, instead of multiple inheritance by classes, we are trying to maintain simplicity as a language by allowing only partial multiple inheritance by abstract classes and interfaces. ** Interface implementation can be done with two or more **. You can also ** extend the interface itself ** and ʻextends` superinterfaces to subinterfaces.

At the end

Looking at the part where inheritance by extension is allowed, I feel that Java is a language that is good at deductive development. Knowing the class interface that has already been created and assuming that you will build a new class while inheriting its functions means that you will start studying Java by first knowing the functions provided by the standard library.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition Java SE11 Silver Problem Collection (commonly known as Kuromoto)

Recommended Posts

Java, interface to start from beginner
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Migration from Cobol to JAVA
Java starting from beginner, override
New features from Java7 to Java8
Connect from Java to PostgreSQL
Java, instance starting from beginner
Java starting from beginner, inheritance
Java8 to start now ~ Optional ~
From Ineffective Java to Effective Java
Java development for beginners to start from 1-Vol.1-eclipse setup
java beginner 4
[Java beginner] About abstraction and interface
java (interface)
java beginner 3
java beginner
[java] interface
Java to be involved from today
From Java to VB.NET-Writing Contrast Memo-
Java overload constructor starting from beginner
The road from JavaScript to Java
Java, arrays to start with beginners
Java 8 ~ Stream API ~ to start now
[Java] Conversion from array to List
Interface Try to make Java problem TypeScript 7-3
Significance of interface learned from Java Collection
Java starting from beginner, nested / break / continue
Convert from java UTC time to JST time
Connect from Java to MySQL using Eclipse
Java 8 to start now ~ Date time API ~
From installing Eclipse to executing Java (PHP)
Post to Slack from Play Framework 2.8 (Java)
Java: How to send values from Servlet to Servlet
[Java] Flow from source code to execution
Introduction to monitoring from Java Touching Prometheus
Java, for statement / while statement starting from beginner
Precautions when migrating from VB6.0 to JAVA
Memo for migration from java to kotlin
Type conversion from java BigDecimal type to String type
About Java interface
Java Beginner Exercises
[Java] Introduction to Java
[Java] Functional interface
About interface, java interface
Introduction to java
Java Exercise "Beginner"
[Java] From two Lists to one array list
Two ways to start a thread in Java + @
Run R from Java I want to run rJava
Connect to Aurora (MySQL) from a Java application
To become a VB.net programmer from a Java shop
How to get Class from Element in Java
Java starting from beginner, class declaration / object generation
[Java] How to switch from open jdk to oracle jdk
Java8 to start now ~ forEach and lambda expression ~
I want to write quickly from java to sqlite
Minecraft BE server development from PHP to Java
Select * from Java SDK to Azure Cosmos DB
About Java functional interface
Launch Docker from Java to convert Office documents to PDF