Interface
C
// Not Available
C++ (Cpp)
// C++ has no native interfaces.
// But you can implement it using abstract classes with "Pure Virtual" Methods
// An abstract class cannot be instantiated.
// Any class with one or more "pure virtual" functions becomes an abstract class
// Header File (MyInterface.h or MyInterface.hpp)
#pragma once
class MyInterface
{
public:
// "Pure Virtual" (Abstract) Method (To create a pure virtual function, we simply assign the function the value 0)
virtual float MyMethod() = 0; // You must override "Pure Virtual" (Abstract) methods in the derived class.
};
// If you want to use the "interface" keyword for a more organized workflow,
// you can use the Macro "define". (Still gonna be a class behind the curtain)
#define interface class
// Header File (MyInterface.h or MyInterface.hpp)
#pragma once
interface MyInterface
{
public:
virtual float MyMethod() = 0;
};
C# (Csharp)
// Classic Example
public interface IMyInterface // In C#, Its a convention to start the name with an I
{
// "Properties" can be declared but not initialized
string MyProperty { get; set; }
// Methods without body Must be overridden in the implemented class
string MyMethod(); // public by default
void MySecondMethod();
}
// Since C# 8.0
// Default Implementation
// Access modifiers (private, protected, internal, public and virtual, abstract, sealed)
// Static members (static methods, static fields, and static constructors)
public interface IMyInterface // In C#, Its a convention to start the name with an I
{
// public "Properties" can be declared but not initialized
public string MyProperty { get; set; }
// Private "property" not allowed
private static string MyStaticField = "Allowed"; // Doesnt need to be initialized
// Methods without body Must be overridden in the implemented class
public abstract string MyMethod(); // "public" and "abstract" by default
public virtual void MySecondMethod()
{
// Just add an implementation to make it a 'Default Method'
// Not mandatory to override
// By default, the default interface methods are virtual unless the sealed or private modifier is used
}
private void MyThirdMethod() // Private Members Must Have Default Implementation
{
// ...
}
// Static can be used directly Ex.: IMyInterface.myStaticMethod();
public static void myStaticMethod() // Static Members Must Have Default Implementation
{
// ...
}
private static void mySecondStaticMethod() // Static Members Must Have Default Implementation
{
// ...
}
// After C# 8.0, you should keep "public" in the declaration when public
}
// Implementation Class Example
class ImplementationClass : IMyInterface
{
// ...
}
More Info:
Java
// Classic Example
public interface MyInterface
{
// You can declare constants
String MY_CONSTANT = "Something"; // static and constant (final) by default
// Methods without body Must be overridden in the implemented class
public abstract String myMethod(); // "public" and "abstract" by default
void mySecondMethod(); // "public" and "abstract"
// After Java 8 and Java 9 changes, you should keep "public"
// and "abstract" in the declaration when public and/or abstract
}
// Since Java 8
// Static and defaults methods
public interface MyInterface
{
// You can declare constants
String MY_CONSTANT = "Something"; // static and constant (final) by default
public abstract String myMethod(); // No Body
// You dont need to override default methods
public default void mySecondMethod()
{
System.out.println("default method");
}
// Static can be used directly Ex.: MyInterface.myThirdMethod();
public static void myThirdMethod()
{
System.out.println("static method");
}
}
// Since Java 9
// Private methods
public interface MyInterface
{
// ...
// You can do all of the above examples plus:
// private methods can only be used inside the interface
// private static method can be used inside other static and non-static interface methods.
// private non-static methods cannot be used inside private static methods.
// "private abstract" not allowed
// "private default" not allowed
private void mySecondMethod() // private non-static method
{
System.out.println("private non-static method");
}
private static void myThirdMethod() // private static
{
System.out.println("private static method");
}
}
// Implementation Class Example
class ImplementationClass implements MyInterface
{
// ...
}
More Info:
Rust
// TODO
Go
// TODO
Javascript
// Not Available
// JavaScript inheritance is based on objects, not classes.
// JavaScript uses what's called duck typing.
// Duck typing is an application of the duck test: "If it walks like a duck and it quacks
// like a duck, then it must be a duck".
//
// To determine if an object can be used for a particular purpose. With normal typing,
// suitability is determined by an object's type.
//
// In duck typing, an object's suitability is determined by the presence of certain methods
// and properties, rather than the type of the object itself.
// But there is a workaround for Abstract Class that could be useful. Check that
// section for more information.
More Info:
Typescript
interface MyInterface
{
// "Properties" can be declared but not initialized
myProperty: string; // Must be defined in the implemented class/object
mySecondProperty?: string; // "Optional Property" - Not mandatory do define in the implemented class/object
readonly myThirdProperty: number; // After its first assignement, it cannot be changed
// Methods without body Must be overridden in the implemented class/object
MyMethod(): string;
MySecondMethod(someParameter: number); // names of the parameters do not need to match
// "private" access modifier not possible in Typescript Intarfaces
}
// Implementation Class Example
class ImplementationClass implements MyInterface
{
// ...
}
More Info:
Python
# Python doesnt have native Interface
# Python uses what's called duck typing.
# Duck typing is an application of the duck test: "If it walks like a duck and it quacks
# like a duck, then it must be a duck".
#
# To determine if an object can be used for a particular purpose. With normal typing,
# suitability is determined by an object's type.
#
# In duck typing, an object's suitability is determined by the presence of certain methods
# and properties, rather than the type of the object itself.
# Python also allow multiple inheritance
# But still, Python has some libraries that helps defining an Interface
# "python-interface" Example
from interface import Interface
# There are no "{ }", you need to use ":" and indentation
# You can have either spaces or tabs but not both mixed
class MyInterface(Interface):
@property
def my_property(self):
pass
def my_method(self):
pass
def my_second_method(self, some_value):
pass
@interface.default # Not mandatory to override
def my_default_method(self):
print("Default Implementation")
# Implementation Class Example
# "python-interface" Example
from interface import implements
class MyClass(implements(MyInterface)):
# ...
# Multiple interface allowed
class MyClass(implements(MyInterface, MySecondInterface)):
# ...
More Info: