1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2.

Slides:



Advertisements
Similar presentations
By Waqas Over the many years the people have studied software-development approaches to figure out which approaches are quickest, cheapest, most.
Advertisements

INHERITANCE BASICS Reusability is achieved by INHERITANCE
OBJECT-ORIENTED PROGRAMMING CONCEPTS (Review). What is an Object? What is an Object? Objects have states and behaviors. Example: A dog has states - color,
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter3: Introduction to Classes and Objects 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
What is an object? Your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior;
EEC-681/781 Distributed Computing Systems Java Tutorial Wenbing Zhao Cleveland State University
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-oriented Programming Concepts
Object Oriented Paradigm Programming Paradigms En Mohd Norafizal A.Aziz.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Pertemuan 8 The Object Definition Language (Lanjutan) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Object-Oriented Programming Concepts
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Chapter 4 Objects and Classes.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Object Oriented Programming Concepts. Object ► An object is a software bundle of related state and behavior. ► Software objects are often used to model.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Programming Paradigms Lecturer Hamza Azeem. What is PP ? Revision of Programming concepts learned in CPLB Learning how to perform “Object-Oriented Programming”
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
© 2007 Lawrenceville Press Slide 1 Chapter 8 Objects  A variable of a data type that is a class. Also called an instance of a class.  Stores data  Can.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
OOPS CONCEPT.  OOPS  Benefits of OOPs  OOPs Principles  Class  Object Objectives.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Object Oriented Programming in Java Habib Rostami Lecture 2.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Introduction to Object-oriented Programming
Object-Oriented Programming
Modern Programming Tools And Techniques-I
Object-Oriented Programming Concepts
JAVA By Waqas.
Inheritance ITI1121 Nour El Kadri.
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Object Oriented Analysis and Design
OOP’S Concepts in C#.Net
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Object-Oriented Programming
Final and Abstract Classes
ITE “A” GROUP 2 ENCAPSULATION.
CSG2H3 Object Oriented Programming
Presentation transcript:

1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2

2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mengimplementasikan konsep OOP dalam bahasa pemrograman Java Membuat program Java dengan konsep OOP

3 Outline Materi Introduction Encapsulation Inheritance Polymorphism Interface Package

4 Procedural versus OOP Procedural programming language –C is an example –Action-oriented –Functions are units of programming Object-oriented programming language –Java is an example –Object-oriented –Classes are units of programming Functions, or methods, are encapsulated in classes

5 OOP In Java Every java program implement all OOP Concept: –Encapsulation: all java program must reside in class, no class, no program –Inheritance: all class have a superclass, if not mentioned, it’s automatically subclassing from Object class –Polymorphism: all method are polymorphic in default

6 Encapsulation “Packaging an object’s variables within protective custody of its methods” Advantages: –Modularity –Information Hiding: object has a public interface for communicate to others, so it can maintain private information and method that can be changed any time without affecting the communication Java Keyword: class

7 OOP Concept: Object Real world objects share 2 characteristics: –State Dogs have state: name, color, breed, hungry Bicycles have state: current gear, current pedal cadence, two wheels, number of gears) –Behavior Dogs have behavior: barking, fetching, wagging tail) Bycyles have behavior: braking, accelarating, slowing down, changing gears

8 OOP Concept: Object (cont) Software objects are modeled after real- world objects in that they too have state and behavior Software object: –State  variables –Behavior  methods, that is a function (subroutine) –Methods and variables is associated with an object, that is reside in object

9 OOP Concept: Object (cont) A Software Object, that have state and behavior

10 OOP Concept: Object (cont) Bicycle modeled as a software object: –10 mph, 90 rpm, 5th known as instance variable because they contain the state for a particular object –changeGears, brake, changeCadence known as instance method, because they inspect or change the state of a particular bicycle instance (object)

11 OOP Concept: Message The bicycle is useful only when another object (you) interacts with it (pedal) Trough the interaction between objects, we can achieve higher-order functionality and more complex behavior Software object interact and communicate by sending message to another object

12 OOP Concept: Message Sometimes the receiver object need more information to do, this information called parameters You  the sender object YourBicycle  the receiver object ChangeGears  the message, the method to perform lowerGear  information from You to YourBicycle, the parameters needed by the method

13 OOP Concept: Class A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind Class = method+attributeThe Bicycle class

14 OOP Concept: Class

15 Creating class //Filename: Point2D.java public class Point2D{ int x, y; // member variable public Point2D() { x=0; y = 0;} public Point2D(int nx, int ny) { setPoint(nx, ny); } // setter method public setPoint(int nx, int ny) { x = nx; y = ny; } // continue class Point declaration // getter method int getX() { return x; } int getY() { return y; } public static void main(String[] args) { // create object p Point2D p = new Point2D(); p.setPoint(1, 5); System.out.println(“x: “, p.getX()): System.out.println(“y: “,p.getY()): } } // end of class declaration

16 Some Guidance for Creating class 1 file can contain >=1 class 1 file only can contains 1 public class Filename must be a public class name, beware of case sensitive, remember that Java is multiplatform Tips: It would be better to have every file for every class you created

17 Class Access Level SpecifierClassPackageSubclassWorld Private No specifier protected public

18 Methods No Default argument All parameter is passing by value –How can we passing by reference? Support function name overloading, ie: same name different function signature (type of argument, number of argument, order of argument)

19 Inheritance Reusability Top down: –Being more specific Bottom Up: –Find similarity Java Keyword: extends

20 Inheritance- Example public class Point3D extends Point2D { int z; public Point3D(int nx, int ny, int nz) { super(nx, ny); // called super class constructor z = nz; } int getZ() { return z; } void setZ(int nz) { z = nz; } public static void main(String[] args) { Point3D p = new Point3d(10, 10, 3); System.out.println(“(x, y, z): “+ p.getX() + “,” p.getY() + “,” + p.getZ()); }

21 Polymorphism Many shapes, 1 function behave differently according to object instance “Late binding”, bind to instance not type In default all Java methods are “polymorphic” Use “final” keyword to make “early binding”, non polymorphic

22 Polymorphism-Example //Filename: Point2D.java public class Point2D{ int x, y; // member variable public Point2D() { x=0; y = 0;} public Point2D(int nx, int ny) { setPoint(nx, ny); } // setter method public setPoint(int nx, int ny) { x = nx; y = ny; } // continue class Point deklaration // getter method int getX() { return x; } int getY() { return y; } // overide method from class Object public String toString() { return “x: “+x “, y: “+y; } } // end of class declaration

23 // Point3D.java public class Point3D extends Point2D { int z; public Point3D(int nx, int ny, int nz) { super(nx, ny); // called super class constructor z = nz; } int getZ() { return z; } void setZ(int nz) { z = nz; } // overide method from class Point2D public String toString() { return super.toString() + “, z: “+ z; }

24 public static void main(String[] args) { Point2D p1 = new Point2d(10, 10); Point2D p2 = new Point3d(10, 10, 3); printObject(p1); // x: 10, y: 10 printObject(p2);// x: 10, y: 10, z: 3 } static printObject(Object o) { System.out.println(“Object content: “+ o); // that is called o.toString() }

25 Interface As container of abstract method Similar to class, except that all method are abstract, just contain declaration. As the way of solved some problem that need Multiple Inheritance feature As call back function

26 Interface - Example // IUpdate.java public interface IUpdate { public void UpdateProgress(int nPercent); } // Child.java, that called from Main class public class Child { IUpdate u; public void addEventListerner(IUpdate iu) { u = iu; } public void run() { for (int i=1; i<=100; i++) u.updateProgress(i); }

27 // Main.java public class Main implements IUpdate { Child child; public Main() // contructor { child = new Child(); // connect to child object child.addEventListener(this); // run child proses, and updating progress child.run(); } // implement method declared in Iupdate interface public void UpdateProgres(int nPercent) { System.out.println(“Progess: “ +nPercent); } public static void main(String[] args() { new Main();} }

28 Packages “A package is a collection of related classes and interfaces providing access protection and namespace management. ” 1 package is 1 subfolder in file system To organize file in project or library Keyword: package name;

29 Packages - Example package com.binus.oop; public class TestPackage { public static void main(String[] args) { System.out.println(“Contoh penggunaan package”); } Catatan: –Program diatas diberi nama TestPackage.java –Program OOP.java terletak pada directory x:\WebProg\OOP\com\binus\oop\TestPackage.java –Setting classpath  SET CLASSPATH = %CLASSPATH%\; x:\WebProg\OOP –Compile : X:\webProg\OOP\com\binus\oop>javac TestPackage.java –Running: x:\>java com.binus.oop.TestPackage