0% found this document useful (0 votes)
107 views

Inheritance Program in Java

The document contains code examples demonstrating inheritance in Java, C#, and C++. In Java, a vertebrata class is defined with methods for checking if an animal has a backbone. A mamalia class inherits from vertebrata and adds methods for checking if an animal nurses its young or its diet type. The code shows initializing an inheritance object and calling methods on it. In C#, a similar example is shown using classes and inheritance. In C++, a base class X is defined with methods to set and get a data member, and a derived class Y inherits from X and the methods can be called on a Y object.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Inheritance Program in Java

The document contains code examples demonstrating inheritance in Java, C#, and C++. In Java, a vertebrata class is defined with methods for checking if an animal has a backbone. A mamalia class inherits from vertebrata and adds methods for checking if an animal nurses its young or its diet type. The code shows initializing an inheritance object and calling methods on it. In C#, a similar example is shown using classes and inheritance. In C++, a base class X is defined with methods to set and get a data member, and a derived class Y inherits from X and the methods can be called on a Y object.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Contoh Program dalam Java package inheritance; class vertebrata { String status_vertebrata = "Hewan Bertulang Belakang"; public void

bertulang_belakang(String hewan, boolean status){ if(status == true){ System.out.println(hewan+ " " + status_vertebrata); } else{ status = false; } } } class mamalia extends vertebrata{ public void menyusui(String animal){ System.out.println(animal + "Merupakan Mamalia Karena Menyusui"); } public void carnivora(String animal, boolean status){ if(status==true){ System.out.println(animal + "Makan Daging"); } } public void herbivora(String animal, boolean status){ if(status==true){ System.out.println(animal + "Makan Tumbuhan"); } } public void omnivora(String animal, boolean status){ if(status==true){ System.out.println(animal + "Pemakan Segalanya"); } } } public class inheritance extends mamalia { public static void main(String[] args) { inheritance sapi = new inheritance(); sapi.bertulang_belakang("Sapi", true); sapi.menyusui("Sapi"); sapi.carnivora("Sapi", true); } }

OOP2_Inheritance_ShallMee

Contoh Program dalam C#


/* Created by shallmee */ using System; namespace inheritance { class vertebrata{ String status_vertebrata= "hewan bertulang belakang"; public void bertulang_belakang(String hewan, Boolean status){ if (status == true) { Console.WriteLine(hewan + " " + status_vertebrata); } else { status = false; } } } class mamalia : vertebrata{ public void menyusui (String animal){ Console.WriteLine(animal + "Merupakan Mamalia karena menyusui"); } public void carnivora (String animal, Boolean status){ if (status == true) { Console.WriteLine(animal + "Makan Daging"); } } public void herbivora (String animal, Boolean status){ if (status == true) { Console.WriteLine(animal + "Makan Tumbuhan"); } } public void omnivora (String animal, Boolean status){ if (status == true) { Console.WriteLine(animal + "Pemakan Segala"); } } } class Program { public class inheritance:mamalia{ public static void Main(string[] args) { inheritance sapi = new inheritance(); sapi.bertulang_belakang("Sapi", true); sapi.menyusui("Sapi"); sapi.herbivora("Sapi", true); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); } } } }

OOP2_Inheritance_ShallMee

Permisalan dalam C++ #include <iostream> using namespace std; class X { int data; public: void a(int arg) { data = arg; } int b() { return data; } }; class Y : public X { }; int main() { Y obj; obj.a(20); cout << obj.b() << endl; }

OOP2_Inheritance_ShallMee

You might also like