Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Thinking in Objects

Instructor: Dr. Fahed Jubair


Why Object-Oriented Programming?
Using objects allow the code to be
• Organized: related functionalities are put in the same class
• Modular: objects allow the code to be easily changed or extended within one
object without the need to change the details of other objects
• Reusable: objects can be reused, reducing the amount of code needs to be
created

The goal of the upcoming lectures is to understand design


principles that allow writing organized, flexible, and reusable
object-oriented code

© All rights reserved.


Class Abstraction and Encapsulation
• Class Abstraction is the separation of the implementation from the use
• Class Encapsulation is hiding the implementation details from the user
• In objected-oriented programming, preserving both of these principles
lead to simplicity and productivity
• Users (also called clients) only need to understand what services are provided by
a class without worrying about how those services are implemented
• Those services are made available as public methods to allow access from the
outside world

Question: how preserving abstraction and encapsulation lead to


having organized, flexible, and reusable object-oriented code?

© All rights reserved.


Example
Implementing a University
• Assume a Java program that represents a whole university
• It is too complex to build one class that represents everything in a university
• Instead, break the problem down into multiple classes, where each class
encapsulates relevant details
• Classes may exist in the program:
• Student class
• Course class Let us examine the details of this class in the next slide
• Instructor class
• Administer class
• Classroom class
• Faculty class
• University class
• Lecture class
© All rights reserved.
Example
The Student Class
Student

-name: String
-regId: String
UML representation -GPA: double
-major: String
+Student(name:String, regId:String)
+getName(): String
+getRegId(): String
+getGPA(): double
+setGPA(GPA:double): void
+getMajor(): String
+setMajor(major:String): void

© All rights reserved.


Association
• Association represents a temporary relationship between two objects
• One object does not belong to the other
• Objects are independent, i.e., one object continues to exist even if
other is destroyed

public class Student { Objects of Student and Sport


interact temporarily
public void play(Sport sport) {
…..
}
}

© All rights reserved.


UML Diagram For Association

Student Sport

0 .. * 0 .. *
… …

0..* on the left means a Student object can be associated with zero or more Sport objects
0..* on the right means a Sport object can be associated with zero or more Student objects

© All rights reserved.


Aggregation
• Aggregation indicates an object is a member in other object
• Also known as “has-a” relationship
• In aggregation, parts continue to exist even if whole is destroyed

public class Meeting { A Meeting object has a list of Employee objects


private List<Employee> attendees ;
public Meeting(){
attendee = new ArrayList<Employee>();
}
public void add(Employee attendee) {
attendees.add(attendee);
}
}
© All rights reserved.
UML Diagram For Aggregation

Meeting Employee

0 .. * 0 .. *

… …

0..* on the left means a Meeting object can have zero or more Employee objects
0..* on the right means an Employee object can be had by zero or more Meeting objects

© All rights reserved.


Composition
• Composition indicates an object is member of another object
• Also known “has-a” relationship
• Unlike aggregation, in composition, parts are destroyed if the whole is
destroyed (i.e., it describes a strong has-a relationship)

public class House {


A House object has a Room object
private Room room; Room object is destroyed if House is destroyed
public House(){
room = new Room();
}
}

© All rights reserved.


UML Diagram For Composition

House Room

1 .. *

… …

1..* on the right means a House object has one Room or more objects

© All rights reserved.


Be Careful
public class House { This is a composition
private Room room;
public House(){
room = new Room();
}
}

public class House {


This is an aggregation
private Room room;
public House(Room room){
this.room = room;
}
}

© All rights reserved.


Composition Example
Type-Wrapper Classes
• Java provides a class (in java.lang package) for primitive data types:
Boolean, Byte, Character, Double, Float, Integer, Long and Short
• Autoboxing and Auto-unboxing
Integer x = 10 ;
int y = x ;
• Type-wrapper classes also provide methods for manipulating data types
int x = Integer.parseInt(“153”) ; // x = 153
Double c = new Double(144.33) ; // c = 144.33
int z = c.intValue(); // z = 144
© All rights reserved.
Generalization
• Common principle used for reducing redundancy and increasing
reusability in the code
• Generalization refers to taking common attributes and behaviors
between multiple classes and encapsulating them in a different class
• Also known “is-a” relationship
• In Java, there are two ways to express Generalization:
• Using Inheritance
• Using Interfaces Topics of the next lesson

© All rights reserved.


Classroom Exercise
Imagine your developing a software for a university. Using only
Abstraction, Encapsulation, Association, Aggregation, and Composition,
draw UML diagrams that show the content and the relationships for the
following classes
• Student class
• Instructor class
• Classroom class
• Course class
• Lecture class

© All rights reserved.

You might also like