Thursday, 10 May 2012

Abstract Class di Java, C# dan C++

Abstract class adalah class yang tidak bisa dibuat secara instan sebagaimana class pada umumnya. Abstract class harus di implementasikan pada class turunan dari class Abstract tersebut. Abstract class berisi berisi sebuah method abstract dan method default yang bisa di override pada class turunan. Tidak semua bahasa pemrograman yang mendukung OOP mempunyai abstract class dan abstract method Seperti halnya c++. Maka dalam contoh code program Abstract class c++ harus menggunakan Class biasa dan virtual method agar bisa di override pada class turunannya.



Csharp


using System;

namespace OOPpraktikum
{
 class Program
 {
  static Implementasi imp = new Implementasi();
  public static void Main(string[] args)
  {
   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);
   
   Console.Write("\nPress any key to continue . . . ");
   Console.ReadKey(true);
  }
 }
 
 abstract class Abs // --- >Abstrak Class
 {
  public abstract void ShowKTP(string nama,string alamat,string tlp); // ---> Abstrak method
  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();
  }
 }
 
 class Implementasi : Abs
 {
  public override 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();
  }
 }
}  

Java


package OOPpraktikum;
import java.util.*;

public class MainClass 
{
 private static Scanner input = new Scanner(System.in);
 static Implementasi imp = new Implementasi();
 
 public static void main(String[] args) 
 {
  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);
  
  System.out.println("\nPress any key to continue . . . ");

 }

}

abstract class Abs // --- >Abstrak Class
{
 public abstract void ShowKTP(String nama,String alamat,String tlp); // ---> Abstrak method
 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();
 }
}

class Implementasi extends Abs
{
 @Override
 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();
 }
}  


C++


#include <cstdlib>
#include <iostream>

using namespace std;

class Abs
{
    public : virtual void ShowKTP(string nama,string alamat,string tlp) = 0; // ---> Abstrak method
 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;
 }
};

class Implementasi : public Abs
{
 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;
 }
};

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;
}
  


No comments:

Post a Comment