[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
[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 ()
Difference between final and Immutable in Java
[Java] Difference between Intstream range and rangeClosed
Difference between int and Integer in Java
Difference between vh and%
Difference between i ++ and ++ i
[Java] Difference between "final variable" and "immutable object"
Difference between class and instance
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
Use of Abstract Class and Interface properly in Java
[Java beginner] Difference between length and length () ~ I don't know ~
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
[Java] Check the difference between orElse and orElseGet with IntStream
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
Differences between Java and .NET Framework
[Java] Difference between Stack Overflow Error and Out Of Memory Error
Is short-circuit evaluation really fast? Difference between && and & in Java
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Java] What is the difference between form, entity and dto? [Bean]
[Java] Difference between Closeable and AutoCloseable
[Note] Cooperation between Java and DB (basic)
[Java / Swift] Comparison of Java Interface and Swift Protocol
Proper use of interface and abstract class
JAVA learning history abstract classes and methods
Difference between getText () and getAttribute () in Selenium
About the difference between irb and pry
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Arrylist and linked list difference in java
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
[Ruby] Difference between print, puts and p
[Java] Contents of Collection interface and List interface
HashMap # putAll () behaves differently between Java 7 and Java 8
[Java] About interface
Java and JavaScript
XXE and Java
[Java] Functional interface
About interface, java interface
[java] abstract class
Understand the difference between int and Integer and BigInteger in java and float and double
Inheritance and interface.
What is the difference between SimpleDateFormat and DateTimeFormatter? ??
Summarize the differences between C # and Java writing
Difference between member and collection of rails routes.rb
[Ruby] Difference between puts and return, output and return value
Distinguish between positive and negative numbers in Java
[Rails] Difference between create method and new + save method