120 likes | 317 Views
Pemrograman Berorientasi Objek. Bab 5 – Package & Interface. Package. A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer.
E N D
Pemrograman Berorientasi Objek Bab 5 – Package & Interface
Package • A package is a namespace that organizes a set of related classes and interfaces. • Conceptually you can think of packages as being similar to different folders on your computer. • You might keep HTML pages in one folder, images in another, and scripts or applications in yet another.
Purpose • Organization. • Because software written in the programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.
Interface • is a class like constructs that only contains constants and abstract methods. • Purpose: define constants and abstract methods (that should be implemented by class) that can be used by several class. • So interface is like contract between class and interface.
Constant and Method in Interface • Each constant in an interface is public static final by default, so we don’t need to write it. • Each method in an interface is public abstract by default.
Interface Rule • Can’t be instantiate. • Can be used as data type. • Implemented not inherited by class. • Can be inherited by other interface. • A class can implement many interfaces not like inheritance (a class only inherits a class). • Each method in interface MUST be implemented by class that implement the interface.
Warning • Since a class can inherit multiple interface, beware of name collision. • Name collision is same name (for constant or method) in different interface but implemented by a class. • Try to avoid it.
How To? • <modifier> interface <Name> {} • Show sample!!