MTS 2023 Object Oriented Programming

Introduction to Object Oriented Programming


An overview of OOP

OOP is a style programming that focuses on an application’s data and the methods you need to manipulate that data


The evolution of programming techniques

People have been writing computer programs since the 1940s The oldest programming languages required programmers to work with memory addresses and to memorize awkward codes associated with machine languages


Newer programming languages
   

more like natural languages Easier for programmers to use Allow programmers to name variable Allow to create self-contained modules / program segments that can be pieced together in a variety of ways Usually created by teams of programmers, each developing his own reusable & connectable program procedures

Writing several small modules is easier than writing 1 large program

Large tasks are easier when you break into units

2 major techniques to develop programs & procedures

Procedural programming
Focuses on the procedures that programmers create to manipulate data Focus on the action that are carried out. Eg getting input data for an employee Breaking down the process into manageable subtasks


Object oriented programming
Focuses on object and describes their features / attributes and behaviors.

Attributes of an object – the features it has, the value of an object’s attributes constitute the object’s state. Behavior of an object – the thing it does

Object oriented approach

Defining the object’s needed to accomplish a task and developing the objects so that each object maintains its own data and carries out tasks when another object request them.


Two major types of OOP 1. Computer simulations applications

Graphical User Interface (GUI)

Computer simulations
Example : A city might want to develop a program that provides a simulation of traffic patterns so as to better prevent traffic tie-ups. By creating a model with objects such as cars and pedestrians that each contain their own data and rules for behavior, the simulation can set into motion. So, a computer can create a simulation of a real city at rush hour

Graphical User Interface (GUI)

It is easy to think of the components a user manipulates on a computer screen such as buttons and scroll bars, as similar to real world objects Each GUI contains data – eg button with a specific size and color Each object contains behavior – eg button can be clicked


Understanding the programming process

Object oriented approach involves 3 separate tasks:



Analyzing the system using an object-oriented approach (object oriented analysis) Designing the system using an object-oriented approach (object oriented design) Writing the programs using an object-oriented approach (object oriented programming)


Writing a program involves:


3. 4. 5.

Identify all the objects you want to manipulate and how they relate to each other – data modeling Create a general category for the object – class and how they will communicate with each other Code the statements in programming languages Test the program Put the program into production


With OOP :

You analyze the objects you’re working with and the tasks that need to be performed with, and on, those objects You pass messages to objects, requesting the objects to take action The same message works differently when applied to different objects A module / procedure can work appropriately with different types of data it receives, without the need to write separate modules Objects can share or inherit traits of objects that have already been created, reducing the time it takes to create new objects Encapsulation and information hiding are emphasized.


OOP terminology

Object Oriented Programming (OOP) emphasizes the following concepts:

 

Class – describes a group or collection of objects with common properties Objects – an instance of a class Inheritance – process of acquiring the traits of one’s predecessors Polymorphism – multiple methods with the same name but act differently and appropriately when used with different types of objects. Encapsulation – process of combining all of an object’s attributes and methods into a single package. It exposes only the functional details, but hides the implementation details of the class.

Object Oriented Programs 

Implementation of all the collections of objects that were modeled in the analysis and design phase of the software development process. A real world problem consists of several collections of entities interacting with one another and with their surroundings When solving a real-world problem, a simplified representation of the problem is used to study the problem and construct the solution The representation – model of the problem


A model is composed of objects, each one representing a real-world entity The model includes descriptions about these objects and their interactions A task of designing and constructing a model is called modeling


Objects and Problem Solving

A simplification of problem solving process
Model of problem

OO approach for problem solving – all phases of the process are based on objects
Real-world objects

Algorithm & data design

Model of problem


Software objects


Part of the development process

Grouping similar real-world objects into collections of objects, modeling of these collections and software implementation of the corresponding classes. When the program executes, objects of these classes are created and made to interact among themselves


Collection of real-world objects
Collection A of objects

Model of class A

Class A

Model of class B
Collection B of objects

Class B

Real world

Model of problem

Software implementation



A problem is often too complex to deal with a single unit A general approach is to divide the problem into smaller problems that are easier to solve The partitioning of a problem into smaller parts is known as decomposition These small parts are called modules, which are easier to manage.

General structure of program
Attributes Operations Attributes Operations

Class A

Class B

Attributes Operations

Attributes Operations

Class C

Class D


Difference between Structured Programming Object Oriented Programming a subset structured Language objects areiscreated in of program, you use from OOP programming. After a
  

those objects and their methods to operate the program. In structured programming, you have a program with many methods in which you can use. One difference between structured programming and objectoriented programming is that structured programming uses the data that is given to them through parameters, while in objectoriented programming, the methods act upon the object's data (fields). This makes programming much easier because the fields are all there and you do not have to make sure that the correct field is passed to the correct method. All you have to do is call which field you want to work with.

What is an object?
 

Object is a thing (tangible/intangible) A program written in object oriented style will consist of interacting objects. Objects are given the responsibility of carrying out specific tasks of the solution Objects are models of the real-world entities identified in the real-world environment of the problem Objects with similar characteristics are grouped into collections called classes

What is an object?

Example :  A program to maintain bank accounts may have many Account, Customer, Transaction and ATM objects.  An Account object may consist of data such as account number, owner, date opened, initial balance etc.  Program also can has many objects of the same type. Two Customer objects with the names of Jack and Jill

Every object has:

State – represented by the set of properties or attributes and their associated values Behavior – represented by the operations, also known as method, of the object Identity – which is a property that can help identify an object identity
:Person name = “Siti” age = “20” play() stop()

state behavior


What is an object?
<Object Name> account 1 This is an Account object named account1

Customer Inside the object icon we indicate the type of an object.


Two Customer objects with the names Jack and Jill



    

In the real-world problem, a class describes a collection of real-world entities or objects with similar characteristics The abstract descriptions of the collections of objects are called classes (or class model) A class is a template to create objects. An object is called an instance of a class. An object is an instance of exactly one class. An instance of a class belongs to the class. Example : The two Customer objects Jack and Jill are instances of the Customer Class.


A class must be defined before you can create an instance (object) of the class. One class is defined, we can create as many objects of the class as a program requires. Every class defines:
 

Attributes (data declaration) One or more operations (functions or method)


A Customer class with two Customer objects

This is the Customer class



This line shows an instance of relationship; e.g Jill is an instance of Customer


Example of Classes and Objects
Class Objects Satria Neo, MyVi, Honda Civic, Toyota Vios Orchid, Rose, Bougainvillea, Morning Glory Circle, Square, Rectangle, Triangle





Java Programming Basics
 

An object-oriented program uses objects To use an object in a program, we first declare and create an object.


Object Declaration
  

Every object we use in a program must be declared. An object declaration designates the name of an object and the class to which the object belongs. Object declaration syntax: <class name> <object names>;

Example: Customer Jack, Jill;


Object Creation
 

No objects are actually created by the declaration. Example: Account account; account is used to refer to an Account object, but the actual Account object is not yet created. We create an object by invoking new operation. The syntax for new is <object name> = new <class name> (<arguments>);

 

<object name> <class name> <arguments>

The name of a declared object The name of the class to which the object belongs A sequences of values passed to the new operation


Distinction between object declaration and object creation
Account account; account = new Account ();

Account account; The identifier account is declared and space is allocated in memory account = new Account (); An Account object is created and the identifier account is set to refer to it.

State of memory


account Account

Example of object declaration and object creation
Customer profWu, drCafe; profWu = new Customer (); drCafe = profWu; Customer profWu, drCafe; profWu = new Customer (); drCafe = profWu;

profWu drCafe

profWu drCafe

Customer profWu, drCafe; profWu = new Customer (); drCafe = profWu;

profWu drCafe

Defining classes

A basic class definition takes the form:
class <classname> { [declaration;] [methods;] }


Class Definition Program Template
Import Statements Class Comment Describe the class in the javadoc format



Class Name Declarations Declare data members shared by multiple methods here, outside of method declarations

. . .



Defining classes

Example of a Java program (a class)
class hello { public static void main (String args[]) { System.out.println(“Hello, World!”); } }

The above code defines one class(hello) and one method (main()).

      

To instruct a class or an object to perform a task, we send a message to it. We cannot send a message to any class or object. We can send a message only to the classes and objects that understand the message. Class or object need a matching method to process the message it receives. Method is a sequence of instructions a class or an object follows to perform a task. A method defined for a class is called a class method, a method defined for an object is an instance method. Class method – for a task that pertains to all instances Instance method – for a task that pertains to an individual instance


Class, Object and Method
<Class Name> Class Method
The dotted line signifies that you cannot send instance messages to a class

<Object Name>
<Class Name>

Object Method


Defining Methods

A method in Java takes the basic form:
[modifier] return-type method-name (parameter list) { [statements;] }


Defining Methods

where specifies the scope(e.g.,public) return-type specifies what type of value the method will return. It can be a simple data type or a class type method-name an identifier Parameter-list contains the variables to be passed. The parameters must always be enclosed in parentheses even if the parameter list is empty.

 

Instance methods are declared WITHOUT the static modifier Class methods are declared WITH the static modifier


class Square{ private double width; private double height; Square(){ width = 0; height = 0; } public double wide(){ return width * height; } }

Method name Return type Access control


Coding Example


Example 1 : Program FunTime will allow you to draw a picture by 1. /* The program
2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

dragging a mouse. */ import javabook.*; class FunTime { public static void main (String args[ ]) { SketchPad doodleBoard; doodleBoard = new SketchPad();; } This statement makes doodleBoard } appear on the screen

Creates a new SketchPad object doodleBoard


Explanation – Example 1

The program consists of 2 classes:
 

FunTime : defined in the program SketchPad : predefined outside the program (in a place called the javabook package).

This program opens a SketchPad window named doodleBoard and makes it appear on the screen by sending the message show to it


Example 2 - GradeBook
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

// // Class declaration with one method public class GradeBook { //display a welcome message to the GradeBook public void displayMessage() { System.out.println(“Welcome to the Grade Book!”); } //end method displayMessage } //end class GradeBook


1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.

// Fig. 3.2 // Create a GradeBook object and call its displayMessage method public class GradeBookTest { // main method begins program execution public static void main (String args[]) { // create a GradeBook Object and assign it GradeBook myGradeBook = new Grade Book ( ) //call myGradeBook’s displayMessage method myGradeBook.displayMessage ( ); } // end main } // end class GradeBookTest

Use class instance creation expression to create object of class GradeBook

Call method displayMessage using GradeBook object

Welcome to the Grade Book!


Benefits of OOP
     

Reusability (reusable components) Reliability Robustness Extensibility Maintainability Reducing large problems to smaller, more manageable problems Better Analysis and Design of Complex Applications