Oop Introduction

2,912 views
2,850 views

Published on

This lecture describes the basic concepts of OOP such as Abstraction, Inheritance, Composition and aggregation

Published in: Education, Technology
1 Comment
10 Likes
Statistics
Notes
  • TAMK University of Applied Sciences
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
2,912
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
0
Comments
1
Likes
10
Embeds 0
No embeds

No notes for slide

Oop Introduction

  1. 1. Object Oriented Programming Department of Computer Science, University of Peshawar Lecture 03 Introduction Introduction Lec - 03
  2. 2. Outline <ul><li>Abstraction </li></ul><ul><li>Class </li></ul><ul><li>Difference b/w class and Object </li></ul><ul><li>Inheritance </li></ul><ul><li>Abstract Classes </li></ul><ul><li>Overriding……. </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  3. 3. Abstraction <ul><li>“ Ali is a MS student and teaches MSc class ” </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar <ul><li>Attributes </li></ul><ul><li>Name </li></ul><ul><li>Student Roll No </li></ul><ul><li>Year of study </li></ul><ul><li>Marks </li></ul><ul><li>Employee ID </li></ul><ul><li>Designation </li></ul><ul><li>Salary </li></ul><ul><li>Age </li></ul>
  4. 4. Abstraction <ul><li>Abstraction is a way to cope with complexity. E.g. Person (complex entity) </li></ul><ul><li>Principle of abstraction </li></ul><ul><li>“ Capture only those details about an object that are relevant to current perspective” </li></ul>Lecture 02 Introduction Department of Computer Science, University of Peshawar Lecture 03 Introduction
  5. 5. Example - Abstraction <ul><li>Student’s Perspective </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar <ul><li>Attributes </li></ul><ul><li>Name </li></ul><ul><li>Student Roll No </li></ul><ul><li>Year of study </li></ul><ul><li>Marks </li></ul><ul><li>Employee ID </li></ul><ul><li>Designation </li></ul><ul><li>Salary </li></ul><ul><li>Age </li></ul>
  6. 6. Example - Abstraction <ul><li>Student’s Perspective </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar <ul><li>Behavior </li></ul><ul><li>Study </li></ul><ul><li>Give Exam </li></ul><ul><li>Play Sports </li></ul><ul><li>Deliver Lecture </li></ul><ul><li>Develop Exam </li></ul><ul><li>Take Exam </li></ul><ul><li>Eat </li></ul><ul><li>Walk </li></ul>
  7. 7. Example - Abstraction <ul><li>Teacher’s Perspective </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar <ul><li>Attributes </li></ul><ul><li>Name </li></ul><ul><li>Student Roll No </li></ul><ul><li>Year of study </li></ul><ul><li>Marks </li></ul><ul><li>Employee ID </li></ul><ul><li>Designation </li></ul><ul><li>Salary </li></ul><ul><li>Age </li></ul>
  8. 8. Example - Abstraction <ul><li>Teacher’s Perspective </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar <ul><li>Behavior </li></ul><ul><li>Study </li></ul><ul><li>Give Exam </li></ul><ul><li>Play Sports </li></ul><ul><li>Deliver Lecture </li></ul><ul><li>Develop Exam </li></ul><ul><li>Take Exam </li></ul><ul><li>Eat </li></ul><ul><li>Walk </li></ul>
  9. 9. Abstraction - Advantage <ul><li>Simplifies the model by hiding irrelevant details </li></ul><ul><li>Abstraction provides the freedom to defer implementation decisions by avoiding commitment to details. </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  10. 10. Class <ul><li>In an OO model, some of the objects exhibits identical characteristics (information structure and behaviour) </li></ul>
  11. 11. Example - Class <ul><li>Ali studies mathematics. </li></ul><ul><li>Saleem studies computer science. </li></ul><ul><li>Tanveer studies statistics. </li></ul><ul><li>Each one is a Student </li></ul><ul><li>We say that these objects are instances of the Student class. </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  12. 12. Example - Class <ul><li>Iqbal teaches mathematics. </li></ul><ul><li>Saleem teaches computer science. </li></ul><ul><li>Tanveer teaches statistics. </li></ul><ul><li>Each one is a teacher. </li></ul><ul><li>We say that these objects are instances of the Teacher class. </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  13. 13. Lecture 03 Introduction Department of Computer Science, University of Peshawar
  14. 14. Declaration of an Object class Rectangle { private: int width; int length; public: void set(int w, int l); int area(); } main() { Rectangle r1; r1.set(5, 8); } r1 is statically allocated width length r1 width = 5 length = 8 Lecture 03 Introduction
  15. 15. Difference Between Object And Class <ul><li>A class is how you define an object. Classes are descriptive categories or templates. Book could be a class. </li></ul><ul><li>Objects are unique instances of classes. This Java Certification book that costs $59.99 with item ID 62467-B is an object of the Book class. </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  16. 16. Difference Between Object And Class (continue) <ul><li>The attributes and operations defined by a class are for its objects, not for itself. There is no concrete realization of the class Book, but there are Book objects, i.e. a class is a logical construct, an object has physical reality. </li></ul><ul><li>A class can be compared to a blueprint. Imagine you are in charge of building a housing development, with one housing blueprint. Each house in a development is shaped the same way, but some have brick or aluminum siding, some have custom paint colors inside and some are just white, and so on. </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  17. 17. Inheritance <ul><li>A child inherits characteristics of its parents </li></ul><ul><li>Besides inherited characteristics, a child may have its own unique characteristics </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  18. 18. Inheritance in classes <ul><li>If a class B inherits from class A then it contains all the characteristics(information structure and behaviour) of class A </li></ul><ul><li>The parent class is called base class and the child class is called derived class </li></ul><ul><li>Besides inherited characteristics, derived class may have its own unique characteristics </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  19. 19. Example - Inheritance Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Teacher Student Doctor
  20. 20. Inheritance Concept Rectangle Triangle Polygon class Polygon { private: int width, length; public: void set(int w, int l); } class Rectangle{ private: int width, length; public: void set(int w, int l); int area(); } class Triangle{ private: int width, length; public: void set(int w, int l); int area(); } Lecture 03 Introduction
  21. 21. Inheritance Concept Rectangle Triangle Polygon class Polygon { protected: int width, length; public: void set(int w, int l); } class Rectangle : public Polygon { public: int area(); } class Rectangle{ protected: int width, length; public: void set(int w, int l); int area(); } Lecture 03 Introduction
  22. 22. Inheritance Concept Rectangle Triangle Polygon class Polygon { protected: int width, length; public: void set(int w, int l); } class Triangle : public Polygon { public: int area(); } class Triangle{ protected: int width, length; public: void set(int w, int l); int area(); } Lecture 03 Introduction
  23. 23. Inheritance Concept Point Circle 3D-Point class Point { protected: int x, y; public: void set(int a, int b); } class Circle : public Point { private: double r; } class 3D-Point: public Point { private: int z; } x y x y r x y z Lecture 03 Introduction
  24. 24. <ul><li>Augmenting the original class </li></ul><ul><li>Specializing the original class </li></ul>Inheritance Concept RealNumber ComplexNumber ImaginaryNumber Rectangle Triangle Polygon Point Circle real imag real imag 3D-Point Lecture 03 Introduction
  25. 25. Inheritance – “IS A” or “IS A KIND OF” Relationship <ul><li>Each derived class is a special kind of its base class </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  26. 26. Example – “IS A” Relatioship Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Program Study_year Study Heldexam Teacher designation salary Teach Take_exam Doctor designation salary Check up prescribe
  27. 27. Example – “IS A” Relatioship Lecture 03 Introduction Department of Computer Science, University of Peshawar shape color coordinates Draw rotate Set color Circle radius draw Compute area Line length Draw Triangle angle Draw Compute area
  28. 28. Inheritance – Advantages <ul><li>Reuse </li></ul><ul><li>Less redundancy </li></ul><ul><li>Increased maintainability </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  29. 29. Reuse with Inheritance <ul><li>Main purpose of inheritance is reuse </li></ul><ul><li>We can easily add new classes by inheriting from existing classes </li></ul><ul><ul><li>Select an existing class closer to the desired functionality </li></ul></ul><ul><ul><li>create a new class and inherit it from the selected class </li></ul></ul><ul><ul><li>Add to and/or modify the inherited functionality </li></ul></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  30. 30. Example Reuse Lecture 03 Introduction Department of Computer Science, University of Peshawar Shape color coordinates Draw rotate Set color Circle radius draw Compute area Line length Draw Triangle angle Draw Compute area
  31. 31. Example – “IS A” Relatioship Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Program Study_year Study Heldexam Teacher designation salary Teach Take_exam Doctor designation salary Check up prescribe
  32. 32. Example – “IS A” Relatioship Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Program Study_year Study Heldexam Teacher designation salary Teach Take_exam Doctor designation salary Check up prescribe
  33. 33. Example Reuse Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Program Study_year Study Heldexam Teacher designation salary Teach Take_exam Doctor designation salary Check up prescribe
  34. 34. Concepts Related with Inheritance <ul><li>Generalization </li></ul><ul><li>Sub-typing (extension) </li></ul><ul><li>Specialization (restriction) </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  35. 35. Generalization <ul><li>In OO models, some classes may have common characteristics </li></ul><ul><li>We extract these features into a new class and inherit original classes from this new class </li></ul><ul><li>This concept is known as Generalization </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  36. 36. Example Generalization Lecture 03 Introduction Department of Computer Science, University of Peshawar Student Name Age gender Program Study_year Eat walk Study Heldexam Teacher Name age gender designation salary Eat walk Teach Take_exam Doctor Name Age gender designation salary Eat walk Check up prescribe
  37. 37. Example - Generalization Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Program Study_year Study Heldexam Teacher designation salary Teach Take_exam Doctor designation salary Check up prescribe
  38. 38. Sub-typing & Specialization <ul><li>We want to add a new class to an existing model </li></ul><ul><li>Find an existing class that already implemented some of the desired state and behaviour </li></ul><ul><li>Inherit the new class form this class and add unique behaviour to the new class </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  39. 39. Example – Sub-typing (Extension) Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Program Study_year Study Heldexam
  40. 40. Specialization (Restriction) <ul><li>Specialization means that derived class is behaviorally incompatible with the base class </li></ul><ul><li>Behaviorally incompatible means that base class can’t always be replaced by the derived class </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  41. 41. Example – Specialization (Restriction) Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Age: [0…..100] Set Age(a) Adult Age: [18….100] Set Age(a) Age = a If age<18 Error Else Age = a
  42. 42. Example – Specialization (Restriction) Lecture 03 Introduction Department of Computer Science, University of Peshawar integerSet ---------- Add ( element) Natural Set ------- Add ( element) Add element To the set If element<1 Error Else Add element to the set
  43. 43. Method Overriding <ul><li>A class may need to override the default behaviour provided by its base class </li></ul><ul><li>Method overriding , in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  44. 44. … Method Overriding.. <ul><li>In derived class, supply new version of that function </li></ul><ul><ul><li>Same function name, different definition </li></ul></ul><ul><li>Reasons for overriding </li></ul><ul><ul><li>Provide behaviour specific to a derived class </li></ul></ul><ul><ul><li>Extend the default behaviour </li></ul></ul><ul><ul><li>Restrict the default behaviour </li></ul></ul><ul><ul><li>Improve performance </li></ul></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  45. 45. Example – Specific Behavior Lecture 03 Introduction Department of Computer Science, University of Peshawar shape color coordinates Draw rotate Set color Circle radius draw Compute area Line length Draw Triangle angle Draw Compute area
  46. 46. Example – Improve Performance Lecture 03 Introduction Department of Computer Science, University of Peshawar Shape color coord draw Rotate Set color Circle redius Draw Rotate <ul><li>Class Circle override </li></ul><ul><li>Rotate operation </li></ul><ul><li>of class Shape </li></ul><ul><li>with a null </li></ul><ul><li>operation </li></ul>
  47. 47. Abstract Classes <ul><li>An abstract class implements an abstract concept </li></ul><ul><li>Main purpose is to be inherited by other classes </li></ul><ul><li>Can’t be instantiated </li></ul><ul><li>Promotes reuse </li></ul>Lecture 03 Introduction Department of Computer Science, University of Peshawar
  48. 48. Example – Abstract Classes Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Teacher Doctor <ul><li>Here, Person is an abstract class </li></ul>
  49. 49. Concrete Classes <ul><li>A concrete class implements a concrete concepts </li></ul><ul><li>Main purpose is to be instantiated </li></ul><ul><li>Provide implementation details specific to the domain context </li></ul>Lecture 03 Introduction
  50. 50. Example – Concrete Classes Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Teacher Doctor <ul><li>Here, Student, Teacher and Doctor are concrete class </li></ul>Program Study Year Study Held Exam
  51. 51. Types of Inheritance <ul><ul><li>Single Inheritance </li></ul></ul><ul><ul><ul><li>Class inherits from one base class </li></ul></ul></ul><ul><ul><li>Multiple Inheritance </li></ul></ul><ul><ul><ul><li>Class inherits from multiple base classes </li></ul></ul></ul>
  52. 52. Types of Inheritance
  53. 53. Multiple Inheritance <ul><li>We may want to reuse characteristics of more than one parent class </li></ul><ul><li>Class can inherit behaviors and features from more than one superclass. This contrasts with  single inheritance , where a class may inherit from at most one superclass </li></ul>
  54. 54. Example – Multiple Inheritance
  55. 55. Example – Multiple Inheritance Land Vehicle Vehicle Water Vehicle Car Amphibious Vehicle Boot
  56. 56. Problems with multiple Inheritance <ul><li>Increased complexity </li></ul><ul><li>Reduce Understanding </li></ul><ul><li>Duplicate features </li></ul>
  57. 57. … Problems with multiple Inheritance… <ul><li>Diamond Problem   </li></ul><ul><ul><li>Is an ambiguity that arises when two classes B and C inherited from A, and class D inherited from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C? </li></ul></ul>
  58. 58. ..Problems with multiple Inheritance..
  59. 59. Solution – Override the common Feature <ul><li>Which Change Gear operation Amphibious Vehicle inherits? </li></ul>Land Vehicle Change Gear Water Vehicle Car Amphibious Vehicle Boot Vehicle
  60. 60. Composition – “has-a” relationship <ul><li>An object may be composed of other smaller objects </li></ul><ul><li>The relationship b/w the “part” objects and the “whole” is known as Composition </li></ul><ul><li>Composition is represented by a line with a filled-diamond head towards the composer object </li></ul>
  61. 61. Example – Composition of Ali <ul><li>1 </li></ul><ul><li>2 </li></ul><ul><li>2 </li></ul><ul><li>1 </li></ul>Head Ali Arms legs Head
  62. 62. Example – Composition of chair <ul><li>1 </li></ul><ul><li>2 1 4 </li></ul>Lecture 03 Introduction Back Chair Arm seat Leg
  63. 63. Composition is Stronger <ul><li>Composition is Stronger relationship, </li></ul><ul><li>because </li></ul><ul><ul><li>Composed object becomes a part of the composer </li></ul></ul><ul><ul><li>Composed object can’t exist independently </li></ul></ul>
  64. 64. Example - Composition is Stronger <ul><li>Ali is made up different body parts </li></ul><ul><li>They can’t exist independent of Ali </li></ul>
  65. 65. Example - Composition is Stronger <ul><li>Chair’s body is made up of different parts </li></ul><ul><li>They can’t exist independently </li></ul>
  66. 66. Aggregation <ul><li>An object may contain a collection (aggregate) of other objects </li></ul><ul><li>The relationship b/w the container and the contained object is called aggregation </li></ul><ul><li>Aggregation is represented by a line with un-filled diamond head towards the container </li></ul>
  67. 67. Example – Aggregation <ul><li>1 </li></ul><ul><li>1 </li></ul><ul><li>1 </li></ul><ul><li>1 </li></ul>Bed Room Chair Table Cupboard
  68. 68. Aggregation <ul><li>Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. </li></ul><ul><li>For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. </li></ul>
  69. 69. Aggregation <ul><li>If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. </li></ul><ul><li>Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. </li></ul><ul><li>In addition, a Professor could work in more than one department, but a department could not be part of more than one university. </li></ul>
  70. 70. Aggregation is weaker <ul><li>Aggregation is weaker relationship, </li></ul><ul><li>because </li></ul><ul><ul><li>Aggregate object is not a part of the container </li></ul></ul><ul><ul><li>Aggregate object can exist independently </li></ul></ul>
  71. 71. Example - Aggregation is weaker <ul><li>Furniture is not an intrinsic part of room </li></ul><ul><li>Furniture can be shifted to another room, and so can exist independent of a particular room </li></ul>
  72. 72. Quizz (time-10 mins) <ul><li>What is an abstraction? Give an example. </li></ul><ul><li>What is mean by restriction in Inheritance? Give an example </li></ul>
  73. 73. <ul><li>Questions ? </li></ul>Thanks Lecture 03 Introduction

×