1 / 30

Object Oriented Programming: Basic Concepts of Inheritance

Learn the basic concepts of inheritance in object oriented programming, including single inheritance and the "IS-A" relationship.

lpink
Download Presentation

Object Oriented Programming: Basic Concepts of Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. In the Name of Allah the Most Beneficent the Most Merciful Subject: Object Oriented ProgrammingBasic concepts of inheritance

  2. Inheritance A child inherits characteristics of its parents Besides inherited characteristics, a child may have its own unique characteristics

  3. Inheritance… The mechanism of deriving a new class from an old one is called inheritance (or derivation). The old class is referred to as the base class the new one is called the derived class or subclass. The derived class inherits some or all of the characteristics from the base class. Besides inherited characteristics, a child may have its own unique characteristics

  4. Single Inheritance The colon indicates that the derived-class-name is derived from the base-class-name. The visibility mode is optional, and if present may be private or public. The default visibility mode is private. • A derived class with only one base class, is called single inheritance. • Syntax: • Class ABC : private XYZ • { • Members of ABC • }; • Class ABC : public XYZ • { • Members of ABC • };

  5. Inheritance in Classes If a class B inherits from class A then it contains all the characteristics (information structure and behavior) of class A The parent class is called base class and the child class is called derivedclass Besides inherited characteristics, derived class may have its own unique characteristics

  6. Example – Inheritance Person Doctor Student Teacher

  7. Example – Inheritance Shape Triangle Line Circle

  8. Inheritance – “IS A” or“IS A KIND OF” Relationship Each derived class is a special kind of its base class

  9. Example – “IS A” Relationship Person name age gender Here, Student IS A Person Teacher IS A Person Doctor IS A Person eat walk Student Teacher Doctor program studyYear designation salary designation salary study heldExam teach takeExam checkUp prescribe

  10. Example – “IS A” Relationship Shape color coord draw rotate setColor Here, Circle IS A Shape Line IS A Shape Triangle IS A Shape Triangle Circle angle radius Line draw computeArea length draw computeArea draw

  11. Inheritance – Advantages Reuse Less redundancy Increased maintainability

  12. 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

  13. Example Reuse Shape color coord draw rotate setColor Triangle Circle angle radius Line draw computeArea length draw computeArea draw

  14. Example Reuse Person name age gender eat walk Student Teacher Doctor program studyYear designation salary designation salary study heldExam teach takeExam checkUp prescribe

  15. Example Reuse Person name age gender eat walk Student Teacher Doctor program studyYear designation salary designation salary study heldExam teach takeExam checkUp prescribe

  16. Access Control and Inheritance A derived class can access all the non-private members of its base class. Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

  17. What Is Inherited? • Inherited • Data members • Most member functions • Functions not inherited • Private members • Constructors, copy constructors • Friend functions

  18. Making Private Member Inheritable C++ provides a third visibility modifier, Protected which serve a limited purpose in inheritance. A member declared as a protected is accessible by the member functions within its class and any class immediately derived from it. It can not be accessed by the functions outside these two classes. The keywords Private, Protected and Public may appear in any order and any number of times in the declaration of class.

  19. Example Is a valid class definition • Class ABC • { • Protecte : • ……………….. • Public : • ……………….. • Private : • ………………… • Public : • ……………….. • };

  20. Types of Inheritance When deriving a class from a base class, the base class may be inherited through public, protected or private inheritance. The type of inheritance is specified by the access-specifier. We hardly use protected or private inheritance, but public inheritance is commonly used. While using different type of inheritance, following rules are applied:

  21. Types of Inheritance… Public Inheritance: When deriving a class from public base class, public members of the base class become public members of the derived class and protected members of the base class become protected members of the derived class. A base class's private members are never accessible directly from a derived class, but can be accessed through calls to the public and protected members of the base class. Protected Inheritance: When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance: When deriving from a private base class, public and protected members of the base class become private members of the derived class.

  22. Types of Inheritance…

  23. Example • Class ABC : private XYZ • { • Members of ABC • }; • Class ABC : Protected XYZ • { • Members of ABC • }; • Class ABC : public XYZ • { • Members of ABC • };

  24. Abstract Classes An abstract class implements an abstract concept Main purpose is to be inherited by other classes Can’t be instantiated Promotes reuse

  25. Example – Abstract Classes Person name age gender eat walk Student Doctor Teacher Here, Person is an abstract class

  26. Example – Abstract Classes Vehicle color model accelerate applyBrakes Truck Car Bus Here, Vehicle is an abstract class

  27. Concrete Classes A concrete class implements a concrete concept Main purpose is to be instantiated Provides implementation details specific to the domain context

  28. Example – Concrete Classes Person Student Doctor program studyYear Teacher study heldExam Here, Student, Teacher and Doctor are concrete classes

  29. Example – Concrete Classes Vehicle Truck Car Bus capacity load unload • Here, Car, Bus and Truck are concrete classes

  30. Question and Answer

More Related