• Tidak ada hasil yang ditemukan

JAVA PROGRAMMING II

N/A
N/A
Protected

Academic year: 2025

Membagikan "JAVA PROGRAMMING II"

Copied!
10
0
0

Teks penuh

(1)

JAVA PROGRAMMING II

CPCS 203

pol ymorhism.ppt 1

(2)

Interfaces

Java Programming

(3)

What is an Interface?

• An interface is a collection of abstract methods.

• A class implements an interface, thereby inheriting the abstract methods of the interface.

• Unless the class that implements the interface is abstract, all the methods of the interface

need to be defined in the class.

(4)

Similarity Between Interface & Class

• An interface can contain any number of methods.

• An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.

• The bytecode of an interface appears in a .class file.

• Interfaces appear in packages, and their corresponding bytecode file must be in a

directory structure that matches the package name.

(5)

Difference Between Interface & Class

• You cannot instantiate an interface.

• An interface does not contain any constructors.

• All of the methods in an interface are abstract.

• An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.

• An interface is not extended by a class; it is implemented by a class.

• An interface can extend multiple interfaces.

(6)

Remember!

• An interface is not a class.

• Writing an interface is similar to writing a class, but they are two different concepts.

• A class describes the attributes and behaviors of an object.

• An interface contains behaviors that a class

implements.

(7)

How to declare an Interface?

• Use the interface keyword

//A package declaration package package_name;

//Any number of import statements import java.lang.*;

public interface NameOfInterface {

//Any number of final, static fields

//Any number of abstract method declarations }

(8)

Properties of an Interface

An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface (although it is acceptable to use it).

Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. You can

explicitly declare a method in an interface as abstract, but typically the abstract keyword is left off.

Methods in an interface are implicitly public. It is common practice to use the public keyword when

writing an interface, but if you do not explicitly declare a method in an interface as public, it will be public

anyway.

(9)

Example of an Interface

• The MouseListener interface in the Java API.

• Defined in the java.awt.event package and contains five methods:

package java.awt.event;

public interface MouseListener extends java.util.EventListener {

public void mouseClicked(MouseEvent m);

public void mouseEntered(MouseEvent m);

public void mouseExited(MouseEvent m);

public void mousePressed(MouseEvent m);

public void mouseReleased(MouseEvent m);

}

(10)

Example of an Interface

Any class implementing MouseListener needs to implement all five of these methods.

Notice that the MouseListener interface has a parent interface named EventListener.

Any class that implements MouseListener must also implement the methods of EventListener; however, EventListener does not contain any methods:

package java.util;

public interface EventListener {}

Referensi

Dokumen terkait

public class Midlet05 extends MIDlet implements CommandListener { private Display display;. private Ticker ticker = new Ticker("Game Midlet"); private Alert alert =

Pada Java, semua code harus berada di dalam deklarasi sebuah class. Class menggunakan akses public , sehingga class Hello dapat diakses oleh class lain yang berada di luar

setelah interface kita buat DAO nya dengan nama class DaoMySql.java package bahrie.model; import bahrie.entity.Mahasiswa; import java.sql.Connection; import java.sql.DriverManager;

Untuk dapat menggunakan class-class yang ada di library Java, program Java harus meng-import dengan menspesifikasikan nama package dan classnya di bagian teratas

Restrictions on clone To allow clone to be used, a class has to implement Cloneable interface Marker interface clone in Object is protected OnlyEmployeeobjects canclone Redefine

H Computer Science, Semester II Lab Exercises BHCS03 – Programming in JAVA Attempt the following questions as part of JAVA Lab class today: 1.. Write a program that will count

H Computer Science, Semester II Lab Exercises BHCS03 – Programming in JAVA Attempt the following questions as part of JAVA Lab class today: 1.. Write a program that prompts the

poly + morphism • poly is Latin for “many”; morph is Latin for “form” • in Java, a method or constructor may have multiple signatures • this is called overloading • EXAMPLE: Cat c1