Why OBJECTORIENTED PROGRAMMING Objectoriented software development 12242021 1
Why? OBJECT-ORIENTED PROGRAMMING Object-oriented software development 12/24/2021 1
What am I expected to know? From 1250, the following items represent a partial list of things with which you are expected to be comfortable. Please notify the instructor ASAP if you need any help. Syntax – braces, semicolons, naming conventions (camel casing/Pascal casing), other punctuation, and so forth Decision making logic such as if. . else; switch. . case Comments – both // and /* … */; UML Class Diagrams Repetition (loops) such as while, for Operators such as =, +, -, *, /, ++, --, +=, >, Methods: calling methods, writing new <, ==, !=, >=, <=, parentheses, and so forth methods; static vs. instance methods Console input and output; System. out and the Scanner class; Methods: parameters; return types and return values Data types: int, float, double, short, long, char, String, Boolean, and so forth; value types vs. reference types; Classes and objects; terminology such as constructor, getter, setter, to. String, public, private, static, final, etc. Assignment statements and arithmetic; problem solving skills Object-oriented software development Creating and using objects and their methods 12/24/2021 2
Object-oriented terminology Introduced here and covered in more detail throughout the course Object-oriented software development 12/24/2021 3
Object-Oriented Terms and Concepts Major terms and concepts to be discussed Modularization, Encapsulation, Abstraction Accessibility – data hiding (private, public) Polymorphism – understanding which of several meanings of a term is the one intended from the context in which it is used Inheritance – creating a new class that is a special case of an existing class (a Student is a specialized Person, a Checking. Account is a specialized Bank. Account, a BMW is a specialized Automobile, etc. ) Object-oriented software development 12/24/2021 4
Modularization, Encapsulation, Abstraction These concepts are related Modularization: organizing the logical components of a solution to a problem into classes representing the prominent nouns in the project Specifications. �Example: A Marketing Company may have Customers, Products, Suppliers, Sales. People, Managers, Accountants, etc. Encapsulation: putting all related components (variables and methods) into a single class so that the class is self-contained �Example: a car encapsulates its motor, transmission, steering, etc. into one unit that is self-contained and can be used as a unit Abstraction: once a class is built, one can treat it as a new data type and use objects of that class without having to know how they work inside �Example: we can drive a car without having to understand how an engine or transmission works or how to build a new car Object-oriented software development 12/24/2021 5
Standardization – following standards for documentation (see course documentation policies) naming (variables, files, methods, classes, etc. ) coding style and so forth saves time for others who may not have been the original developers but who are now maintaining or upgrading the product Object-oriented software development 12/24/2021 6
Data-Hiding Data-hiding (public, private, etc) Some information should be hidden for security, privacy, and similar reasons (credit card numbers, bank account ID and password, health records, etc. ) Other information should be hidden because the user �Does not need to know it �I don’t have to know how a transmission works or be able to build one to drive a car �I don’t have to understand how a television set converts an electronic signal received over the cable into a picture on the screen in order to enjoy watching a game �Should not access it �If the user was given uncontrolled access to some feature, the user might tinker with it unnecessarily and cause widespread problems �Problems may be intentional or unintentional Object-oriented software development 12/24/2021 7
Data Hiding Ideally, everything is private unless user really NEEDS access to it The user of the television �Needs access to the buttons on the remote control to select a channel, adjust the volume, turn TV off/on �Does not need access to the transistors, resistors, diodes, and capacitors and so forth inside the TV Good rule of thumb when designing a class – make all attributes private and only make a method public if the user of your class has a need to use it directly Object-oriented software development 12/24/2021 8
Polymorphism literally means “many forms” Polymorphism allows the use of the same term to mean different things, but it does so in such a way that we know what is meant unambiguously For example, the operations one must perform to draw a rectangle are different than the operations one must perform to draw a circle �We call both sets of operations by the same term: Draw �When we know the context (what we are drawing - circle or rectangle), we understand which operations are needed to Draw it Object-oriented software development 12/24/2021 9
Polymorphism Method-overloading is one type of polymorphism in Java Overloading methods allows you to have several methods with the same name but with different parameter lists The signature of the particular overload distinguishes it from the others with the same name �Reminder: a method’s signature consists of the method’s name and the number, types, and ordering of its parameters Object-oriented software development 12/24/2021 10
Polymorphism, continued Many different classes (data types) may have features with the same names but different meanings or implementations Length of a rectangle object may be a measure of distance for one side Length of sentence may be a count of words or letters Length of a lecture may be a measure of time (or boredom) Length of a book is a number of pages Polymorphism allows us to use the same names for different things as long as the context makes clear which meaning is intended Object-oriented software development 12/24/2021 11
Inheritance As one gains experience with object-oriented programming, he or she begins to accumulate a collection of classes he or she has created Once this set of classes becomes large enough, re-use of classes becomes possible When one needs a class, the set of existing classes may not contain exactly what we need, but it may contain a class that is close Rather than starting from scratch to create the class we need, we can start with the class that is close and only add the necessary features to get the one we need Object-oriented software development 12/24/2021 12
Inheritance allows us to take a general case and specialize it by adding new features without starting over If we have a Person class (with a name, address, ID, phone number, birth date, sex, etc. ) �We can create a Student class by adding features like Major, Class Schedule, GPA, etc. �We do not have to recreate the features the Person class already has (Name, birth date, sex, …) �The Student class inherits those from the Person class �We can derive an Employee class from Person by adding features unique to employees �We can derive Soldier class from Person by adding features that make a Soldier a unique type of Person Object-oriented software development 12/24/2021 13
Inheritance terminology If class D inherits from class B we may say B is the base class and D is derived from it D is a subclass of B B is a superclass of D If D inherits from B (every D object IS-A B object) Every object of type D is-a object of type B �Every Employee is-a Person �Every Student is-a Person �Every Soldier is-a Person However, the converse is not true. Not every object of type B is of type D �Some Persons are not Employees �Some Persons are not Students Object-oriented software development 12/24/2021 14
Classes and Objects A class is a blue-print, pattern, or design describing the attributes and functionality of every instance of the class An object is one unique instance created from that blue-print or design Many objects may be created from the same blue-print (class) Each object will have the same attributes and the same functionality (all cars have motors, seats, brakes, …) The values of the attributes allow the objects to differ Example, every Person object has a name attribute, but every person may have his/her own value for that attribute – everyone has a name, but not everyone has the same name Object-oriented software development 12/24/2021 15
Message passing The act of asking an object of a class to perform some action (by invoking one of its methods) is called passing a message to the object. We may also refer to this action as Invoking the method Calling a function For example, We may send a message to a Person object asking it to tell us its Name (by using a public getter method such as get. Name( )) We may send a message to a bank account object asking it to report its current balance (again by invoking a public method) Objects communicate with each other by sending messages (asking each other to take some specific action) Object-oriented software development 12/24/2021 16
State of an Object The set of values for all of the attributes of an object at a particular point in time describe its state An object’s state may change over time as other objects send it messages (by invoking its methods) that ask it to take some action(s) that change the values of one or more of its attributes A setter method is an example of a method designed to change an object’s state A constructor method initializes an object’s state when the object is first created Object-oriented software development 12/24/2021 17
Object-Oriented Programming in Java Recall: an object should encapsulate all of the data and all the functionality required for it to be self-contained for its purpose To create a Java class that encapsulates this material, what does it need to contain? Attributes (also called fields or variables) that hold the data the object needs to do its job Methods that use or manipulate the data in the fields to do the job �An object may have a few methods or it may have many methods depending on the range of capabilities objects of the class need Object-oriented software development 12/24/2021 18
Objects in Java Objects contain Attributes (fields/variables) - private Methods �Constructors – one or more methods, one of which is invoked to initialize the fields when an object is first created – must have same name as the class �Getters/Setters – public methods that give other objects controlled access to the private fields �Accessor Methods – methods that allow the user to retrieve some information (and/or manipulate it) from the object without changing the values of its attributes �Manipulator (Mutator) Methods – methods that allow the user to modify the value(s) of one or more fields Object-oriented software development 12/24/2021 19
Constructor Methods Constructors Each class has one or more Name of constructor is same as name of class Purpose is to give initial values to fields when an object is created �Done in any way that makes sense for the object �Example: Person kelly = new Person (“Kelly”, “Green”, 21); Object Type – Class name Object reference name Object-oriented software development Call to one of the constructors for Person 12/24/2021 20
Constructor Methods Class may have 1 or more constructors All have same name as class None return anything (not even void) Each must have unique signature �The name must be same for all �The number, type, and/or order of parameters must be unique for each different constructor �Examples �Person ( ) // Default constructor – no parameters �Person (String first, String last, int age) // Parameterized Object-oriented software development 12/24/2021 21
Constructors in UML (astah*) Private Attributes/Fields Two constructor methods Object-oriented software development 12/24/2021 22
Getter Methods A getter is typically a public method that retrieves the value of a private attribute (field) Allows user of object to retrieve a value without changing it; examples: �A student can see, but not change, his/her GPA �A bank’s depositor, can determine his/her balance but cannot change it (without making a deposit or withdrawal) �An employee can “see” his/her salary, but cannot change it Object-oriented software development 12/24/2021 23
Public Getter A private field may or may not have its own public getter If the user SHOULD be able to see the value of the corresponding attribute, it should have a getter (all of the examples above) If a field should be hidden from the user and should be only for internal use by the object, it SHOULD NOT have a getter (the user of a TV need not be able to get information such as the voltage across a certain circuit or the electrical resistance of a particular resistor) A person would not want a public getter for his/her credit card number Object-oriented software development 12/24/2021 24
Getters in UML Getter methods for the private fields Object-oriented software development 12/24/2021 25
Setter methods A Setter method is a public method that allows a user to change the value of a private attribute in a way controlled by the designer of the class A field should have a setter if the user should be allowed to change an attribute �A remote control may use a setter to change (set) the channel on a TV �A brake may be used to change the speed of a car A field should not have a setter if the user should not be allowed to change its value. Examples: �Bank balance �GPA �Salary Object-oriented software development 12/24/2021 26
Setters A setter method usually takes one or more parameters used to set the new field value but it returns nothing - it just changes the field value Setter methods Object-oriented software development 12/24/2021 27
Accessor Method An accessor method is a public method that does not change any field in the object but it allows one to use the values of those fields for some purpose It may use the values in the pay. Rate and hours. Worked fields (attributes) to calculate and return a weekly salary It may format the values of some of the fields for display by the driver program It may calculate GPA from credits and hours earned Each examples uses information without changing it Object-oriented software development 12/24/2021 28
Accessor Method in UML Object-oriented software development 12/24/2021 29
Manipulator (Mutator) Method A manipulator (mutator) method may change the value of one or more fields in some way controlled by the designer of the class In a Circle object, a manipulator/mutator may set the value of an area field with a calculation that uses a formula involving the radius (area = p * radius 2) In an Employee object, a mutator may calculate the value of the SSN tax field using the values of other fields in the object Object-oriented software development 12/24/2021 30
Java code … Three attributes/fields Object-oriented software development 12/24/2021 31
Using an object How many fields does the driver class have? How many class names are used? How many objects can you see? What constructors are used? What methods of the Person class are used? What getters/setters are used? What accessors are used? Object-oriented software development What mutator is used? 12/24/2021 32
- Slides: 32