Object Oriented Programming Department  of Computer Science, University of Peshawar Lecture 03 Introduction Introduction Lec - 03
Outline Abstraction Class Difference b/w class and Object Inheritance Abstract Classes Overriding……. Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Abstraction “   Ali is a MS student and teaches MSc class ” Lecture 03 Introduction Department  of Computer Science, University of Peshawar Attributes Name Student Roll No Year of study Marks Employee ID Designation Salary Age
Abstraction Abstraction is a way to cope with complexity. E.g. Person (complex entity) Principle of abstraction “ Capture only those details about an object that are relevant to current perspective” Lecture 02 Introduction Department  of Computer Science, University of Peshawar Lecture 03 Introduction
Example - Abstraction Student’s Perspective Lecture 03 Introduction Department  of Computer Science, University of Peshawar Attributes Name Student Roll No Year of study Marks Employee ID Designation Salary Age
Example - Abstraction Student’s Perspective Lecture 03 Introduction Department  of Computer Science, University of Peshawar Behavior Study Give Exam Play Sports Deliver Lecture Develop Exam Take Exam Eat Walk
Example - Abstraction Teacher’s Perspective Lecture 03 Introduction Department  of Computer Science, University of Peshawar Attributes Name Student Roll No Year of study Marks Employee ID Designation Salary Age
Example - Abstraction Teacher’s Perspective Lecture 03 Introduction Department  of Computer Science, University of Peshawar Behavior Study Give Exam Play Sports Deliver Lecture Develop Exam Take Exam Eat Walk
Abstraction - Advantage Simplifies the model by hiding irrelevant details Abstraction provides the freedom to defer implementation decisions by avoiding commitment to details. Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Class In an OO model, some of the objects exhibits identical characteristics (information structure and behaviour)
Example - Class Ali studies mathematics. Saleem studies computer science. Tanveer studies statistics. Each one is a Student We say that these objects are instances of the Student class. Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Example - Class Iqbal teaches mathematics. Saleem teaches computer science. Tanveer teaches statistics. Each one is a teacher. We say that these objects are instances of the Teacher class. Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Lecture 03 Introduction Department  of Computer Science, University of Peshawar
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
Difference Between Object And Class A class is how you define an object. Classes are descriptive categories or templates. Book could be a class. 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. Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Difference Between Object And Class (continue) 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. 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. Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Inheritance A child inherits characteristics of its parents Besides inherited characteristics, a child may have its own unique characteristics Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Inheritance in classes If a class B inherits from class A then it contains all the characteristics(information structure and behaviour) of class A The parent class is called base class and the child class is called derived class Besides inherited characteristics, derived class may have its own unique characteristics Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Example - Inheritance Lecture 03 Introduction Department  of Computer Science, University of Peshawar Person Teacher Student Doctor
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
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
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
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
Augmenting the original class Specializing the original class Inheritance Concept RealNumber ComplexNumber ImaginaryNumber Rectangle Triangle Polygon Point Circle real imag real imag 3D-Point Lecture 03 Introduction
Inheritance – “IS A” or “IS A KIND OF” Relationship Each derived class is a special kind of its base class Lecture 03 Introduction Department  of Computer Science, University of Peshawar
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
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
Inheritance – Advantages Reuse Less redundancy Increased maintainability Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Reuse with  Inheritance Main purpose of inheritance is reuse We can easily add new classes by inheriting from existing classes Select an existing class closer to the desired functionality create a new class and inherit it from the selected class Add to and/or modify the inherited functionality Lecture 03 Introduction Department  of Computer Science, University of Peshawar
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
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
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
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
Concepts Related with Inheritance Generalization Sub-typing (extension) Specialization (restriction) Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Generalization In OO models, some classes may have common characteristics We extract these features into a new class and inherit original classes from this new class This concept is known as Generalization Lecture 03 Introduction Department  of Computer Science, University of Peshawar
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
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
Sub-typing & Specialization We want to add a new class to an existing model Find an existing class that already implemented some of the desired state and behaviour Inherit the new class form this class and add unique behaviour to the new class Lecture 03 Introduction Department  of Computer Science, University of Peshawar
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
Specialization (Restriction) Specialization means that derived class is behaviorally incompatible with the base class Behaviorally incompatible means that base class can’t always be replaced by the derived class Lecture 03 Introduction Department  of Computer Science, University of Peshawar
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
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
Method Overriding A class may need to override the default behaviour provided by its base class 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  Lecture 03 Introduction Department  of Computer Science, University of Peshawar
… Method Overriding.. In derived class, supply new version of that function Same function name, different definition Reasons for overriding Provide behaviour specific to a derived class Extend the default behaviour Restrict the default behaviour Improve performance Lecture 03 Introduction Department  of Computer Science, University of Peshawar
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
Example – Improve Performance Lecture 03 Introduction Department  of Computer Science, University of Peshawar Shape color coord draw Rotate Set color Circle redius Draw Rotate Class Circle override Rotate operation of class Shape with a null operation
Abstract Classes An abstract class implements an abstract concept Main purpose is to be inherited by other classes Can’t be instantiated Promotes reuse Lecture 03 Introduction Department  of Computer Science, University of Peshawar
Example – Abstract Classes Lecture 03 Introduction Department  of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Teacher Doctor Here, Person is an abstract class
Concrete Classes A concrete class implements a concrete concepts Main purpose is to be instantiated Provide implementation details specific to the domain context Lecture 03 Introduction
Example – Concrete Classes Lecture 03 Introduction Department  of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Teacher Doctor Here, Student, Teacher and Doctor are concrete class Program Study Year Study Held Exam
Types of Inheritance Single Inheritance  Class inherits from one base class Multiple Inheritance  Class inherits from multiple base classes
Types of Inheritance
Multiple Inheritance We may want to reuse characteristics of more than one parent class 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
Example – Multiple Inheritance
Example – Multiple Inheritance Land Vehicle Vehicle Water Vehicle Car Amphibious Vehicle Boot
Problems with multiple  Inheritance Increased complexity Reduce Understanding Duplicate features
… Problems with multiple  Inheritance… Diamond Problem   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?
..Problems with multiple  Inheritance..
Solution – Override the common Feature Which Change Gear operation Amphibious Vehicle inherits? Land Vehicle Change Gear Water Vehicle Car Amphibious Vehicle Boot Vehicle
Composition – “has-a” relationship An object may be composed of other smaller objects The relationship b/w the “part” objects and the “whole” is known as Composition Composition is represented by a line with a filled-diamond head towards the composer object
Example – Composition of Ali 1 2 2 1 Head Ali Arms legs Head
Example – Composition of chair 1 2  1  4 Lecture 03 Introduction Back Chair Arm seat Leg
Composition is Stronger Composition is Stronger relationship, because Composed object becomes a part of the composer  Composed object can’t exist independently
Example - Composition is Stronger Ali is made up different body parts They can’t exist independent of Ali
Example - Composition is Stronger Chair’s body is made up of different parts They can’t exist independently
Aggregation An object may contain a collection (aggregate) of other objects The relationship b/w the container and the contained object is called  aggregation Aggregation is represented by a line with un-filled diamond head towards the container
Example – Aggregation 1 1 1 1 Bed Room Chair Table Cupboard
Aggregation 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. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors.
Aggregation If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist.  Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.
Aggregation is weaker Aggregation is weaker relationship, because Aggregate object is not a part of the container Aggregate object can exist independently
Example - Aggregation is weaker Furniture is not an intrinsic part of room Furniture can be shifted to another room, and so can exist independent of a particular room
Quizz (time-10 mins) What is an abstraction? Give an example. What is mean by restriction in Inheritance? Give an example
Questions ? Thanks Lecture 03 Introduction

Oop Introduction

  • 1.
    Object Oriented ProgrammingDepartment of Computer Science, University of Peshawar Lecture 03 Introduction Introduction Lec - 03
  • 2.
    Outline Abstraction ClassDifference b/w class and Object Inheritance Abstract Classes Overriding……. Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 3.
    Abstraction “ Ali is a MS student and teaches MSc class ” Lecture 03 Introduction Department of Computer Science, University of Peshawar Attributes Name Student Roll No Year of study Marks Employee ID Designation Salary Age
  • 4.
    Abstraction Abstraction isa way to cope with complexity. E.g. Person (complex entity) Principle of abstraction “ Capture only those details about an object that are relevant to current perspective” Lecture 02 Introduction Department of Computer Science, University of Peshawar Lecture 03 Introduction
  • 5.
    Example - AbstractionStudent’s Perspective Lecture 03 Introduction Department of Computer Science, University of Peshawar Attributes Name Student Roll No Year of study Marks Employee ID Designation Salary Age
  • 6.
    Example - AbstractionStudent’s Perspective Lecture 03 Introduction Department of Computer Science, University of Peshawar Behavior Study Give Exam Play Sports Deliver Lecture Develop Exam Take Exam Eat Walk
  • 7.
    Example - AbstractionTeacher’s Perspective Lecture 03 Introduction Department of Computer Science, University of Peshawar Attributes Name Student Roll No Year of study Marks Employee ID Designation Salary Age
  • 8.
    Example - AbstractionTeacher’s Perspective Lecture 03 Introduction Department of Computer Science, University of Peshawar Behavior Study Give Exam Play Sports Deliver Lecture Develop Exam Take Exam Eat Walk
  • 9.
    Abstraction - AdvantageSimplifies the model by hiding irrelevant details Abstraction provides the freedom to defer implementation decisions by avoiding commitment to details. Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 10.
    Class In anOO model, some of the objects exhibits identical characteristics (information structure and behaviour)
  • 11.
    Example - ClassAli studies mathematics. Saleem studies computer science. Tanveer studies statistics. Each one is a Student We say that these objects are instances of the Student class. Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 12.
    Example - ClassIqbal teaches mathematics. Saleem teaches computer science. Tanveer teaches statistics. Each one is a teacher. We say that these objects are instances of the Teacher class. Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 13.
    Lecture 03 IntroductionDepartment of Computer Science, University of Peshawar
  • 14.
    Declaration of anObject 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.
    Difference Between ObjectAnd Class A class is how you define an object. Classes are descriptive categories or templates. Book could be a class. 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. Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 16.
    Difference Between ObjectAnd Class (continue) 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. 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. Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 17.
    Inheritance A childinherits characteristics of its parents Besides inherited characteristics, a child may have its own unique characteristics Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 18.
    Inheritance in classesIf a class B inherits from class A then it contains all the characteristics(information structure and behaviour) of class A The parent class is called base class and the child class is called derived class Besides inherited characteristics, derived class may have its own unique characteristics Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 19.
    Example - InheritanceLecture 03 Introduction Department of Computer Science, University of Peshawar Person Teacher Student Doctor
  • 20.
    Inheritance Concept RectangleTriangle 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.
    Inheritance Concept RectangleTriangle 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.
    Inheritance Concept RectangleTriangle 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.
    Inheritance Concept PointCircle 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.
    Augmenting the originalclass Specializing the original class Inheritance Concept RealNumber ComplexNumber ImaginaryNumber Rectangle Triangle Polygon Point Circle real imag real imag 3D-Point Lecture 03 Introduction
  • 25.
    Inheritance – “ISA” or “IS A KIND OF” Relationship Each derived class is a special kind of its base class Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 26.
    Example – “ISA” 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.
    Example – “ISA” 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.
    Inheritance – AdvantagesReuse Less redundancy Increased maintainability Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 29.
    Reuse with Inheritance Main purpose of inheritance is reuse We can easily add new classes by inheriting from existing classes Select an existing class closer to the desired functionality create a new class and inherit it from the selected class Add to and/or modify the inherited functionality Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 30.
    Example Reuse Lecture03 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.
    Example – “ISA” 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.
    Example – “ISA” 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.
    Example Reuse Lecture03 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.
    Concepts Related withInheritance Generalization Sub-typing (extension) Specialization (restriction) Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 35.
    Generalization In OOmodels, some classes may have common characteristics We extract these features into a new class and inherit original classes from this new class This concept is known as Generalization Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 36.
    Example Generalization Lecture03 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.
    Example - GeneralizationLecture 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.
    Sub-typing & SpecializationWe want to add a new class to an existing model Find an existing class that already implemented some of the desired state and behaviour Inherit the new class form this class and add unique behaviour to the new class Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 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.
    Specialization (Restriction) Specializationmeans that derived class is behaviorally incompatible with the base class Behaviorally incompatible means that base class can’t always be replaced by the derived class Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 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.
    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.
    Method Overriding Aclass may need to override the default behaviour provided by its base class 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 Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 44.
    … Method Overriding..In derived class, supply new version of that function Same function name, different definition Reasons for overriding Provide behaviour specific to a derived class Extend the default behaviour Restrict the default behaviour Improve performance Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 45.
    Example – SpecificBehavior 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.
    Example – ImprovePerformance Lecture 03 Introduction Department of Computer Science, University of Peshawar Shape color coord draw Rotate Set color Circle redius Draw Rotate Class Circle override Rotate operation of class Shape with a null operation
  • 47.
    Abstract Classes Anabstract class implements an abstract concept Main purpose is to be inherited by other classes Can’t be instantiated Promotes reuse Lecture 03 Introduction Department of Computer Science, University of Peshawar
  • 48.
    Example – AbstractClasses Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Teacher Doctor Here, Person is an abstract class
  • 49.
    Concrete Classes Aconcrete class implements a concrete concepts Main purpose is to be instantiated Provide implementation details specific to the domain context Lecture 03 Introduction
  • 50.
    Example – ConcreteClasses Lecture 03 Introduction Department of Computer Science, University of Peshawar Person Name Age Gender Eat walk Student Teacher Doctor Here, Student, Teacher and Doctor are concrete class Program Study Year Study Held Exam
  • 51.
    Types of InheritanceSingle Inheritance Class inherits from one base class Multiple Inheritance Class inherits from multiple base classes
  • 52.
  • 53.
    Multiple Inheritance Wemay want to reuse characteristics of more than one parent class 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
  • 54.
  • 55.
    Example – MultipleInheritance Land Vehicle Vehicle Water Vehicle Car Amphibious Vehicle Boot
  • 56.
    Problems with multiple Inheritance Increased complexity Reduce Understanding Duplicate features
  • 57.
    … Problems withmultiple Inheritance… Diamond Problem   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?
  • 58.
  • 59.
    Solution – Overridethe common Feature Which Change Gear operation Amphibious Vehicle inherits? Land Vehicle Change Gear Water Vehicle Car Amphibious Vehicle Boot Vehicle
  • 60.
    Composition – “has-a”relationship An object may be composed of other smaller objects The relationship b/w the “part” objects and the “whole” is known as Composition Composition is represented by a line with a filled-diamond head towards the composer object
  • 61.
    Example – Compositionof Ali 1 2 2 1 Head Ali Arms legs Head
  • 62.
    Example – Compositionof chair 1 2 1 4 Lecture 03 Introduction Back Chair Arm seat Leg
  • 63.
    Composition is StrongerComposition is Stronger relationship, because Composed object becomes a part of the composer Composed object can’t exist independently
  • 64.
    Example - Compositionis Stronger Ali is made up different body parts They can’t exist independent of Ali
  • 65.
    Example - Compositionis Stronger Chair’s body is made up of different parts They can’t exist independently
  • 66.
    Aggregation An objectmay contain a collection (aggregate) of other objects The relationship b/w the container and the contained object is called aggregation Aggregation is represented by a line with un-filled diamond head towards the container
  • 67.
    Example – Aggregation1 1 1 1 Bed Room Chair Table Cupboard
  • 68.
    Aggregation Aggregation differsfrom 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. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors.
  • 69.
    Aggregation If theuniversity closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.
  • 70.
    Aggregation is weakerAggregation is weaker relationship, because Aggregate object is not a part of the container Aggregate object can exist independently
  • 71.
    Example - Aggregationis weaker Furniture is not an intrinsic part of room Furniture can be shifted to another room, and so can exist independent of a particular room
  • 72.
    Quizz (time-10 mins)What is an abstraction? Give an example. What is mean by restriction in Inheritance? Give an example
  • 73.
    Questions ? ThanksLecture 03 Introduction