Chapter Chapter 1 Introduction to Object-Oriented Programming and Software Development
Chapter Objectives Understand the basic of object-oriented programming –Differentiate classes and objects. –Differentiate class and instance methods. –Differentiate class and instance data values. –Draw UML diagrams for classes and objects –Describe significance of inheritance in object-oriented programs Name and explain the stages of the software lifecycle
Chapter What is an Object ? Real-world objects: –Concrete objects: Apple1, Car1, TV2, Teacher2, Student3, … –Conceptual Objects: 1, 2.3, Date1, Meeting2, point2, … Objects have: –Properties (attributes): color, weight, height, sex, name, speed, position,… –Capabilities (behaviors): can receive commands(, request, query) and respond (do actions) based on its internal states to change its internal state and/or external environment. The properties of an object constitutes its current state.
Chapter What is a Software object ? a software bundle of data and functions used to model real-world objects you find in everyday life. In OOP, Software objects are building block of software systems –program is a collection of interacting objects –objects cooperate to complete a task –to do this, they communicate by sending “messages” to each other Software objects can model –tangible things: School, Car, Bicycle, –conceptual things: meeting, date –Processes: finding paths, sorting cards Note: Software objects are only abstraction of real-world objects; properties and behavior of irrelevance will not be modeled in software objects.
Chapter What is a Java Object? In Java, an object consists of 0 or more fields and 0 or more methods. –Fields are used to model properties. –Methods are used to model capabilities. Fields are variables. –like the fields of a C struct. –An object without methods is equivalent to a C struct. A method is similar to a C function. –Normally, it will operate on the fields of the object. –These fields are accessible by name in the method. Java variables can not hold objects, but only references to them. –Object do not have a names. –Object are created only at runtime.
Chapter Classes and Objects Current conception: –a java/software object a real-life object, –e.g., a Java car a real car Disadvantage: impractical to work with objects this way –may be indefinitely many (i.e., modeling all atoms in the universe) –do not want to describe each individual separately, because they may have much in common Classifying objects into classes of similar properties/behaviors –factors out commonality among sets of similar objects –lets us describe what is common once –then “stamp out” any number of copies later –Ex: Student: { S1, S2, S3 } Course:{C1,C2,C3} Teacher:{ T1,T2} – but not {s1, t1}, {s2, t2}, {c1,c2,c3,s3} Analog: –blueprint (class) –constructions (objects)
Chapter What is a Java Class? In Java, a class is a template (textual description) of a set of similar objects. –All objects in the class have the same types of properties and the same set of capabilities. It defines the fields and methods that all objects in that class will have. –Classes have names. –Class appear in the text of your program. –A java program consists of a set of classes. A defined class is a Java Type, so you can have objects or variables of that type.
Chapter Example class Person { // fields or properties int age ; String name ; Person father, mother ; Person[] : siblings // methods or behavior capability void eat( Food food) ; void play() ; void work() ; void sleep(); } //possible statements m1 = new Person(); p1 = new Person; p1.mother = m1;
Chapter Classes and Objects In OOP, a running application is results of participating objects and their interactions. Similar objects are described or defined by the same (set of ) classes –properties fields –behavior capability methods An object is called an instance of its defined classes. –Ex: stusent1 is an instance of both class Student and Person and Object( 萬物 ).
Chapter Graphical Representation of a Class The notation we used here is based on the industry standard notation called UML, which stands for Unified Modeling Language. We use a rectangle to represent a class with its name appearing inside the rectangle. Example: Account Motorcycle
Chapter Graphical Representation of an Object We use a rectangle to represent an object and place the underlined name of the object inside the rectangle. Example: SV198 This is an object named SV198.
Chapter An Object with the Class Name : This notation indicates the class which the object is an instance. This tells an object SV198 is an instance of the BankAccount class. Example: SV198 : BankAccount
Chapter Messages and Methods How do objects interact ? –message passing ; method requests/invocation/call To instruct a class or an object to perform a task, we –send a message (method call) to it. –the message must be understandable to the receiving classes or objects. i.e., –the receiving class or an object must possess a matching method to be able to handle the received message. Kinds of method: –A method defined for a class is called a class method, and –a method defined for all instances of a class is called an instance method. A associated value we pass to an object when sending a message is called an argument of the message. –e.g., p1.eat( apple1 ); // receiver.method ( argument …).
Chapter Sending a Message deposit Message deposit with the argument is sent to a BankAccount object SV198. SV198 : BankAccount Rreceiver caller
Chapter Sending a Message and Getting an Answer current balance getCurrentBalance() Ask for the current balance of this particular account. SV198 : BankAccount The current balance of SV198 is returned.
Chapter Calling a Class Method maximum speed MobileRobot getMaximumSpeed() Ask for the maximum possible speed for all MobileRobot objects is returned.
Chapter Class and Instance Data Values An object is composed of data values (for its properties) and methods. –Property has name, type and value –ex: int age = 10 ; –property name and type are static (time-invariant ) –property value may change with time. An instance data value is a value of one of its properties of an individual instance. –For example, each BankAccount object maintains its balance. A class data value is a value of some property of the whole class. –It is shared by all instances of the class. –Ex: minimum balance and average balance in Account class.
Chapter SV098 : BankAccountSV211 : BankAccountSV129 : BankAccount Sample Instance Data Value current balance All three BankAccount objects possess the same current balance property. The actual dollar amounts (data value) are, of course, different.
Chapter Sample Class Data Value SV098 : BankAccountSV211 : BankAccountSV129 : BankAccount current balance BankAccount minimum balance There is one copy of minimum balance for the whole class and shared by all instances. This line is an instance-of relationship.
Chapter Object Icon with Class Data Value When the class icon is not shown, we include the class data value in the object icon itself. SV129 : BankAccount current balance minimum balance
Chapter Class and object diagrams
Chapter Inheritance Inheritance is a mechanism in OOP to design two or more entities that are different but share many common features. –design/programming by difference. –Features common to all classes are defined in the superclass. –The classes that inherit common features from the superclass are called subclasses. We also call the superclass an ancestor and the subclass a descendant.
Chapter A Sample Inheritance Here are the superclass Account and its subclasses Savings and Checking. Account Checking Savings
Chapter Inheritance Hierarchy An example of inheritance hierarchy among different types of students. Student Graduate Undergrad Commuting Law Resident Masters Doctoral
Chapter Software Engineering Much like building a skyscraper, we need a disciplined approach in developing complex software applications. Software engineering is the application of a systematic and disciplined approach to the development, testing, and maintenance of a program.
Chapter Software Life Cycle The sequence of stages from conception to operation of a program is called software life cycle. Five stages are –Analysis –Design –Coding –Testing –Operation and Maintenance –Update and Evolution