oop concept 1 abstraction oop concept 2 encapsulation oop...

40

Upload: buinhu

Post on 15-Aug-2019

251 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 2: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

OOP Concept 1 Abstraction

OOP Concept 2 Encapsulation

OOP Concept 3 Inheritance

Constructor

OOP Concept 4 Polymorphism

Page 3: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

The way we look at a system in a simpler form, that is as a collection of subsystems (object) that interact with each other. For Example:

Car is a collection of ignition system, steering system, braking system

Tool to abstract something is class.

Object is modularity. Object can be written and maintained separately(independently) from another object.

Page 4: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 5: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 6: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

Mechanism to hide a process in the system to avoid

interference and simplify the use of the system itself. For Example:

Transmission rod (gear) on the car.

The on / off / on the air conditioning temperature setting.

Class access levels (public, protected, private) is the implementation of the concept of encapsulation.

Page 7: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

Sumber:http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html Contoh lain: Abdul kadir Dasar Pemrograman Java 2 Hal 149-154 dan 177-179

Page 8: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 9: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 10: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 11: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

A class can inherit attributes and methods in another

class (subclass), and forming class hierarchy.

Consist of:

Superclass (parent)

Subclass (child)

Important for reusability.

Java keyword: extends.

Page 12: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

A subclass inherits all the members

(fields, methods, and nested classes) from its superclass. Constructors are not

members, but the constructor of the superclass can be invoked from the subclass.

Page 13: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 14: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 15: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 16: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 17: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

• MountainBike inherits all the fields and methods of

Bicycle and adds the field seatHeight and a method to set it.

Page 18: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

Method used for the initial value at the

time the object was created.

Called automatically when the keyword "new" is used to make instant class

Character of the constructor:

Constructor name same with name of the class

Has no return value and there should be no void keyword.

Page 19: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 20: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 21: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 22: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

your method overrides superclass's methods

invoke the overridden super

Page 23: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 24: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

The inherited fields can be used directly, just like

any other fields.

You can declare a field in the subclass with the same name as the one in the superclass, thus

hiding it (not recommended).

You can declare new fields in the subclass that are not in the superclass.

The inherited methods can be used directly as

they are.

You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.

Page 25: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

You can write a new static method in the

subclass that has the same signature as the one in the superclass, thus hiding it.

You can declare new methods in the subclass

that are not in the superclass.

You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.

Page 26: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

The dictionary definition of polymorphism refers to a

principle in biology in which an organism or species can have many different forms or stages.

Page 27: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 28: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

This principle can also be applied to object-oriented

programming and languages like the Java language.

Subclasses of a class can define their own unique

behaviors and but share some of the same functionality of the parent class.

In shorthand “an object can have various shapes”.

Implementation of the concept of polymorphism:

Overloading: The use of one name for several method with different parameter.

Overriding: Occurs when a subclass method declaration exactly same as the method of the superclass.

Page 29: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 30: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 31: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 32: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 33: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 34: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 35: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 36: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 37: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 38: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 39: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to
Page 40: OOP Concept 1 Abstraction OOP Concept 2 Encapsulation OOP ...dinus.ac.id/repository/docs/ajar/5-Object-Oriented_Programming_Concepts(2).pdf · This principle can also be applied to

Contact: [email protected]