OBJECT ORIENTED CONCEPTS Chapter 2
Introduction In the real world, there are many individual objects all of the same kind. There may be thousands of cars, all of the same make and model. Each car was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your car is an instance of the class of objects known as cars. Many objects can be instantiated from a single class. Therefore a class is an abstract descriptor for a set of instances.
Class A class is the blueprint from which individual objects are created These objects have similar characteristics (common structure, common behaviour, common relationship and common semantics). Semantics is a set of rules that specify the meaning of syntactically legal construct in a programming language. Classes are sometimes referred to as entity classes or model class
Class For example, a Car class may be used to represent the common state (e.g., make, model, colour, mileage, etc...) and behaviour (e.g., start, stop, changeGear, turn etc...) between all car objects a 2DShape class may represent common state (e.g., centerPoint, lineWidth) and behaviour (e.g., calculateArea, calculatePerimeter) for 2D shapes.
Class CLASS : Car ATTRIBUTES Make Model Number of doors Engine size Another Example ATTRIBUTES Make Model Number of doors Engine size Colour METHODS Stop Turn Accelerate brake
TASK 1 List out the classes that you think are involve in the Student Information System?
CLASS Class table/notation is used to model the static structure of a system which is based on data it contains the name of class, attributes and methods Class name (singular) List of attributes List of methods
Class Class name A class Car represented in class notation. attributes make model doors bodyLength engineSize colour speed stop() turn() accelerate() brake() Class name A class Car represented in class notation. attributes methods
Implementation in Java The following codes implements a class car (it is just a blueprint that might be used in an application) : class car { String make = “ “; String model = “ “; int doors=0; int bodyLength = 0; int engineSize = 0; String colour = “ “; int speed = 0; void accelerate(int increment) { speed = speed + increment; } void brake(int decrement) { speed = speed - decrement; } }
Implementation in Java The following carDemo class will creates 2 car objects and invoke their methods: class carDemo { public static void main(String[] args) { // Create two different car objects car car1 = new car(); car car2 = new car(); // Invoke methods on those objects car1.accelerate(50); car2.brake(20) ; }
Objects Software objects are conceptually similar to real-world objects They too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages) attributes – they know things behavior (methods) - they do things
Objects object of the same type will have an identical set of attributes Attributes of one object may contain different data values from those of another object.
Objects Object can be considered as a container for a set of data and the operation that need to be performed on it. Properties are : It has a unique identity for the lifetime of the object Its data is in the form of a set of attributes, each has a value at any given point in time. A set of methods can be performed on the data It is an instance of a class
Objects Example - CLASS: car An instance (object) of the class car refers to a specific vehicle eg. Mercedes, Honda, BMW, Toyota
ATTRIBUTES Attributes are data items that define a class They are features/properties that help describes the system
TASK 2 List the attributes necessary for the class student
CLASS & OBJECTS Car BJ1132: car KN1123: car A Car class Car objects make model doors bodyLength engineSize colour speed stop() turn() accelerate() brake() BJ1132: car make=‘Ford’ model=‘Falcon’ doors=4 bodyLength=300 engineSize=6 colour=‘red’ speed=0 stop() turn() accelerate() brake() KN1123: car make=‘Toyota’ model=‘Corolla’ doors=5 bodyLength=200 engineSize=4 colour=‘white’ speed=100 stop() turn() accelerate() brake() A Car class Car objects Class Notation Object Notation/diagram
CLASS & OBJECTS There are 4 different types of methods/behaviour : Constructor methods – executed each time an instance of the class is created. Often used to initialise attributes. Mutator methods – methods which allow other objects to set or change the value(s) of an object attribute(s). This method begins with the word set e.g. setName. Accessor methods – methods which allow other objects to query the value(s) of a n object attribute(s). This method begins with the word get e.g.getName. Other methods – normally used to perform more complex operations e.g. calculation, validation
CLASS & OBJECTS Tips : To find class/objects/attributes, read the problem definition for nouns and noun phrases. (Not all nouns in the problem statement are vital to the solution) To identify methods, look at the verbs that describe the actions performed by the nouns
CLASS & OBJECTS Example: A program is required to process the employee pay. The employee data is read from a file. For each employee, compute the employee’s monthly pay and then print a report containing their detail together with their pay. The input file contains the employee number, pay rate, the number of hours worked and the overtime hours worked in a week. Assuming the overtime rate rate is 1.5 times the standard rate. Analyzed the above problem.
CLASS & OBJECTS EmployeePay empNumber payRate hoursWorked overtimeHrs Listing the attributes
CLASS & OBJECTS Example: A program is required to process the employee pay. The employee data is read from a file. For each employee, compute the employee’s monthly pay and then print a report containing their detail together with their pay. The input file contains the employee number, pay rate, the number of hours worked and the overtime hours worked in a week. Assuming the overtime rate rate is 1.5 times the standard rate. Analyzed the above problem.
CLASS & OBJECTS EmployeePay empNumber payRate hoursWorked overtimeHrs readData() printDetail() calcPay() printPay() Listing the methods