Konstruktor :
- Nama method sama dengan nama class
- Tanpa void
- Otomatis muncul tanpa dipanggil hanya menggunakan inialisasi
Inheritance :
- Penurunan / pewarisan class
Polymorphism :
- Nama method sama dengan method lainnya
- Nama method berbeda dengan nama class
- Memiliki parameter berbeda
Pengertian selengkapnya bisa dibaca disini.
Contoh Program Pada Java
Main Program
package contoh_inheritance;
public class Contoh_inheritance {
public static void main(String[] args) {
matematika2 mtk = new matematika2();
mtk.perhitungan(5, 2);
mtk.perhitungan(5.5, 2.8);
mtk.pangkat(2, 2);
mtk.kurang(10, 2);
}
}
public class Contoh_inheritance {
public static void main(String[] args) {
matematika2 mtk = new matematika2();
mtk.perhitungan(5, 2);
mtk.perhitungan(5.5, 2.8);
mtk.pangkat(2, 2);
mtk.kurang(10, 2);
}
}
Class matematika
package contoh_inheritance;
public class matematika {
public void kurang(int a, int b)
{
System.out.println("Hasil Kurang = "+ (a-b));
}
}
public class matematika {
public void kurang(int a, int b)
{
System.out.println("Hasil Kurang = "+ (a-b));
}
}
Class matematika2
package contoh_inheritance;
import java.math.*;
public class matematika2 extends matematika { /*Inheritance*/
public matematika2() /*Konstruktor*/
{
System.out.println("UTS OOP2 - JOKO PRASETYO");
}
public void perhitungan(int a, int b) /*Pholymorphism*/
{
System.out.println("Hasil Tambah = "+ (a+b));
}
public void perhitungan(double a, double b) /*Pholymorphism*/
{
System.out.println("Hasil Kali = "+ (a*b));
}
public void pangkat(int a, int b)
{
int hasil=1; /*Menggunakan For*/
for (int i=1; i<=b; i++)
{
hasil=hasil*a;
}
System.out.println("Hasil Pangkat Dengan (for) = "+ hasil);
//System.out.println("Hasil Pangkat = "+ (Math.pow(a,b))); /*Math.pow*/
}
@Override
public void kurang(int a, int b)
{
System.out.println("Override Hasil Kurang menjadi 'Bagi' = "+ (a/b));
}
}
import java.math.*;
public class matematika2 extends matematika { /*Inheritance*/
public matematika2() /*Konstruktor*/
{
System.out.println("UTS OOP2 - JOKO PRASETYO");
}
public void perhitungan(int a, int b) /*Pholymorphism*/
{
System.out.println("Hasil Tambah = "+ (a+b));
}
public void perhitungan(double a, double b) /*Pholymorphism*/
{
System.out.println("Hasil Kali = "+ (a*b));
}
public void pangkat(int a, int b)
{
int hasil=1; /*Menggunakan For*/
for (int i=1; i<=b; i++)
{
hasil=hasil*a;
}
System.out.println("Hasil Pangkat Dengan (for) = "+ hasil);
//System.out.println("Hasil Pangkat = "+ (Math.pow(a,b))); /*Math.pow*/
}
@Override
public void kurang(int a, int b)
{
System.out.println("Override Hasil Kurang menjadi 'Bagi' = "+ (a/b));
}
}
Contoh Program Pada C#
Main Program
/*
* Created by SharpDevelop.
* User: Joko
* Date: 19/05/2015
* Time: 1:43
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace contoh_inheritance
{
class Program
{
public static void Main(string[] args)
{
matematika2 mtk = new matematika2();
mtk.perhitungan(5, 2);
mtk.perhitungan(5.5, 2.8);
mtk.pangkat(2, 2);
mtk.kurang(10, 2);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
* Created by SharpDevelop.
* User: Joko
* Date: 19/05/2015
* Time: 1:43
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace contoh_inheritance
{
class Program
{
public static void Main(string[] args)
{
matematika2 mtk = new matematika2();
mtk.perhitungan(5, 2);
mtk.perhitungan(5.5, 2.8);
mtk.pangkat(2, 2);
mtk.kurang(10, 2);
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
Class matematika
/*
* Created by SharpDevelop.
* User: Joko
* Date: 19/05/2015
* Time: 1:43
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace contoh_inheritance
{
/// <summary>
/// Description of matematika.
/// </summary>
public class matematika
{
public virtual void kurang(int a, int b)
{
Console.WriteLine("Hasil Kurang = "+ (a-b));
}
}
}
* Created by SharpDevelop.
* User: Joko
* Date: 19/05/2015
* Time: 1:43
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace contoh_inheritance
{
/// <summary>
/// Description of matematika.
/// </summary>
public class matematika
{
public virtual void kurang(int a, int b)
{
Console.WriteLine("Hasil Kurang = "+ (a-b));
}
}
}
Class matematika2
/*
* Created by SharpDevelop.
* User: Joko
* Date: 19/05/2015
* Time: 1:45
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace contoh_inheritance
{
/// <summary>
/// Description of matematika2.
/// </summary>
public class matematika2 : matematika //Inheritance
{
public matematika2() //Konstruktor
{
Console.WriteLine("UTS OOP2 - JOKO PRASETYO");
}
public void perhitungan(int a, int b) //Pholymorphism
{
Console.WriteLine("Hasil Tambah = "+ (a+b));
}
public void perhitungan(double a, double b) //Pholymorphism
{
Console.WriteLine("Hasil Kali = "+ (a*b));
}
public void pangkat(int a, int b)
{
int hasil = 1; //Menggunakan For
for (int i = 1; i <= b; i ++)
{
hasil = hasil * a;
}
Console.WriteLine("Hasil Pangkat Dengan (for) = "+ hasil);
//Console.WriteLine("Hasil Pangkat = "+ (Math.Pow(a,b))); //Menggunakan Math.pow
}
public override void kurang(int a, int b)
{
Console.WriteLine("Override Hasil Kurang menjadi 'Bagi' = "+ (a/b));
}
}
}
* Created by SharpDevelop.
* User: Joko
* Date: 19/05/2015
* Time: 1:45
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
namespace contoh_inheritance
{
/// <summary>
/// Description of matematika2.
/// </summary>
public class matematika2 : matematika //Inheritance
{
public matematika2() //Konstruktor
{
Console.WriteLine("UTS OOP2 - JOKO PRASETYO");
}
public void perhitungan(int a, int b) //Pholymorphism
{
Console.WriteLine("Hasil Tambah = "+ (a+b));
}
public void perhitungan(double a, double b) //Pholymorphism
{
Console.WriteLine("Hasil Kali = "+ (a*b));
}
public void pangkat(int a, int b)
{
int hasil = 1; //Menggunakan For
for (int i = 1; i <= b; i ++)
{
hasil = hasil * a;
}
Console.WriteLine("Hasil Pangkat Dengan (for) = "+ hasil);
//Console.WriteLine("Hasil Pangkat = "+ (Math.Pow(a,b))); //Menggunakan Math.pow
}
public override void kurang(int a, int b)
{
Console.WriteLine("Override Hasil Kurang menjadi 'Bagi' = "+ (a/b));
}
}
}
Contoh Program Pada C++
Source Code
#include <iostream>
#include <conio.h>
using namespace std;
class matematika
{
public:
virtual void kurang(int a, int b)
{
cout<<"Hasil Kurang = "<<a-b<<endl;
}
};
class matematika2 : matematika //Inheritance
{
public :
matematika2() //Konstruktor
{
cout<<"UTS OOP2 - JOKO PRASETYO"<<endl;
}
void perhitungan(int a, int b) //Pholymorphism
{
cout<<"Hasil Tambah "<<a+b<<endl;
}
void perhitungan(double a, double b) //Pholymorphism
{
cout<<"Hasil Kali = "<<a*b<<endl;
}
void pangkat(int a, int b)
{
int hasil = 1; //Menggunakan For
for (int i = 1; i <= b; i ++)
{
hasil = hasil * a;
}
cout<<"Hasil Pangkat Dengan (for) = "<<hasil<<endl;
}
void kurang(int a, int b) override
{
cout<<"Override Hasil Kurang menjadi 'Bagi' = "<<a/b<<endl;
}
};
int main()
{
matematika2 mtk;
mtk.perhitungan(5, 2);
mtk.perhitungan(5.5, 2.8);
mtk.pangkat(2, 2);
mtk.kurang(10, 2);
cin.get();
return 0;
};
#include <conio.h>
using namespace std;
class matematika
{
public:
virtual void kurang(int a, int b)
{
cout<<"Hasil Kurang = "<<a-b<<endl;
}
};
class matematika2 : matematika //Inheritance
{
public :
matematika2() //Konstruktor
{
cout<<"UTS OOP2 - JOKO PRASETYO"<<endl;
}
void perhitungan(int a, int b) //Pholymorphism
{
cout<<"Hasil Tambah "<<a+b<<endl;
}
void perhitungan(double a, double b) //Pholymorphism
{
cout<<"Hasil Kali = "<<a*b<<endl;
}
void pangkat(int a, int b)
{
int hasil = 1; //Menggunakan For
for (int i = 1; i <= b; i ++)
{
hasil = hasil * a;
}
cout<<"Hasil Pangkat Dengan (for) = "<<hasil<<endl;
}
void kurang(int a, int b) override
{
cout<<"Override Hasil Kurang menjadi 'Bagi' = "<<a/b<<endl;
}
};
int main()
{
matematika2 mtk;
mtk.perhitungan(5, 2);
mtk.perhitungan(5.5, 2.8);
mtk.pangkat(2, 2);
mtk.kurang(10, 2);
cin.get();
return 0;
};
No comments:
Post a Comment
Untuk pengunjung Prasetyo-Online diharapkan berkomentar dengan sopan dan ber-etika. Terimakasih atas kunjungannya, nantikan update info dan tutorial terbaru dari kami. Salam bloggers Indonesia.