• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lecture01
 

Lecture01

on

  • 1,066 views

Object Oriented Concepts

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

Statistics

Views

Total Views
1,066
Views on SlideShare
1,064
Embed Views
2

Actions

Likes
0
Downloads
29
Comments
0

1 Embed 2

http://hijjawitech.blogspot.com 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lecture01 Lecture01 Presentation Transcript

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