260 likes | 332 Views
Chapter 1. Introduction to Object-Oriented Programming and Software Development. Objectives. Understand the basic of object-oriented programming Differentiate classes and objects. Differentiate class and instance methods. Differentiate class and instance data values.
E N D
Chapter 1 Introduction to Object-Oriented Programming and Software Development
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
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.
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-worldobjects; properties and behavior of irrelevance will not be modeled in software objects.
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.
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)
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.
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;
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(萬物).
We use a rectangle to represent a class with its name appearing inside the rectangle. <Class Name> Account Motorcycle Example: 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 an object and place the underlined name of the object inside the rectangle. This is an object named SV198. <Object Name> Example: SV198 Graphical Representation of an Object
This notation indicates the class which the object is an instance. This tells an object SV198 is an instance of the BankAccount class. <Object Name> : <Class Name> Example: SV198 : BankAccount An Object with the Class Name
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 aclass 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 …).
Message deposit with the argument 250.00 is sent to a BankAccount object SV198. Rreceiver deposit 250.00 SV198 : BankAccount Sending a Message caller
Ask for the current balance of this particular account. getCurrentBalance() current balance SV198 : BankAccount The current balance of SV198 is returned. Sending a Message and Getting an Answer
Ask for the maximum possible speed for all MobileRobot objects is returned. MobileRobot maximum speed getMaximumSpeed() Calling a Class Method
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 itsproperties 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.
1304.98 908.55 354.00 SV098 : BankAccount SV211 : BankAccount SV129 : BankAccount current balance current balance current balance Sample Instance Data Value All three BankAccount objects possess the same current balance property. The actual dollar amounts (data value) are, of course, different.
BankAccount There is one copy of minimum balance for the whole class and shared by all instances. minimum balance This line is an instance-of relationship. 100.00 1304.98 908.55 354.00 SV098 : BankAccount SV211 : BankAccount SV129 : BankAccount current balance current balance current balance Sample Class Data Value
When the class icon is not shown, we include the class data value in the object icon itself. 908.55 SV129 : BankAccount minimum balance 100.00 current balance Object Icon with Class Data Value
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.
Account Checking Savings A Sample Inheritance • Here are the superclass Account and its subclasses Savings and Checking.
Student Graduate Law Resident Masters Doctoral Undergrad Commuting Inheritance Hierarchy • An example of inheritance hierarchy among different types of students.
Software Engineering • Much like building a skyscraper, we need a disciplined approach in developing complex software applications. • Software engineeringis the application of a systematic and disciplined approach to the development, testing, and maintenance of a program.
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