[Java beginner] About abstraction and interface

Overview

I will post it as a review of ** Abstraction ** and ** Interface **.

Abstraction

In the upper module, declare the methods and variables that are commonly implemented in the lower module, and leave the implementation to the lower module. Also, abstracted classes cannot be instantiated.

**-Advantages of abstraction ** --Prevention of forgetting to override —— Make a distinction from processing that does nothing --Prevention of unintended instantiation

Human.java


public abstract class Human{
  private String name;
  public Human(String name){
    this.name = name;
  }
  public String getName(){
    return name;
  }
  public void setName(String name){
    this.name = name;  
  }
  //Declare common methods and leave implementation to lower modules
  public abstract void talk();
  //Method that does nothing
  public void tmp(){}
}

Japanese.java


public class Japanese extends Human{
  public Japanese(String name){
    super(name);
  }
  @Override
  public void talk(){
    System.out.println("Hello");
  }
}

American.java


public class American extends Human{
  public American(String name){
    super(name);
  }
  @Override
  public void talk(){
    System.out.println("Hello");
  }
}

Main.java


public class Main{
  public static void main(String[] args){
    //Human human = new Human("Yamada Taro");
    //Compile error because instance cannot be created

    Japanese jp = new Japanese("Yamada Taro");
    jp.talk();    //Hello
    American en = new American("TaroYamada");
    en.talk();    //Hello

  }
}

interface

In the interface, you can declare a method to be implemented in common as in the abstraction, and leave the implementation to the class that inherits the interface. However, fields are treated differently from abstract classes.

** Interface features ** --Basically, it doesn't have any fields --All methods are abstract methods.

Basically, it is not allowed to implement fields in the interface, but only fields with ** public static final ** can be declared, and they are automatically added when the variable declaration is started from the type name in the interface. Will be done. Since it has a ** final ** modifier that cannot be reassigned, it must be initialized at the same time as the variable declaration.

Regarding methods, the methods that can be declared in the interface are limited to those that are ** public ** and ** abstract **, and are automatically added when declared from the return type.

Creature.java


public interface Creature{
  //public static final double PI = 3.14;
  double PI = 3.14; //It automatically becomes a public static final variable.
  
  //public abstract void breath();
  void breath();  //It automatically becomes a public abstract method.
}

Summary

My honest impression is that it is difficult to benefit from the benefits because I have little development experience using inheritance.

Recommended Posts

[Java beginner] About abstraction and interface
About Java interface
[Java] About interface
About interface, java interface
About Java functional interface
[Java] About String and StringBuilder
About Java Packages and imports
About Java static and non-static methods
Differences between "beginner" Java and Kotlin
About fastqc of Biocontainers and Java
java beginner 4
java (interface)
java beginner 3
[Java beginner] == operator and equals method
java beginner
[java] interface
[JAVA] Difference between abstract and interface
Java, interface to start from beginner
About Java primitive types and reference types
This and that about Base64 (Java)
[Java] I thought about the merits and uses of "interface"
Java starting from beginner, variables and types
[About JDBC that connects Java and SQL]
[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java beginner] About initialization of multidimensional array
[Java] Contents of Collection interface and List interface
[Java] About Java 12 features
[Java] About arrays
interface and abstract
Something about java
Where about java
About Java features
Java Beginner Exercises
About Java threads
About Java class
Java and JavaScript
About Java arrays
XXE and Java
[Java] Functional interface
About java inheritance
Inheritance and interface.
About List [Java]
About java var
About Java literals
About Java commands
Java Exercise "Beginner"
[ev3 × Java] Interface, implementation and inheritance (event handling)
About Java data types (especially primitive types) and literals
Build Java environment and output hello world [Beginner]
Basics of threads and Callable in Java [Beginner]
About Java log output
Java, about 2D arrays
Commentary: About the interface
About [Java] [StreamAPI] allMatch ()
Getters and setters (Java)
About Java StringBuilder class
Callable Interface in Java
[Java] Thread and Runnable
java standard functional interface
Java true and false
[Java] About Singleton Class