1 / 31

OBJECT ORIENTED PROGRAMMING

OBJECT ORIENTED PROGRAMMING. 2 nd Meeting Arief Setyanto. TOPIC COVERED. CLASS CONCEPT TURUNAN PEWARISAN KELAS ABSTRAK POLIMORPHISM INTERFACE PACKAGES. MEMBUAT CLASS sampel 1. Sampel 2 kelas biasa. public class TigaDimensi { private int pusat; protected int posx, posy,posz;

lew
Télécharger la présentation

OBJECT ORIENTED PROGRAMMING

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. OBJECT ORIENTED PROGRAMMING 2nd Meeting Arief Setyanto

  2. TOPIC COVERED • CLASS CONCEPT • TURUNAN • PEWARISAN • KELAS ABSTRAK • POLIMORPHISM • INTERFACE • PACKAGES

  3. MEMBUAT CLASS sampel 1

  4. Sampel 2 kelas biasa public class TigaDimensi { private int pusat; protected int posx, posy,posz; protected static double phi = 3.14; public TigaDimensi(int x, int y, int z) { this.pusat = 0; this.posx=x; this.posy=y; this.posz=z; } public TigaDimensi( ) { //overloading fungsi konstruktor this.pusat = 0; this.posx=0; this.posy=10; this.posz=10; } public void lihatposisi(){ System.out.print("x : ");System.out.println(posx); System.out.print("y : ");System.out.println(posy); System.out.print("z : ");System.out.println(posz); } public void geser(int jx,int jy,int jz){ this.posx = posx+jx; this.posy = posy+jy; this.posz = posz+jz; } public void geser(int jx){ //overloading fungsi geser this.posx = posx+jx; } public double volume(){ return 10.1; } }

  5. Kelas abstrak public abstract class TigaDimensi_A { private int pusat; protected int posx, posy,posz; protected static double phi = 3.14; public TigaDimensi(int x, int y, int z) { this.pusat = 0; this.posx=x; this.posy=y; this.posz=z; } public void lihatposisi(){ System.out.print("x : ");System.out.println(posx); System.out.print("y : ");System.out.println(posy); System.out.print("z : ");System.out.println(posz); } public void geser(int jx,int jy,int jz){ this.posx = posx+jx; this.posy = posy+jy; this.posz = posz+jz; } public abstract luas(); public double volume(){ return 10.1; } }

  6. Sekurang kurangnya punya 1 fungsi abstract

  7. Deklarasi kelas final public final class TigaDimensi _F{ private int pusat; protected int posx, posy,posz; protected static double phi = 3.14; public TigaDimensi(int x, int y, int z) { this.pusat = 0; this.posx=x; this.posy=y; this.posz=z; } public void lihatposisi(){ System.out.print("x : ");System.out.println(posx); System.out.print("y : ");System.out.println(posy); System.out.print("z : ");System.out.println(posz); } public void geser(int jx,int jy,int jz){ this.posx = posx+jx; this.posy = posy+jy; this.posz = posz+jz; } public double volume(){ return 10.1; } }

  8. Penjelasan tentang opsi class

  9. Kelas abstract, final vs biasa • Sebut saja kelas yang bukan abstrak atau final sebagai kelas biasa • Kelas abstrak adalah kelas yang tidak pernah bisa di wujudkan menjadi objek/instan – kelas ini wajib di turunkan. • Kelas final adalah kelas yang tidak boleh di turunkan • Kelas biasa adalah kelas yang bisa di deklarasikan menjadi obyek sekaligus boleh diturunkan

  10. Kapan kelas di bikin abstract • Pada saat ditemukan sebuah konsep yang tidak bisa ditemukan anggotanya sebagai sebuah obyek nyata. • Misal manusia, pada kenyataannya kita tidak mungkin menemukan sebuah obyek adalah manusia – keculai obyek tersebut adalah angota dari kelas pria atau kelas wanita. • Dalam kasus ini manusia adalah abstrak, pria dan wanita adalah kelas nyata yang merupakan turunan (himpunan bagian) dari manusia.

  11. Kapan kelas di buat final • Jika di temukan sebuah konsep tentang himpunan benda(obyek) yang tidak mungkin di dapatkan himpunan bagian darinya (sub class/turunan) . • Misalnya terdapat himpunan wanita, tidak ada satupun anggotanya yang punya atribut atau fungsi tambahan lagi maka wanita adalah kelas final • Prakteknya teknik ini digunakan untuk menghindarkan programmer menurunkan sebuah kelas yang tidak ingin/diperbolehkan mereka menurunkannya lagi

  12. Brief Detail • public • By default, a class can be used only by other classes in the same package. The public modifier declares that the class can be used by any class regardless of its package. Look in Creating and Using Packages for information about how to use modifiers to limit access to your classes and how it affects your access to other classes. • abstract • Declares that the class cannot be instantiated. For a discussion about when abstract classes are appropriate and how to write them, see Writing Abstract Classes and Methods . • final • Declares that the class cannot be subclassed. Writing Final Classes and Methods shows you how to use final and discusses the reasons for using it. • class NameOfClass • The class keyword indicates to the compiler that this is a class declaration and that the name of the class is NameOfClass. • extends Super • The extends clause identifies Super as the superclass of the class, thereby inserting the class within the class hierarchy. • How Do These Concepts Translate into Code? in the Object-Oriented Programming Concepts lesson, showed you a subclass of Applet and talked briefly about the responsibilities and benefits of subclasses. Managing Inheritance goes into further detail on this subject. • implements Interfaces • To declare that your class implements one or more interfaces, use the keyword implements followed by a comma-separated list of the names of the interfaces implemented by the class. How Do These Concepts Translate into Code? explained how the ClickMe applet implements an interface. Details about writing your own interfaces and how to use them can be found in Creating Interfaces .

  13. The Class Body • The class body contains all of the code that provides for the life cycle of the objects created from it: • constructors for initializing new objects, declarations for the variables that provide the state of the class • its objects, methods to implement the behavior of the class and its objects • in rare cases, a finalize method to provide for cleaning up an object after it has done its job. • Variables and methods collectively are called members.

  14. Deklarasi Method Sampel 1

  15. Deklarasi Method Sampel 2 public double volume(){ double v; v= (phi * jari2* jari2 * jari2 )/3 ; return v; }

  16. iIustrasi turunan

  17. PEWARISAN • What Members Does a Subclass Inherit? • Rule:  A subclass inherits all of the members in its superclass that are accessible to that subclass unless the subclass explicitly hides a member variable or overrides a method. Note that constructors are not members and are not inherited by subclasses. The following list itemizes the members that are inherited by a subclass: • Subclasses inherit those superclass members declared as public or protected. • Subclasses inherit those superclass members declared with no access specifier as long as the subclass is in the same package as the superclass. • Subclasses don't inherit a superclass's member if the subclass declares a member with the same name. In the case of member variables, the member variable in the subclass hides the one in the superclass. In the case of methods, the method in the subclass overrides the one in the superclass. • Creating a subclass can be as simple as including the extends clause in your class declaration. However, you usually have to make other provisions in your code when subclassing a class, such as overriding methods or providing implementations for abstract methods.

  18. CLASS TURUNAN class Super { Number aNumber; } class Subbie extends Super { Float aNumber; }

  19. Kelas Turunan public class Bola extends TigaDimensi{ public int jari2; public Bola(int x,int y,int z, int r){ super(x,y,z); posx=6; jari2=r; } public double volume(){ double v; v= (phi * jari2* jari2 * jari2 )/3 ; return v; } }

  20. Interface • In English, an interface is a device or a system that unrelated entities use to interact. According to this definition, a remote control is an interface between you and a television set, the English language is an interface between two people, and the protocol of behavior enforced in the military is the interface between people of different ranks. Within the Java programming language, an interface is a device that unrelated objects use to interact with each other. An interface is probably most analogous to a protocol (an agreed on behavior). In fact, other object-oriented languages have the functionality of interfaces, but they call their interfaces protocols. • The bicycle class and its class hierarchy defines what a bicycle can and cannot do in terms of its "bicycleness." But bicycles interact with the world on other terms. For example, a bicycle in a store could be managed by an inventory program. An inventory program doesn't care what class of items it manages as long as each item provides certain information, such as price and tracking number. Instead of forcing class relationships on otherwise unrelated items, the inventory program sets up a protocol of communication. This protocol comes in the form of a set of constant and method definitions contained within an interface. The inventory interface would define, but not implement, methods that set and get the retail price, assign a tracking number, and so on. • To work in the inventory program, the bicycle class must agree to this protocol by implementing the interface. When a class implements an interface, the class agrees to implement all the methods defined in the interface. Thus, the bicycle class would provide the implementations for the methods that set and get retail price, assign a tracking number, and so on. • You use an interface to define a protocol of behavior that can be implemented by any class anywhere in the class hierarchy. Interfaces are useful for the following: • Capturing similarities among unrelated classes without artificially forcing a class relationship. • Declaring methods that one or more classes are expected to implement. • Revealing an object's programming interface without revealing its class.

  21. Interface • interface • A Java(TM) programming language keyword used to define a collection of method definitions and constant values. It can later be implemented by classes that define this interface with the "implements" keyword.

  22. WHAT’S INTERFACE • An interface is a named collection of method definitions (without implementations). An interface can also declare constants.

  23. Perbedaan Interface dengan kelas abstract • An interface cannot implement any methods, whereas an abstract class can. • A class can implement many interfaces but can have only one superclass. • An interface is not part of the class hierarchy. Unrelated classes can implement the same interface.

  24. Deklarasi

  25. Example lets look at blueJ

  26. implements lets look to example

  27. Packages • A package is a collection of related classes and interfaces providing access protection and namespace management.

  28. Kenapa Bikin Packages • You and other programmers can easily determine that these classes and interfaces are related. • You and other programmers know where to find classes and interfaces that provide graphics-related functions. • The names of your classes wont conflict with class names in other packages, because the package creates a new namespace. • You can allow classes within the package to have unrestricted access to one another yet still restrict access for classes outside the package.

  29. Deklarasi package graphics; public class Circle extends Graphic implements Draggable { . . . } • The Circle class is a public member of the graphics package.

  30. menggunakan packages • Referring to a Package Member by Name graphics.Rectangle • To create an instance of graphics.Rectangle: graphics.Rectangle myRect = new graphics.Rectangle(); • Importing a Package Member • import graphics.Circle; • Refer to the Circle class by its simple name: • Circle myCircle = new Circle(); • Importing an Entire Package • import graphics.*; • Circle myCircle = new Circle();

  31. overloading

More Related