0% found this document useful (0 votes)
325 views3 pages

Contoh Coding

The document contains examples of inheritance, exceptions, and polymorphism in Java. The inheritance example shows a Car class extending the Vehicle class and overriding the honk() method. The exceptions example uses a try-catch block to catch an ArrayIndexOutOfBoundsException when looping through an array. The polymorphism example is not shown.

Uploaded by

Ram Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
325 views3 pages

Contoh Coding

The document contains examples of inheritance, exceptions, and polymorphism in Java. The inheritance example shows a Car class extending the Vehicle class and overriding the honk() method. The exceptions example uses a try-catch block to catch an ArrayIndexOutOfBoundsException when looping through an array. The polymorphism example is not shown.

Uploaded by

Ram Sai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Fatin Nur Farisha J33STM18F026

Inheritance (Perwarisan)
Contoh

class Vehicle {
protected String brand = "Ford";
public void honk() {
System.out.println("Tuut, tuut!");
}
}

class Car extends Vehicle {


private String modelName = "Mustang";
public static void main(String[] args) {
Car myFastCar = new Car();
myFastCar.honk();
System.out.println(myFastCar.brand + " " +
myFastCar.modelName);
}
}
Output
 Tuut, tuut!
Ford Mustang
Fatin Nur Farisha J33STM18F026

EXCEPTION
Contoh

class Exceptions {
public static void main(String[] args) {
String languages[] = { "C", "C++", "Java", "Perl", "Python" };
try {
for (int c = 1; c <= 5; c++) {
System.out.println(languages[c]);
}
}
catch (Exception e) {
System.out.println(e);
}
}
}

Output
1. C++
2. Java
3. Perl
4. Python
5. java.lang.ArrayIndexOutOfBoundsException: 5
Fatin Nur Farisha J33STM18F026

POLIMORFISME
Contoh

You might also like