You are on page 1of 36

Week 2 Introduction to Object Oriented Programming

TIA1013 : Introduction To Programming

Lecture Outlines
Object, Class, Message, Method & Attributes Object-Oriented Programming Characteristics

Learning Objectives
To describe fundamental concepts of OOP. To state the difference between objects
and classes To identify characteristics of OOP. To draw a basic of UML class diagram.

Introduction to OOP
Two popular programming design methods: Structured Programming (SP) Object-Oriented Programming (OOP)

Structured Programming(SP)
Other name: top-down, stepwise, modular Main problem will be divided into subproblem Analyze each sub-problem All sub-problem solutions will be combined to solves the main problem

Object Oriented Programming (OOP)

Designs a program as collections of interacting objects that work together to accomplish tasks. Model the world as it is. The main concepts OO:
Objects, Classes and Instances Attributes and operations (behaviour) Object State Message-passing and methods

Object Oriented Programming (OOP)


Identify object as foundation for problem solving Identify interaction method for each object Identify data/attribute for each object Identify operation for each data Object will combines data and operation in one unit An OOP program is a collection of objects that interacts each other

Objects
An Object is a thing, both tangible and intangible. Account, Vehicle, Employee, etc. Objects have state, behaviour and identity.
State: the condition of an object at any moment, affecting how it can behave Behaviour: what an object can do, how it can respond to events and stimuli Identity: each object is unique

Objects are categorized into classes Each individual object is an instance of a class

Class
To create an object inside the computer program, we must provide a definition for objectshow they behave and what kinds of information they maintain called a Class. Class - a category of objects that share the same: attributes, operations/methods Attribute keeping objects characteristics Operation represents objects behavior.

Attribute

Attribute- a named property of a class that describes a range of values that instances of the attribute might hold Attributes are the way classes encapsulate data Example: Class : Employee Attributes: employee_name, employee_id, position, department etc..

Operation

A behavior of an object Implemented in classes are methods Methods are identified and invoked by their signatures, including name, parameters, and return type Example:
Class : Employee Attributes: employee_name, employee_id, position, department, salary etc.. Operations: setEmployeeDetails calculateSalary printEmployeeDetails

Summary
Class Template (blue print) to define specific instances or objects Object Instantiation of a class Attributes Describes the object Behaviors/Operation specify what object can do Methods implement an objects behavior
Analogous to a function or procedure

Messages are sent to trigger methods


Procedure call from one object to the next

Class Declaration
Basic skeleton of Java class
/* Sample program: Displaying Welcome */ public class Greeting { public static void main (String[] args) { System.out.println(Welcome); } }

Java Anatomy
Class title Comments Reserved words Identifiers Statements Blocks Classes Methods The main method Class name

//printing welcome public class Greeting { public static void main(String[] args) { System.out.println(Welcome!"); block } } block Main method signature String

Class Declaration
The Java class declaration takes the following form:
<modifier>* class <class_name> { <attribute_declaration>* <method_declaration>* }

Class Declaration
public class Cd { class private String title; attributes private String no_of_stock; public void setData(){ title = Animal Planet; no_of_stock = 45; Method definition } public void print(){ System.out.println(This CD title is + title + and still available + no_of_stock + copies); } }

Graphical Representation of a Class


<Class Name> <list of attributes> Example: <list of operations>
The notation we used here is based on the industry standard notation called UML, which stands for Unified Modeling Language.

Graphical Representation of a Class


Cd

- title - no_of_stock + setData() + print()

Message and Method


To instruct a class or an object to perform a task, we send a message to it. You can send a message ONLY TO the classes and objects that understand the message you sent to them. A class or an object must possess a matching method to be able to handle the received message.

Sending Message..

Message print is sent to a Cd object myCd.

print()
myjCd : Cd

Creating Object
In Java, instance/object is created by using new keyword. Basic syntax: <class name> <object name> = new <class name> (parameters); Example:
Cd myCd = new Cd ();

Graphical Representation of an Object


<Object Name> We use a rectangle to represent an object and place the underlined name of the object inside the rectangle.

Example:
myCd This is an object named myCd.

Main method, Object, Accessing method


public class TestCd{ public static void main (String args[ ]) { Cd myCd = new Cd(); myCd.print(); }

}
Accessing print() method (sending message print to object myCd)

Sample of Complete Program


class Cd { private String title; private int no_of_stock; public void setData(){ title = Animal Planet; no_of_stock = 45; } void print() { System.out.println(This CD title is + title + and still available + no_of_stock + copies); } } public class TestCd { public static void main (String args[ ]){ Cd myCd = new Cd(); myCd.print(); } }

Characteristics of OOP
Abstraction Encapsulation Inheritance Polymorphism

Abstraction..
Abstraction is the process of finding the essential feature set for a class. Concentrating on what an object is and does before making any decision about how the object will be implemented. Capture the attributes and operation of a object/real-world deck. Object in a program is an abstraction of the real-world object. Example: myCd.

Encapsulation..
Encapsulation is the process of hiding object data and providing methods for data access (data and behavior are bundled into a class and hidden from the outside world) Process of hiding the implementation details. Each object of a class has its own set of instance fields. Instance fields are declared with the access modifier private, public or protected. Access to the data and behavior is provided and controlled through an objects interface Useful, because programmer can reuse the program code. Data, element, and method is being encapsulated in a class, and can be use by the other class.

Inheritance..
Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass, or base class The derived class is called the child class or subclass As the name implies, the child inherits characteristics of the parent A subclass inherits accessible data fields and methods from its super class, and may also add new data fields and methods.

Inheritance..
Super class (more general)

Subclass (more specialized)

Inheritance..
Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class
Vehicle

Car

Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent

Inheritance..
Class A

Class B

Class C

Class D

Class E

A is a superclass to B B is a subclass to A B is a superclass to C, D and E. C, D and E is a subclass to B

Inheritance..

Polymorphism..
The ability for different classes of objects to respond to identical messages in different ways
Polymorphism = having many forms Different behaviors for the same message

Polymorphism..
Can be overloading or overriding. Overloading methods appear in the same class have the same name but, have different parameter lists, and, can have different return types

Overriding methods overriding allows a subclass to re-define a method it inherits from it's superclass overriding methods: appear in subclasses have the same name as a superclass method have the same parameter list as a superclass method have the same return type as as a superclass method

Here, each type of vehicle has its own version of calcPrice()

Conclusion Q & A Session

You might also like