Lecture 1 - OO-Basic (Compatibility Mode)
Lecture 1 - OO-Basic (Compatibility Mode)
Objectives
Upon completion of this section, you are expected to : 1. Explain the basic concepts of Object Oriented technology. 2. Understand the advantages of Object Oriented system development.
Contents
Chapter 1. What Object Orientation is Chapter 2. Basic Concepts of OO Technology Chapter 3. Advantages of OO Technology Chapter 4. Object Oriented Programming
In this chapter, you will learn ... 1.1 Object 1.2 Object Orientation
1-4
1.1 Object
Physical things (Car, Camera, etc.) OBJECTS Abstract things (Job, Weather, etc.)
1-5
1.1 Object
Conventional approach
1-6
Object
Easy modification !
Conventional approach
1-7
2-8
2.1 Object
Object name Data
Method
Jukebox Record disks Titles of music play music stop music accept a coin
A system consists of Objects. Objects consist of Data and Methods. The data and the methods in an object are strongly related.
2-9
2.1 Object
Encapsulation
Jukebox Record disks Titles of music play music stop music accept a coin Necessary data for this Object Necessary methods for this Object
Data and Methods are encapsulated as an object. We dont have to know the internal structure of the object.
2-10
2.1 Object
2.2 Class
Class name Data Method Car owner type color speed up slow down turn
Katsra's car
Ryo's car
Shuji's car
Keisuke's car
2-11
2.2 Class
Generalization/Specialization
Generalization -Super-class Vehicle
Truck
Aggregation
A class has or contains another class.
contains or is composed of
CAR Whole
SEAT Part
Computer
A computer has CPU, memory and disk.
CPU
2-13
Memory
2.2 Class
Disk
Subclass
Taxi customer carry customer
Peculiar data Peculiar method
Subclass
Truck cargo transport cargo
2-14
Override
employee_info name birthday age address calc_age(birthday) calc_salary(age)
inheritance
executive_info post calc_salary(age)
2-15
(Reference) Overload
employee_info name birthday age post address calc_age(birthday) calc_salary(age) calc_salary(age,post)
2-16
2.4 Instance
CLASS
Car owner type color speed up slow down turn Class is used as a mold to make an INSTANCE.
Private Data
2-17
2.4 Instance
2.5 Message
MESSAGE is an interface between Objects. When an Object asks another Object to do something, the first Object sends a MESSAGE to the second Object.
2-18
2.5 Message
Example of Message
MESSAGE is an interface between objects.
mike.go_to(Tengeru)
2-19
2.5 Message
2.6 Polymorphism
We use the same verb in different Objects to do different behaviors. (e.g. We use OPEN to open a window and a data file. The ways to open are different, but the verb is the same.) That is called POLYMORPHISM. Its an abstraction of Message.
2-20
2.6 Polymorphism
Example of Polymorphism
rio : Cat cry() attack() move() MIAOW
Scratch Jump
Kei:Master
hamu.cry()
hamu : Dog cry() attack() move() ku-chi : Parakeet cry() attack() move() PIYOPIYOP
Pick Fly
BOWWOW
Bite Run
The messages are the same cry(). But the behaviors are different.
2-21
2.6 Polymorphism
Summary
In this chapter, you have studied about ... Object, Encapsulation Class Generalization, Specialization, Aggregation Inheritance, Override Instance Message Polymorphism
2-22 2. Basic Concepts of OO Technology
Drill 2-1
Encapsulation
Encapsulate the following data items and methods into 2 groups and make 2 objects. Write your answer on the next page. Object name
Bank account Foreign exchange
Data
Customer_name Account_Number Yen Dollar Balance Exchange_rate
Method
Withdraw_cash Exchange_Dollar_to_TZS Exchange_TZS to_Dollar Deposit_cash
Object name
Example
Data
2-23 2. Basic Concepts of OO Technology
Methods
Drill 2-1
Answer
2-24
Drill 2-2
Inheritance Make a class hierarchy chart with the following words. You can use each word only once.
Class name Personal_info Data name address telephone_number birthday
2-26
Customer_info
Employee_info
Drill 2-3
Aggregation Make Object model of the following objects. Object Model of a Car Tire Car Engine Door
2-27
3. Advantages of OO Technology
In this chapter, you will study about ... 3.1 Problems in Current System Development 3.2 OO Technology Characteristics 3.3 OO System Development 3.4 What is OO Heading for ...
3-29
3. Advantages of OO Technology
Software Crisis
1) Inferior Quality
Specifications not fully met
2) Decreased Productivity
Prolonged period of development
3) Lower Maintainability
Difficult to maintain due to complexity in structure
3-30
Account
Int x, y getPassword(aN){ }
Account
Int x, y getPassword(aN){ } calcBalance(aN){ }
import java.applet.*; import java.awt.*; public class Account extends Applet{ int x, y; public void getPassword(aN) { } public static calcBalance(xxx) }
Cut Over
3-32
3-33
In this section, you will learn ... 4.1 OO Programming Languages 4.2 JAVA Language 4.3 Object Oriented Programming
4-35
Eiffel
Pure object oriented language developed by Interactive Software Engineering
C++
Extended language from C developed by AT & T Bell lab.
Java
C++ like object oriented language developed by Sun Microsystems. Portable, Network, Architecture independent.
4-36
4-37
4-38
4-39
4-40
Application
1) Example1. java
// A first program public class Example1 { public static void main(String argv[]){ int i; for(i=1;i<=10;i++){ System.out.println(i+":Welcome to JAVA!"); } } }
1) Example1.java A simple JAVA program is shown on the previous page. This is a stand-alone program that writes Welcome to JAVA! to standard output ten times. You should use a text editor such as the notepad to enter the source code. Note also that you need to use .java as an extension for all JAVA source codes. 2) Compiling javac is a command used to compile JAVA source programs. You need to enter this command at the DOS-window command line. When compilation of a program is completed normally, a file with the .class extension is generated. This is a JAVA binary class file. 3) JAVA Interpreter A JAVA interpreter is available to run this stand-alone JAVA application. Use the java command to run a class file specifying the class name to be loaded as an argument.
4-43
4-45
Applet
CLASS : java.applet.Applet
init(); start(); paint(); stop(); destroy();
inherits or overrides
4-46
1) // Hello World! This is a comment. import java.awt.*; 2) import java.applet.*; 3) public class Exercise2 extends Applet { public void paint( Graphics gc ) { 4) int x, y; x = 100; y = 400; gc.drawString( "Hello World ! ",125,95); 6) gc.drawString( "x + y = " + (x+y),125,115); } }
(5)
4-47
4-48
4-49
Applet
CLASS : java.applet.Applet
init(); start(); paint(); stop(); destroy();
inherits or overrides
4-50
1) // Hello World! This is a comment. import java.awt.*; 2) import java.applet.*; 3) public class Exercise2 extends Applet { public void paint( Graphics gc ) { 4) int x, y; x = 100; y = 400; gc.drawString( "Hello World ! ",125,95); 6) gc.drawString( "x + y = " + (x+y),125,115); } }
(5)
4-51
4-52
4.3.2 Class
1) 2) import packageName; [modifier] class ClassName [extends SuperclassName] { //declaration of instance variables [modifier] type variableName [=IntialValue]; //declaration of methods [modifier] type methodName(argType argName, ...) { //process description //... } }
3)
4)
4-53
4.3.2 Class
4.3.3 Packages
java
lang
String Thread
io
File
applet
Applet Audio Clip
net
URL Socket
awt
Graphics Component
4-54
4.3.3 Packages
Graphics Class
Shape Drawing Methods in the Graphics Class
Method draw3DRect() drawArc() drawLine() drawOval() drawPolygon() drawRect() drawRoundRect() fillRoundRect() fillArc() fillOval() fillPolygon() fillRect() fillRoundRect() Description Draws a highlighted, 3D rectangle Draws an arc Draws a line Draws a oval Draws a polygon Draws a rectangle Draws a rounded-corner rectangle Draws a filled,highlighted,3D rectangle Draws a filled arc Draws a filled oval Draws a filled polygon Draws a filled rectangle Draws a filled, rounded-corner rectangle
4-55
4.3.3 Packages
4.3.4 Instance
import java.awt.*; import java.applet.*;
: Example 2
public class Example2 extends Applet { Class Button theButton; public void init(){ theButton = new Button("Push me"); add(theButton); } } Constructor (a special method that has the same name as class)
Instance
4-56
4.3.4 Instance
4-57
4.3.4 Instance
4-58
4.3.4 Instance
4.3.5 Encapsulation
It is possible to achieve encapsulation by using the following modifiers.
Modifiers
public protected (default) private
4-59
4.3.5 Encapsulation
4.3.6 Polymorphism(1/3)
Employee public calc_salary( )
: Exercise5
4.3.6 Polymorphism(2/3)
2) Employee.java
public class Employee { public String calc_salary(int age) { return salary = + age*1000; }
: Exercise5
3) SalesPerson.java }
public class SalesPerson extends Employee { public String calc_salary(int numOfSales) { return salary = + (numOfSales*500 + 20000); } } 4) Executive.java public class Executive extends Employee { public String calc_salary(int age) { return salary = + age*10000; }
5) Exercise5.html }
<html> <head><title> Exercise5 </title></head> <applet code=Exercise5 width=400 height=200></applet> 4-61 </html> 4.3.6 Polymorphism
4.3.6 Polymorphism(3/3)
1) Exercise5.java
: Exercise5
Even though the same name method, calc_salary() is called, using a reference variable declared as being of the same class type, Employee, the processing of the method varies depending on the referenced instance. This is called polymorphism. 2) Employee.java Instances of this class have a method called calc_salary, which calculates salaries of regular employees based on their ages. Employee is the superclass of the SalesPerson and Executive classes. 3) SalesPerson.java This is a subclass of the Employee class. It overrides the calc_salary method and calculates the salary of employees in the Sales department based on their unit sales. 4) Executive.java This is a subclass of the Employee class. It overrides the calc_salary method and calculates salaries of executives based on their ages. 5) Exercise5.html Displays salaries of regular employees, employees in the Sales department and executives in a browser.
4-62 4.3.6 Polymorphism
4-63
4.3.7 Others
Definition
true or false 16-bit Unicode character 8-bit signed integer 16-bit signed integer 32-bit signed integer 64-bit signed integer 32-bit floating~point value 64-bit floating~point value
4.3.7 Others
4-65
4.3.7 Others
4-66
4.3.7 Others
4-68
4.3.7 Others
4-69
4.3.7 Others
4-71
4.3.7 Others