Chapter 10 Thinking in Objects Dr. Clincy
Structured or Procedural Programming Versus OO Programming Structured Programming Languages Fortran Pascal Ada C BASIC Description: Procedural approach tackles a specific problem upfront – build exactly what is needed to solve the problem, focus on using the specific data needed and in designing and implementing the needed subroutines and functions (or “methods” in using an OOP term) – focuses on specific data and methods. Object-Oriented Programming Languages Java Python Ruby C# C++ Visual Basic Description (Student): The initial step is to design and build classes and their operations (i.e. methods) with no real focus on a specific problem. The second step is to use the derived objects and their methods and data to solve some specific problem. In addition to a focus on data and methods in general, also focuses on “Classes and Objects” Description (Professional): The 1st step is to look for existing classes and objects that can be used to solve some specific problem. If no luck, determine which existing classes and objects can be altered. If no luck, build new classes and objects. Dr. Clincy
Structured or Procedural Programming Versus OO Programming Focuses on designing methods Data and operations on the data are separated Data must be passed to the methods Programs are more abstract and catered for a specific problem/use/etc.. Code reuse is not suitable Involves thinking in terms of specific methods and data Object-Oriented Programming Couples data and methods into objects Programs mirror real world Languages are built to easy allow objects to have associated attributes and activities Code reuse is suitable Involves thinking in terms of objects (and classes) Btw: encompasses structured programming with an added dimension that integrates data with operation into objects Dr. Clincy
Class Abstraction and Encapsulation Class abstraction means to separate class implementation from the use of the class. The creator of the class provides a description of the class and let the user know how the class can be used. The user of the class does not need to know how the class is implemented. The detail of implementation is encapsulated and hidden from the user. A description and collection of public constructors, methods, and field that are accessible from outside the class is called the Class’s Contract Encapsulation For OOP, it is really all about understanding Class’s Contract in not re-inventing the wheel Explain OOL to high-level language to low-level language concept Dr. Clincy
Example: Building a Car from a OOP Perspective Car has many components such as carburetor, starter, alternator, engine, AC system, gas tank, transmission system, etc Each component can be viewed as an object that has properties and methods To get the components to work together, you only need to know how the component is used and how it interacts with one another (abstraction) – OOP way of Thinking You don’t need to know how each component works internally – you can build the car without knowing (encapsulation). Dr. Clincy
Class Relationships In designing classes, you must first explore the relationships among classes. COMMON CLASS RELATIONSHIPS Association – a general binary relationship that describes an activity between two classes Aggregation – an association that represents an ownership relationship between two objects Composition – an association that represents an “exclusive” ownership relationship between two objects Inheritance – the ability to generate a specialized class (subclass) from a general class (superclass). Will cover this in a later chapter. Dr. Clincy
Association – UML Example Association – a general binary relationship that describes an activity between two classes A Faculty member teaching a course is an association between the Faculty Class and the Course Class A Student taking a Course is an association between the Student Class and the Course Class Relationship’s “label” and the relationship’s direction Relationship’s “label” and the relationship’s direction Student Role name that describes role in relationship A course is taught by only one faculty member A course may have 5 to 60 students A student may take any number of courses A faculty may at most 3 courses Dr. Clincy
Association – Implementation The association relations are implemented using data field and methods in classes. The Faculty-Teaches-A-Course relationship is implemented using addCourse method in Faculty class and setFaculty method in the Course class The Student-Taking-A-Course relationship is implemented using addCourse method in Student class and addStudent method in the Course class The Faculty class can use a list to store the courses the faculty is teaching The Student class can store a list of courses the student is taking The Course class can use a list to store the students taking the course The Course class can use a data field to store the faculty who teaches the course Dr. Clincy
Aggregation and Composition Aggregation – an association that represents an ownership relationship between two objects Owner object is called the “aggregating object” and it’s class is called the “aggregating class” Subject object is called the “aggregated object” and it’s class is called the “aggregated class” Name Student Address If the subject object CAN NOT exist without the Owner object, this aggregation is called “Composition” Name cannot exist without Student Address CAN exist without Student Up to 3 students can share the same address Dr. Clincy
Aggregation and Composition - Implementation The Student-Has-A-Name relationship is implemented using the data field “name” in the Student class – the aggregating class The Student-Has-An-Address relationship is implemented using the data field “name” in the Student class – the aggregating class NOTE: Since aggregation and composition relationships are represented using classes in similar ways, many textbooks don’t differentiate them and call both compositions. Dr. Clincy
Aggregation Between Same Class Aggregation may exist between objects of the same class. For example, a person may have a supervisor. public class Person { // The type for the data is the class itself private Person supervisor; ... } The Person-Has-A-Supervisor relationship implementation What happens if a person has several supervisors? Can use an array to implement the Person-Has-Many-Supervisors relationship Dr. Clincy
Wrapper Classes Boolean Integer Character Long Short Float Byte Double Primitive data types (ie. Bool, Char, Int, Byte, etc) are NOT treated like objects – for performance and efficiency reasons However, many Java methods require the use of objects as arguments Java provides the ability to incorporate or “wrap” a primitive data type value into an object – wrapper class Java provides the following wrapper classes via it’s java.lang package: Boolean Character Short Byte Integer Long Float Double NOTE: (1) The wrapper classes do not have no-arg constructors. (2) The instances of all wrapper classes are immutable, i.e., their internal values cannot be changed once the objects are created. Dr. Clincy
We will cover the String class in the next lecture. Let’s review and start Lab 2 Dr. Clincy