Published on

Object Oriented Concepts
Object Oriented Programming (OOP) vs. Structured/Procedural Programming

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Object Oriented Software Modeling and Design CE 350 Abdel-Karim Al-Tamimi, Ph.D. [email_address] http://faculty.yu.edu.jo/altamimi Al-Tamimi 2010 ©
  2. 2. Overview <ul><li>Object Oriented Concepts </li></ul><ul><li>Object Oriented Programming (OOP) vs. Structured/Procedural Programming </li></ul>Al-Tamimi 2010 ©
  3. 3. Introduction to OOP <ul><li>Things in real life are objects , and an object has both: attributes and behaviors associated with it </li></ul><ul><li>Adam is a member of class Person , as a member of this class it takes on Person attributes and behaviors </li></ul>Walking, Eating, Sleeping, … Eye-Color, Age, Height, … Attributes Behaviors
  4. 4. OOP Concepts <ul><li>An object may have only attributes </li></ul><ul><ul><li>Multiple attributes: struct </li></ul></ul><ul><ul><li>A single attribute (e.g. integer) </li></ul></ul><ul><li>Attributes = data </li></ul><ul><li>Behavior = functions </li></ul><ul><li>Restricting the access to object’s data or functions is called data hiding </li></ul><ul><li>Combining data and behavior in the same object is called encapsulation </li></ul>
  5. 5. OOP vs. Procedural Programming Inputs Outputs object OOP Global Data Procedural Function 1 Function 2 Function 4 Function 3
  6. 6. Moving from Procedural to OOP <ul><li>Procedural separates data from the data-manipulations done on it </li></ul><ul><li>OOP encapsulates both data and its manipulation inside one object </li></ul>
  7. 7. Procedural/Structured Programming <ul><li>Carry out the required tasks using functions, if the function is too big, break it down to smaller functions </li></ul><ul><ul><li>Functional decomposition </li></ul></ul><ul><li>Functions are correlated with each other to form a data flow diagram (DFD) </li></ul>Input Process Output
  8. 8. Structured Programming Students Exams Courses Tutors Add Student Enter for Exams Check Exam Marks Issue Certificate Expel Student
  9. 9. Structured Programming <ul><li>Harder to maintain as the system grows </li></ul><ul><li>The impact of one change can affect many functions, which are hard to trace and to re-test </li></ul><ul><li>To overcome these difficulties in maintaining the code and to perform expected adjustments, object oriented approach was introduced </li></ul>
  10. 10. How to Think in Terms of Objects <ul><li>Know the difference between interface and implementation </li></ul><ul><li>Think more abstractly </li></ul><ul><li>Give the user the minimal interface possible </li></ul>
  11. 11. Object to Object Communications MyObject AnotherObject Data Data MyFunction1 MyFunction2 Function_1 Function_2
  12. 12. Object to Object Communications <ul><li>Each object provide a specific interface to allow other objects to communicate with it </li></ul><ul><li>The class interface includes the following: </li></ul><ul><ul><li>The method/function name </li></ul></ul><ul><ul><li>The inputs passed to the method/function </li></ul></ul><ul><ul><li>The return type of the method/function </li></ul></ul><ul><li>Any public data is considered also as a part of the class interface(better use get/set methods) </li></ul><ul><li>By designing the class interfaces , the developer hide extra or sensitive data </li></ul>
  13. 13. Object to Object Communications
  14. 14. Interface vs. Implementation
  15. 15. Interface vs. Implementation <ul><li>Only the services the end user needs are represented </li></ul><ul><ul><li>Data hiding with the use of encapsulation </li></ul></ul><ul><li>Change in the class implementation should not require change in the class user’s code </li></ul><ul><ul><li>Interface is still the same </li></ul></ul><ul><li>Always provide the minimal interface </li></ul><ul><li>Use abstract thinking in designing interfaces </li></ul><ul><ul><li>No unnecessary steps </li></ul></ul><ul><ul><li>Implement the steps in the class implementation </li></ul></ul>
  16. 16. Interface vs. Implementation Student Age, GPA, … +getAge():integer +admit():void +expel():void +getFullName():string attributes methods
  17. 17. How to determine the minimum interface possible <ul><li>Only what the user absolutely needs </li></ul><ul><ul><li>Fewer interfaces as possible </li></ul></ul><ul><ul><li>Use polymorphism </li></ul></ul><ul><li>Starts with hiding everything (private) </li></ul><ul><ul><li>Only use public interfaces (try not to use public attributes, instead get/set) </li></ul></ul><ul><li>Design your class from the user’s perspective and what they need (meet the requirements) </li></ul>
  18. 18. Determining the Class’s Environment <ul><li>Determine the class user </li></ul><ul><ul><li>What type of technical programmer </li></ul></ul><ul><li>Determine the behavior of the object </li></ul><ul><li>Environmental constraints on objects </li></ul><ul><ul><li>Hardware or/and software constraints </li></ul></ul><ul><li>The next step is the implementation, anything that is not a public interface is considered an implementation </li></ul>
  19. 19. Concept of Scope <ul><li>Local attributes </li></ul><ul><ul><li>Local to a specific method </li></ul></ul><ul><li>Object attributes </li></ul><ul><ul><li>Attributes shared between object methods </li></ul></ul><ul><li>Class attributes </li></ul><ul><ul><li>Attributes share between objects </li></ul></ul><ul><ul><li>Starts with static identifier </li></ul></ul>public class Employee{ … static int Count; … }
  20. 20. Object Attributes vs. Class Attributes
  21. 21. Inheritance <ul><li>Inheritance allows reusing code, with a better organization and overall management </li></ul><ul><li>Inheritance allows classes to inherit attributes from other classes, and you can still overwrite common behaviors </li></ul><ul><li>Inheritance categorizes classes into superclasses (parent class) and subclasses (child class) </li></ul>
  22. 22. Inheritance
  23. 23. Multiple Inheritance <ul><li>Class can inherit multiple classes </li></ul><ul><li>In real life: kids inherit both father and mother attributes and behaviors </li></ul><ul><li>Multiple inheritance is a complex solution that can solve complex problems elegantly </li></ul><ul><ul><li>Problem to the designer </li></ul></ul><ul><ul><li>Problem to the programming language compiler writer </li></ul></ul><ul><ul><li>Java and C# decided not to allow it in the form C++ allows it </li></ul></ul><ul><li>C# uses interfaces to carry on the architecture of methods that a child class should have </li></ul>
  24. 24. Polymorphism <ul><li>Polymorphism means many shapes </li></ul><ul><li>Polymorphism allows a single behavior to have different interfaces, and thus different inner-object behavior </li></ul><ul><li>Polymorphism means that similar objects can respond to the same message in different manners </li></ul><ul><li>Polymorphism gives the developer more options and different levels of customization to achieve a certain data manipulation or output </li></ul>
  25. 25. Polymorphism Al-Tamimi 2010 © +ReturnArea():double Shape +ReturnArea():double Circle +ReturnArea():double Square Area = Length^2 Area =   Radius^2 Returns a double value
  26. 26. Polymorphism Al-Tamimi 2010 © +GetArea(Shape) MainClass Will call Shape.ReturnArea() Area = Length^2 Area =   Radius^2 If shape is Circle If shape is Square
  27. 27. Constructors <ul><li>Only with objects </li></ul><ul><li>No return value (even void) </li></ul><ul><li>Method name is identical to class name </li></ul>public Student(int StudentNumber) { /* Implementation Code */ } ... Student BestStudent = new Student(1234); ...
  28. 28. Constructors <ul><li>Default constructor (no arguments) </li></ul><ul><li>Any object will have a constructor even if you do not define it, since it inherits its superclass constructor </li></ul><ul><ul><li>Every object is a subclass of object class </li></ul></ul><ul><ul><li>Superclass constructor is called by default </li></ul></ul><ul><li>Always provide a constructor </li></ul><ul><li>Overloading methods/constructors </li></ul><ul><ul><li>Same method name, different parameters </li></ul></ul><ul><ul><li>Polymorphism : using different objects that have the same superclass (child override parent) </li></ul></ul>
  29. 29. Constructors <ul><li>Initialize all attributes </li></ul><ul><li>Make sure that the initialization values leaves the object in a stable and safe state </li></ul><ul><li>Do not include implementation in the constructor </li></ul><ul><ul><li>Call a method if needed </li></ul></ul>Student Age, GPA, … +Student: +getAge:integer +admit:void +expel:void +getFullName:string
  30. 30. Error Handling <ul><li>What can be done as a software developer when there is a potential risk of causing an error: </li></ul><ul><ul><li>Ignore the problem that might cause the error </li></ul></ul><ul><ul><ul><li>Very dangerous, even if it has a very low probability </li></ul></ul></ul><ul><ul><li>Check the problem then abort the program </li></ul></ul><ul><ul><ul><li>Handle the error by aborting the program, and show the error message </li></ul></ul></ul><ul><ul><li>Check the problem, and try to fix it </li></ul></ul><ul><ul><ul><li>You already know how to fix it, may not be the best solution </li></ul></ul></ul><ul><ul><li>Check the problem and throw an exception </li></ul></ul><ul><ul><ul><li>Throw the exception to the calling object to handle it </li></ul></ul></ul>
  31. 31. Exception Handling <ul><li>Exception is unexpected event that happens in the system </li></ul><ul><li>We use keywords : try and catch and throw to manage exceptions </li></ul>try { // potential risky code } catch( Exception object) { // how to solve the exception // can not solve it, throw an exception }
  32. 32. Composition <ul><li>Composition is to combine multiple objects to achieve a better or more complex objects </li></ul><ul><li>Example : A computer is composed of different objects : memory, hard drive, motherboard, and each of these objects are composed of smaller objects … etc </li></ul>
  33. 33. Resources <ul><li>Matt Weisfeld, The Object Oriented Thought Process , Sams Publishing, ISBN:0-672-32611-6 </li></ul>