[JAVA] Interface / abstract class / override

I'm a beginner, but I wrote it for personal study

Interface

An interface does not provide concrete processing like a class, but is explicitly stated to define ** rules **, and the actual processing is defined by the class that implements the interface. As a result, in the field of a company where multiple people create a program, by defining the method that you want the child class to implement in the parent abstract class as an abstract method, even the child class created by other people can be used. There is an advantage that the method is always implemented

** * Abstract method ** ・ ・ ・ Method that defines only name / type / argument

① Only the "mold" is taken out from the class

** * "Type" ** ... itself ** "How to decide how to handle" **, and when declaring a variable, the type is specified in order to decide how to handle the variable.

-It should be noted that ** the type of the object to be handled and the "type of the thing to be handled" specified by the type are different concepts **. For example, the type and handling method are different so that you can change whether the number 1 is handled as an int type or a double type.

(2) Those that specify "handling" from other classes

-The interface is ** automatically made public ** by the compiler because all specified methods are interpreted as public so that they can be handled by other classes. -Methods defined in the interface cannot be qualified with protected or private (from Java8, the contents can also be defined by adding the default modifier). -As you can see from using implements when a class inherits an interface, it only works if there is a ** "class that implements the specification" **.

③ Interface cannot have implementation

** Since the interface only specifies how to handle **, ** cannot have an implementation , so create an instance and ** describe something that operates dynamically (thing that changes dynamically) Can not However, it can be described if either of the following two conditions is met.

-Use ** final ** and the value should not be changed dynamically (constant) -Cannot create an instance using ** static **

④ Interface inheritance

-Inheritance can define a new class that extends the class function as you can see from using extends, but you can also define (inherit) a new interface that extends a certain interface with extends. -** Multiple inheritance of classes is prohibited, but multiple realization of interfaces is allowed **

Abstract class

(1) Has both interface and class properties

· Can have both ** concrete methods ** with implementations and ** abstract methods ** without implementations -The concrete method defined in the abstract class is inherited by the subclass that inherits the abstract class. -** Abstract methods must be overridden and reimplemented in their subclasses **

② Cannot be instantiated

-Abstract classes with interface characteristics (abstract methods) cannot be instantiated like interfaces ** -As you can see from the fact that it cannot be instantiated, it is a prerequisite that it is inherited and used **. -The abstract method defined in the abstract class must be implemented by a concrete class that inherits it ** -Abstract classes that inherit from abstract classes do not necessarily have to be implemented like concrete classes, but can be implemented by extending the original abstract class, adding new abstract methods, or overriding existing abstract methods. it can.

③ Field definition

-The instance of the subclass that inherits the abstract class includes the instance of the abstract class. In other words, an instance of a subclass that inherits an abstract class contains an instance of the abstract class, and an interface that provides only types can only have constant fields, but an abstract class can define fields whose values can be changed dynamically.

override

-** Override ** is to ** redefine ** the method defined in the superclass in the subclass. ・ Be careful not to mistake it for ** overload **, which represents ** overload **.

There are three rules for overriding

① To redefine the method, the ** signatures of the methods must be the same ** ** Signature **: A combination of method name, argument list type, number, and order ② ** The return value is the same or it is a subclass ** ③ ** Access modifiers can be looser than the original definition, but cannot be strict ** -The following example can be overridden because Integer type is a subclass of Number type.

Sample.java


public Number method{
//any code
}

Sample.java


public Integer method{
//any code
}

Override added (redefined)

-Because the override is not an overwrite but an add (redefinition), there are multiple methods with the same name in the subclass instance. In this case, the overridden method is used

Recommended Posts

Interface / abstract class / override
Difference between interface and abstract class
java (abstract class)
interface and abstract
[java] abstract class
Proper use of interface and abstract class
Java Basic Learning Content 6 (Inheritance / Abstract Class / Interface)
About java abstract class
Use of Abstract Class and Interface properly in Java
Advanced inheritance abstract, interface -java
Java learning memo (abstract class)
interface