Fungsi dari inheritance :
- Memudahkan pengembangan aplikasi.
Turunan tidak memiliki batasan, semuanya tetap dipanggil dikelas utama.
Overaide berfungsi untuk menimpa method dikelas lain.
Contoh Progam Inheritance Java :
Main.java :
package contoh_inheritance; public class Main { public static void main(String[] args) { matematika3 mtk = new matematika3(); matematika2 mtk2 = new matematika2(); mtk.kali(20, 20); //dari class matematika mtk.tambah(10, 20); //dari class matematika3 mtk2.tambah(10, 20);//dari class matematika2 mtk.kali(20.5, 20); //dari class matematika2 mtk.kurang(20, 10);//dari class matematika3/dirinya sendiri } }Matematika2.java :
package contoh_inheritance; import class_matematika.*; public class matematika2 extends matematika { public void tambah(int a, int b){ System.out.println("Hasil tambah "+(a+b)); } public void kali(double a, int b){ System.out.println("Hasil kali "+(a*b)); } }
Matematika3.java :
package contoh_inheritance; public class matematika3 extends matematika2{ public void kurang(int a, int b){ System.out.println("Hasil pengurangan "+(a-b)); } @Override public void kali(int a, int b){ System.out.println("Perkalian "+(a*b)); } @Override public void tambah(int a, int b){ System.out.println("Penambahan "+(a+b)); } }
Contoh Program Inheritance C++ :
Main.cpp :
#includeusing namespace std; class matematika1 { public: void kali(int a, int b) { cout<<" hasil kali =" <<(a*b) << endl; } }; class matematika2 : matematika1 { public: void tambah(int a, int b) { cout << "hasil tambah ="<< (a+b) << endl; } void kali (double a,int b) { cout << "hasil kali =" << (a*b) << endl; } }; class matematika3 : matematika2 { public: void kurang(int a,int b) { cout<<"hasil pengurangan ="<< (a-b) << endl; } virtual void kali(int a,int b) { cout << "hasil perkalian ="<<(a*b) << endl; } virtual void tambah(int a,int b) { cout << "hasil penambahan ="<<(a+b) << endl; } }; int main() { matematika3 x; x.kali(25,25); x.tambah(50,7); x.kali(10.0,20); x.kurang(100,75); x.kali(10,10); x.tambah(10,5); return 0; }
Contoh Program Csharp :
Program.cs :
using System; namespace inheritance { class Program { public static void Main(string[] args) { matematika3 mtk = new matematika3 (); mtk.kali(30,10); mtk.tambah(25,25); mtk.kali(20.0,30); mtk.kurang(100,25); mtk.kali(50,50); mtk.tambah(100,25); Console.ReadKey(true); } } }
class_matematika.cs :
namespace inheritance { public class matematika { void kali(int a, int b) { Console.WriteLine("hasil kali = " +(a*b)); } } }Class_matematika2.cs :
namespace inheritance { public class matematika2 : matematika { void tambah(int a, int b) { Console.WriteLine(" hasil tambah = " +(a+b)); } public void kali(double a, int b) { Console.WriteLine("hasil kali = "+(a*b)); } } }
Class_matematika3.cs :
namespace inheritance { public class matematika3 : matematika2 { public void kurang(int a ,int b) { Console.WriteLine("hasil kurang " +(a-b)); } public virtual void kali (int a,int b) { Console.WriteLine(" perkalian " +(a*b)); } public virtual void tambah(int a,int b) { Console.WriteLine(" penambahan "+(a+b)); } } }
Sekian Terima Kasih ....
0 komentar:
Posting Komentar