Interface di Java, C# dan C++


Interface adalah sebuah fitur OOP yang menampung method-method kosong. Interface harus diturunkan pada sebuah Class jika ingin di implementasikan. Tidak jauh berbeda dengan Abstract Class kegunaan dari interface, hanya saja interface tidak bisa menampung method default dan tidak mempunyai modifier.

Penggunaan interface dalam bahasa pmrograman Csharp, Java dan C++ berbeda. Jika pada bahasa pmrograman Csharp dan Java, penggunaan interface menggunakan keyword “interface”, tetapi dalam C++ menggunakan fitur Hider File.


Csharp


using System;

namespace OOPpraktikum
{
class Program : KTP,MHS
{
public void ShowKTP(string nama,string alamat,string tlp)
{
Console.WriteLine("KTP");
Console.WriteLine();
Console.WriteLine("Nama anda : {0}",nama);
Console.WriteLine("Alamat : {0}",alamat);
Console.WriteLine("No Telp : {0}",tlp);
Console.WriteLine();
Console.WriteLine();
}

public void ShowMHS(string nama,string nim,string prodi,string tlp)
{
Console.WriteLine("MHS");
Console.WriteLine();
Console.WriteLine("Nama anda : {0}",nama);
Console.WriteLine("NIM : {0}",nim);
Console.WriteLine("Prodi : {0}",prodi);
Console.WriteLine("No Telp : {0}",tlp);
Console.WriteLine();
Console.WriteLine();
}

static Program pro = new Program();

public static void Main(string[] args)
{
string nama = "Muhammad Rizal Afani";
string alamat = "Banyuwangi";
string telp = "303058";
string NIM = "1110100601";
string Jurusan = "Tehnik Informatika";

pro.ShowKTP(nama,alamat,telp);
pro.ShowMHS(nama,NIM,Jurusan,telp);

Console.Write("\nPress any key to continue . . . ");
Console.ReadKey(true);
}
}

interface KTP
{
void ShowKTP(string nama,string alamat,string tlp);
}

interface MHS
{
void ShowMHS(string nama,string nim,string prodi,string tlp);
}
}

Java


package OOPpraktikum;
import java.util.*;

public class MainClass implements KTP,MHS
{

public void ShowKTP(String nama,String alamat,String tlp)
{
System.out.println("KTP");
System.out.println();
System.out.println("Nama anda : "+nama);
System.out.println("Alamat : "+alamat);
System.out.println("No Telp : "+tlp);
System.out.println();
System.out.println();
}

public void ShowMHS(String nama,String nim,String prodi,String tlp)
{
System.out.println("MHS");
System.out.println();
System.out.println("Nama anda : "+nama);
System.out.println("NIM : "+nim);
System.out.println("Prodi : "+prodi);
System.out.println("No Telp : "+tlp);
System.out.println();
System.out.println();
}

private static Scanner input = new Scanner(System.in);
static MainClass pro = new MainClass();

public static void main(String[] args)
{
String nama = "Muhammad Rizal Afani";
String alamat = "Banyuwangi";
String telp = "303058";
String NIM = "1110100601";
String Jurusan = "Tehnik Informatika";

pro.ShowKTP(nama,alamat,telp);
pro.ShowMHS(nama,NIM,Jurusan,telp);

System.out.println("\nPress any key to continue . . . ");
}

}

interface KTP
{
void ShowKTP(String nama,String alamat,String tlp);
}

interface MHS
{
void ShowMHS(String nama,String nim,String prodi,String tlp);
}


C++

KTP.h
Class KTP
{
public : virtual void ShowKTP() = 0;
}



MHS.h
Class MHS
{
public : virtual void ShowMHS() = 0;
}

Main


#include <cstdlib>
#include <iostream>

using namespace std;

#include "MHS.h"
#include "KTP.h"

class Implementasi : public MHS,KTP
{
public : void ShowKTP(string nama, string alamat, string tlp)
{
cout<<"KTP"<<endl;
cout<<endl;
cout<<"Nama anda : "<<nama<<endl;
cout<<"Alamat : "<<alamat<<endl;
cout<<"No Telp : "<<tlp<<endl;
cout<<endl;
cout<<endl;
}

public : void ShowMHS(string nama,string nim,string prodi,string tlp)
{
cout<<"MHS"<<endl;
cout<<endl;
cout<<"Nama anda : "<<nama<<endl;
cout<<"NIM : "<<nim<<endl;
cout<<"Prodi : "<<prodi<<endl;
cout<<"No Telp : "<<tlp<<endl;
cout<<endl;
cout<<endl;
}
};

int main(int argc, char *argv[])
{
Implementasi imp;


string nama = "Muhammad Rizal Afani";
string alamat = "Banyuwangi";
string telp = "303058";
string NIM = "1110100601";
string Jurusan = "Tehnik Informatika";

imp.ShowKTP(nama,alamat,telp);
imp.ShowMHS(nama,NIM,Jurusan,telp);


cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}


Author:

Facebook Comment