[JAVA] Difference between abstract and interface

I was embarrassed that I couldn't answer the Java language specification at the interview, so I will relearn it.

About the relationship between interface and abstract

Expressed in various ways.

# interface abstract
Specifications Used when you want to define as a class specification Used when there is an inheritance relationship and you want to reuse the process
Relationship with implementation class or concrete class Implementation classCANAbstract function Concrete classISAbstract class

Code difference

interface

//abstract
public interface Cashier {
    void bill();
}

//Implementation class 1
public class CreditCart implements Cashier {
    public CreditCart(){
    }    
    @Override
    public void bill(){
        System.out.println("billed by credit card");
    }
}
//Implementation class 2
public class Cash implements Cashier {
    @Override
    public void bill() {
        System.out.println("billed by cash");
    }
}

abstract

//Abstract class
public abstract class Animal {
    String name;
    public Animal(String name) {
        this.name = name;
    }
    public void sleep() {
        System.out.println("Sleeping");
    }
    public abstract void speak();
}

//Implementation class 1
public class Human extends Animal {   
    public Human(String name) {
        super(name);
    }   
    @Override
    public void speak() {
        System.out.println(name + " speack human languages");
    }
}
//Implementation class 2
public class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }   
    @Override
    public void speak() {
        System.out.println(name + " speack cat's language");
    }
}

Qualifier scope

# Same class Same package Sublux all
default - -
private - - -
protected -
public

Recommended Posts

[JAVA] Difference between abstract and interface
Difference between interface and abstract class
[JAVA] What is the difference between interface and abstract? ?? ??
[Java] Difference between == and equals
[Java] Difference between Hashmap and HashTable
interface and abstract
[Java] Difference between array and ArrayList
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[For beginners] Difference between Java and Kotlin
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
Difference between vh and%
Difference between i ++ and ++ i
[Java] Understand the difference between List and Set
Difference between next () and nextLine () in Java Scanner
Understand the difference between abstract classes and interfaces!
[Java] Difference between "final variable" and "immutable object"
Advanced inheritance abstract, interface -java
Difference between product and variant
Difference between redirect_to and render
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
Difference between CUI and GUI
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
java Generics T and? Difference
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Java abstract methods and classes
Difference between render and redirect_to
[Java] Difference between static final and final in member variables
Use of Abstract Class and Interface properly in Java
What is the difference between Java EE and Jakarta EE?
[Java beginner] Difference between length and length () ~ I don't know ~
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between == operator and equals method
Differences between "beginner" Java and Kotlin
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
Difference between Java and JavaScript (how to find the average)
java (interface)
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[java] interface
[Java] Relationship between H2DB and JDBC
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between assignment of basic type variable and assignment of reference type variable