[Java / Swift] Comparison of Java Interface and Swift Protocol

What to write in this article

Comparison of Java ʻinterface and Swift Protocol`

Why I wrote this article

・ Basic information engineer exam preparation -Comparison of similarities between Java and Swift

Code example (Java interface)

main.java


public class Main {
    
    interface sampleInterface{
        String sampleFunction(String a, String b);
    }
    
    
    public static void main(String[] args) throws Exception {
    
    /*When implementing an interface in a class, write implements*/    
    class A implements sampleInterface{
        public String sampleFunction(String a, String b){
            return "「" + a + ""When"" + b + "Is the argument of sampleFunction defined in classA";
        }
    }
    
    class B implements sampleInterface{
        public String sampleFunction(String a, String b){
            return "「" + a + ""When"" + b + "Is the argument of sampleFunction defined in classB";
        }
    }
    
    A a = new A();
    B b = new B();
    
    System.out.println(a.sampleFunction("c","d"));
    System.out.println(b.sampleFunction("e","f"));

    }
}

Output result (Java)

"C" and "d" are the arguments of sampleFunction defined in classA
"E" and "f" are the arguments of sampleFunction defined in classB

Code example (Swift Protocol)

sample.swift


//protocol
protocol SampleProtocol{
    func sampleFunction(_ a:String, _ b:String) -> String
}

// class A
class A:SampleProtocol{
    func sampleFunction(_ a:String, _ b:String) -> String{
        return "「\(a)"When"\(b)Is the argument of sampleFunction defined in classA"
    }
}


// class B
class B:SampleProtocol{
    func sampleFunction(_ a:String, _ b:String) -> String{
        return "「\(a)"When"\(b)Is the argument of sampleFunction defined in classB"
    }
}


var a = A()
var b = B()

print(a.sampleFunction("c","d"))
print(b.sampleFunction("e","f"))

Output result (swift)

"C" and "d" are the arguments of sampleFunction defined in classA
"E" and "f" are the arguments of sampleFunction defined in classB

Recommended Posts

[Java / Swift] Comparison of Java Interface and Swift Protocol
[Java] Contents of Collection interface and List interface
[Java] String comparison and && and ||
Java and Swift comparison (3) Class implementation / Class inheritance / Class design
The comparison of enums is ==, and equals is good [Java]
Advantages and disadvantages of Java
Use of Abstract Class and Interface properly in Java
Equivalence comparison of Java wrapper classes and primitive types
[Java] Comparison method of character strings and comparison method using regular expressions
Same judgment / equal value judgment / comparison / order in Swift and Java
[Java] I thought about the merits and uses of "interface"
[Java] Speed comparison of string concatenation
About fastqc of Biocontainers and Java
[Java beginner] About abstraction and interface
[JAVA] Difference between abstract and interface
[Java] Judgment of identity and equivalence
[Swift] Protocol concept and definition method
[Java] Correct comparison of String type
Comparison of WEB application development with Rails and Java Servlet + JSP
java (interface)
[java] interface
Comparison of processing times based on awk, shell command, and Java
This and that of Swift corner Radius
Comparison of Android Handler Looper and runOnUiThread
After 3 months of Java and Spring training
[Java] Inheritance and structure of HttpServlet class
Thorough comparison of Android and iOS implementations
A Java engineer compared Swift, Kotlin, and Java.
[Java] Collection and StringBuilder operation method comparison
Proper use of interface and abstract class
[Java] Comparison of String type character strings
Comparison of JavaScript objects and Ruby classes
Summary of Java Math.random and import (Calendar)
Basics of java basics ② ~ if statement and switch statement ~
Discrimination of Enums in Java 7 and above
Java and Swift comparison (2) Basic type / arithmetic expression / control syntax / function definition
Comparison of thread implementation methods in Java and lambda expression description method
The story of low-level string comparison in Java
[ev3 × Java] Interface, implementation and inheritance (event handling)
[Java] Personal summary of classes and methods (basic)
[Swift vs Java] Let's understand static and final
Chrome59 Comparison of normal and headless mode operation
I compared the characteristics of Java and .NET
interface and abstract
JAVA: Realizes generation and scanning of various barcodes
[Java] About interface
Basics of threads and Callable in Java [Beginner]
[Java] Map comparison
Java framework comparison
[Java] Classification memo of compilation error and run-time error
Java and JavaScript
XXE and Java
[Java] Functional interface
About interface, java interface
[Java] Overview of Java
Inheritance and interface.
Java enables extraction of PDF text and images
ArrayList and the role of the interface seen from List
[Java] Types of comments and how to write them
Summary of ToString behavior with Java and Groovy annotations
Please note the division (division) of java kotlin Int and Int