Difference between interface and abstract class

Difference between interface and abstract class

Abstract class

Abstract class can be enforced by adding abstract method, method can be created as usual ⇒ By inheriting, you can forcibly attach a subclass method. In other words, by giving the derived class the same method and forcibly creating the method You can create a class that gives you coercion

The team has a common tax rate method in the class A, which everyone uses. Regional taxes vary from region to region, so make a method with individual derived classes.

interface

⇒ A class that implements an interface can be put in the interface box. In other words, if you implement an interface, you can switch between A and B depending on the conditions.

Sample (interface)

public interface ControlPanelIf {
    void play();
    void pause();
    void stop();
    void forwardFast();
    void backwordFast();
}
The class that implements this is the following code.

Actually, it is often more complicated, but let's write it as a sample.

public class DvdDeck implements ControlPanelIf {
    @Override
    public void play() {
        System.out.println("DVD playback");
    }

    @Override
    public void stop() {
        System.out.println("DVD playback stop");
    }

    @Override
    public void pause() {
        System.out.println("DVD pause");
    }

    @Override
    public void forwardFast() {
        System.out.println("DVD fast forward");
    }

    @Override
    public void backwardFast() {
        System.out.println("DVD fast rewind");
    }
}
The class that uses this DVDDeck class is as follows.

public class DeckUser {
    public static void main(String[] args) {
        ControlPanelIf myDeck = createDeck(args[0]);

        myDeck.play();
        myDeck.forwardFast();
        myDeck.pause();
        myDeck.play();
        myDeck.backwardFast();
    }

    private static ControlPanelIf createDeck(String deckType) {

        ControlPanelIf deck;
        if(deckType.equals("DVD") {
            deck = new DvdDeck();
        } else if(deckType.equals("BluRay") {
            deck = new BluRayDeck();
        } else if(deckType.equals("HDD") {
            deck = new HddRayDeck();
        } else {
            deck = new DvdDeck();
        }
        return deck;
    }
}

Reference material https://www.slideshare.net/graminmakeall/java-43178044?qid=6a49c149-fcb0-4aa8-87d0-2e4e2c18362b&v=&b=&from_search=5

Recommended Posts

Difference between interface and abstract class
[JAVA] Difference between abstract and interface
abstract (abstract class) and interface (interface)
Difference between class and instance
Difference between instance method and class method
interface and abstract
Difference between instance variable and class variable
[JAVA] What is the difference between interface and abstract? ?? ??
Proper use of interface and abstract class
Difference between vh and%
Difference between i ++ and ++ i
Interface / abstract class / override
Understand the difference between abstract classes and interfaces!
Difference between redirect_to and render
[Java] Difference between == and equals
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 bundle and bundle install
Relationship between package and class
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
What is the difference between a class and a struct? ?? ??
Difference between Spring AOP and library proxy target class
Use of Abstract Class and Interface properly in Java
[Ruby] Difference between get and post
Difference between render method and redirect_to
Difference between == operator and equals method
[Java] Difference between Hashmap and HashTable
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
[Ruby] Difference between is_a? And instance_of?
Difference between == operator and eqals method
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 Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
Differences between Applet class and JApplet class
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
[Ruby] Maybe you don't really understand? [Difference between class and module]
Easy to understand the difference between Ruby instance method and class method.
Note: Difference between Ruby "p" and "puts"
Difference between final and Immutable in Java
[Memo] Difference between bundle install and update
Difference between Ruby instance variable and local variable