Object-Oriented Programming

Object-Oriented Programming

Why? OBJECT-ORIENTED PROGRAMMING Object-oriented software development 02/26/2020 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 =, +, -, *, /, + +, --, +=, >, <, ==, !=, >=, <=, parentheses, and so forth Methods: calling methods, writing new 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 Object-oriented software development types vs. reference types; Classes and objects; terminology such as constructor, getter, setter, toString, public, private, 2 02/26/2020 static, final, etc. Object-oriented terminology Introduced here and covered in more detail throughout the course Object-oriented software development

02/26/2020 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 CheckingAccount is a specialized BankAccount, a BMW is a specialized Automobile, etc.) Object-oriented software development

02/26/2020 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, SalesPeople, 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 02/26/2020 5 Standardization 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 02/26/2020

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 dont have to know how a transmission works or be able to build one to drive a car I dont 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 02/26/2020 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 02/26/2020 8 Polymorphism 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

02/26/2020 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 methods signature consists of the methods name and the number, types, and ordering of its parameters Object-oriented software development 02/26/2020

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

02/26/2020 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

02/26/2020 12 Inheritance 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 02/26/2020 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 02/26/2020 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 02/26/2020 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 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 getName( ))

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 02/26/2020 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 objects 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 objects state A constructor method initializes an objects state when the object is first created Object-oriented software development 02/26/2020 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 02/26/2020 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 02/26/2020 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 02/26/2020 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 02/26/2020 21 Constructors in UML (astah*) Private Attributes/Fields

Two constructor methods Object-oriented software development 02/26/2020 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 banks 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 02/26/2020 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 02/26/2020 24 Getters in UML Getter methods for the private fields Object-oriented software development 02/26/2020 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 02/26/2020 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 method s Object-oriented software development 02/26/2020 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 payRate and hoursWorked 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 02/26/2020 28 Accessor Method in UML Object-oriented software development 02/26/2020 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 * radius2) 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 02/26/2020 30 Java code

Three attributes/fie lds Object-oriented software development 02/26/2020 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 mutator is used? Object-oriented software development What accessors are used? 02/26/2020 32

Recently Viewed Presentations

  • The Duties of the Iowa Assessor (ISAC New

    The Duties of the Iowa Assessor (ISAC New

    (Courses required to be approved of Iowa Department of Revenue) Membership of the County & City Conference Board County Assessor Conference Board (Iowa Code Chapter 441.2) County Board of Supervisors (Supervisor is chairperson) Mayor from each city within county School...
  • Sin título de diapositiva

    Sin título de diapositiva

  • Looking Into The Funny Figurative language uses FIGURES

    Looking Into The Funny Figurative language uses FIGURES

    is an overused, trite expressions or idea. Many cliches are idioms, but not all idioms are necessarily cliches. To say something is a cliche is a negative comment, whereas idioms usually are not. Sometimes cliches are used to be ironic...
  • Postclassical Europe: The Emergence of Christendom

    Postclassical Europe: The Emergence of Christendom

    Urbanization=merchants, artisans, bankers, universities. Guilds, apprentices. Agricultural technology increases productivity. Water and animal powered grain mills, larger looms. Women's rights limited, some professional rights, educated at convents. Territorial states with common cultures expand, but decentralized
  • The Following is a Product of the Northern

    The Following is a Product of the Northern

    "AIDS was sent by god to punish gay men." If HIV and AIDS were sent by god to punish gay men, a divine intervention of this nature was a very poor move. HIV has also infected a lot of women,...
  • The Apothecary &amp; Household System of Measurement

    The Apothecary & Household System of Measurement

    Apothecary System. An old system of measurement. First used by apothecaries (early pharmacists) and moved from Europe to colonial America. Household system evolved from the apothecary system. Older medications are still measured in apothecary units
  • Beyond The Survey… - EDCO

    Beyond The Survey… - EDCO

    Project analysis slide 4. Recommendations(Highlights). General. Work Force. Quality of life - Limited community programming to attract families and workers. Partner with municipalities to assist with understanding needs & developing regional strategies
  • Snímek 1 - cvut.cz

    Snímek 1 - cvut.cz

    SG, TG, DG Charakteristika Diamant CBN Měrná hustota (g.cm3) 3,52 3,48 Tvrdost Knoop (GPa) 60-110 40-70 Koeficient teplotní roztažnosti .10-6 (mm/mm/oC) 4,8 5,6 Hranice tepelné odolnosti (oC) 800 1400 Vlastnosti abrazivních materiálů Materiál zrna Tepelná vodivost [W.m-1.K-1] Diamant 140 Kubický...