1Inheritance in Java is a mechanism in which one object acquires
2all the properties and behaviors of a parent object.
3It is an important part of OOPs (Object Oriented programming system).
1// super class
2class Animal
3{
4 void eat()
5 {
6 System.out.println("Animal is eating.");
7 }
8}
9// subclass
10class Lion extends Animal
11{
12 public static void main(String[] args)
13 {
14 Lion obj = new Lion();
15 obj.eat();
16 }
17}
1// Multiple inheritance in java
2interface M
3{
4 public void helloWorld();
5}
6interface N
7{
8 public void helloWorld();
9}
10// implementing interfaces
11public class MultipleInheritanceExample implements M, N
12{
13 public void helloWorld()
14 {
15 System.out.println("helloWorld() method");
16 }
17 public static void main(String[] args)
18 {
19 MultipleInheritanceExample obj = new MultipleInheritanceExample();
20 obj.helloWorld();
21 }
22}
1public class Bicycle {
2
3 // the Bicycle class has three fields
4 public int cadence;
5 public int gear;
6 public int speed;
7
8 // the Bicycle class has one constructor
9 public Bicycle(int startCadence, int startSpeed, int startGear) {
10 gear = startGear;
11 cadence = startCadence;
12 speed = startSpeed;
13 }
14
15 // the Bicycle class has four methods
16 public void setCadence(int newValue) {
17 cadence = newValue;
18 }
19
20 public void setGear(int newValue) {
21 gear = newValue;
22 }
23
24 public void applyBrake(int decrement) {
25 speed -= decrement;
26 }
27
28 public void speedUp(int increment) {
29 speed += increment;
30 }
31
32}
33
1// single inheritance example
2import java.util.*;
3class Animal
4{
5 void eat()
6 {
7 System.out.println("Animal is eating.");
8 }
9}
10class Lion extends Animal
11{
12 void roar()
13 {
14 System.out.println("lion is roaring.");
15 }
16 public static void main(String[] args)
17 {
18 Lion obj = new Lion();
19 obj.eat();
20 obj.roar();
21 }
22}
1abstract class Pesan {
2 public void success() {
3 System.out.println("Mobil Berhasil Dibeli");
4 }
5
6 public void error() {
7 System.out.println("Uang Anda Tidak Cukup");
8 }
9}
10
11class Car extends Pesan {
12 protected String nama = "toyota supra";
13 protected String warna = "merah";
14 protected int harga = 2000000000;
15 protected String brand = "toyota";
16}
17
18class ShowRoom extends Car {
19 protected String namaShowroom = "Catur Sentosa Raya";
20 protected String alamatShowroom = "Jl.siliwangin kec pancoranmas kota depok 16436";
21}
22
23class Pembeli extends ShowRoom {
24 protected String namaPembeli = "anto jayabaya";
25 protected String alamatPembeli = "jl.swadaya rt.01/rw.04 no.112 kec pancoranmas kota depok";
26 protected int saldoPembeli = 50000000;
27}
28
29class BeliMobil extends Pembeli {
30
31 public BeliMobil(String nama, String warna, int harga, String brand, String nsr, String asr, String np, String ap, int sdp) {
32 super();
33
34 super.nama = nama;
35 super.warna = warna;
36 super.harga = harga;
37 super.brand = brand;
38 super.namaShowroom = nsr;
39 super.alamatShowroom = asr;
40 super.namaPembeli = np;
41 super.alamatPembeli = ap;
42 super.saldoPembeli = sdp;
43 }
44
45void getResult(String nama, String warna, int harga, String brand, String np, String ap) {
46
47 if(super.harga > super.saldoPembeli) {
48 System.out.println("=======================");
49 super.error();
50 System.out.println("=======================");
51 } else {
52 System.out.println("=======================");
53 super.success();
54 System.out.println("=======================");
55 System.out.println("");
56 System.out.println("=======================");
57 System.out.println("Jenis Mobil");
58 System.out.println("=======================");
59 System.out.println("");
60 System.out.println("Nama Mobil:" + nama);
61 System.out.println("Warna Mobil:" + warna);
62 System.out.println("Harga Mobil:" + harga);
63 System.out.println("Brand Mobil:" + brand);
64 System.out.println("");
65 System.out.println("=======================");
66 System.out.println("Nama Pembeli Mobil");
67 System.out.println("=======================");
68 System.out.println("Nama Pembeli:" + np);
69 System.out.println("Nama Pembeli:" + ap);
70 }
71 }
72
73 public static void main(String[] args) {
74 BeliMobil beli = new BeliMobil("avanza", "hitam", 128000000, "toyota", "Jaya Mobil", "Jakarta", "Anton", "Depok", 228000000);
75 beli.getResult(beli.nama, beli.warna, beli.harga, beli.brand, beli.namaPembeli, beli.alamatPembeli);
76 }
77}
78