[Java] abstract (abstract class) and interface (interface)

1 minute read

What is #abstract (abstract class) A class that has one or more abstract methods.

Abstract method is a method that declares only the definition of arguments and method names without describing specific processing.

Abstract class declaration.java


abstract class class name {}

Abstract method declaration.java


abstract return type method name (argument type argument name);

Feature

⭕️ Superclass methods can be overridden (rewritten) by subclasses.

❌ Cannot inherit multiple classes.

❌ It cannot be instantiated directly.

What is #interface A method that defines only the types of variables and methods without describing the specific processing of the methods included in the class.

Interface declaration.java


interface interface name {}

Implementation of

interface.java


class class name implements interface name {}

Feature

⭕️ Only the method type can be described first, and the process can be described immediately before use.

⭕️ Multiple inheritance of interface is OK.

image

abstract class

Son Goku (super class) can inherit the surname of Son to multiple names of Son Gohan (subclass) and Son Goten (subclass).

Also, if Gohan enters his son-in-law, the family name will be rewritten to Suzuki (override).

Interface

Gohan can take over the specifications from Goku to Saiyan and from Chichi to Earthling.

The precise gene is decided immediately before.