1 / 60

Inheritance

Learn about inheritance, the "is-a" relationship between classes, and the benefits and syntax of using inheritance in programming. Explore different types of inheritance and how to access and reuse code from base classes.

martinezc
Download Presentation

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

  2. What is Inheritance?Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects. The term “insect” describes a very general type of creature with numerous characteristics. Grasshoppers and bumblebees are insects They share the general characteristics of an insect. However, they have special characteristics of their own. grasshoppers have a jumping ability, and bumblebees have a stinger. Grasshoppers and bumblebees are specialized versions of an insect. 10-3

  3. Inheritance

  4. The “is a” Relationship The relationship between a superclass and an inherited class is called an “is a” relationship. A grasshopper “is a” insect. A poodle “is a” dog. A car “is a” vehicle. A specialized object has: all of the characteristics of the general object, plus additional characteristics that make it special. In object-oriented programming, inheritance is used to create an “is a” relationship among classes. 10-5

  5. The “is a” Relationship We can extend the capabilities of a class. Inheritance involves a superclass and a subclass. The superclass is the general class and the subclass is the specialized class. The subclass is based on, or extended from, the superclass. Superclasses are also called base classes, and subclasses are also called derivedclasses. The relationship of classes can be thought of as parent classes and child classes. 10-6

  6. Inheritance, Fields and Methods Members of the superclass that are marked private: are not inherited by the subclass, exist in memory when the object of the subclass is created may only be accessed from the subclass by public methods of the superclass. Members of the superclass that are marked public: are inherited by the subclass, and may be directly accessed from the subclass. 10-7

  7. Inheritance base subclass2 subclass1 It is the Process of creating a New class from an existing class. The Existing class is called Base or Parent class. The New class is called as Child or Derived Class

  8. Advantages • It permits code reusability. So, save time and increase the program reliability. • Improve Program Reliability • It Permits code sharing

  9. Syntax for Inheritance class <derived classname > : access-specifier <base classname > { --- ---- };

  10. Access specifier Public - The members declared as Public are accessible from outside the Class through an object of the class. Protected - The members declared as Protected are accessible from outside the class BUT only in a class derived from it. Private - These members are only accessible from within the class. No outside Access is allowed.

  11. Base Class Methodsand Properties Inheritance is the property that allows the reuse of an existing class to build a new class Derived class Base class methods + Additional methods

  12. Inheritance Hierarchical Inheritance Single Inheritance Multilevel Inheritance Multiple Inheritance Types Of Inheritance

  13. Person Employee Single Inheritance A class can be derived from a single base class is called single inheritance

  14. Solution to Problem solving sequence

  15. Person Vehicle Car Racing car Multilevel Inheritance  A class be can derived from a derived class which is known as multilevel inheritance. Employee Part time Employee

  16. Employee Person Teacher Multiple Inheritance It is the process of creating new class from more than one base classes. Syntax : class <derived class >:<access specifier> base_class1,<access specifier> base_class2... { private : // members; protected : // members; public : //memebers; };

  17. Same Data Member Name in Base and Derived Class

  18. Hierarchical Inheritance Employee Permanent Employee Temporary Employee If more than one class is inherited from a base class, it's known as hierarchical inheritance. In general, all features that are common in child classes are included in base class in hierarchical inheritance.

  19. Use Scope resolution operator to access the data member

  20. Hybrid Inheritance Hybrid is nothing but the combination of Multilevel and multiple Inheritance

  21. Order of Constructor Call Base class constructors are always called in the derived class constructors. Whenever you create derived class object, first the base class default cpnstructor is executed and then the derived class's constructor finishes execution. Points to Remember Whether derived class's default constructor is called or parameterised is called, base class's default constructor is always called inside them. To call base class's parameterised constructor inside derived class's parameterised constructor, we must mention it explicitly while declaring derived class's parameterized constructor.

  22. Base class Parameterized Constructor in Derived class Constructor We can explicitly mention to call the Base class's parameterized constructor when Derived class's parameterized constructor is called.

  23. Constructor call in Multiple Inheritance Its almost the same, all the Base class's constructors are called inside derived class's constructor, in the same order in which they are inherited. class A : public B, public C ; In this case, first class B constructor will be calledd, then class C constructor and then class A constructor.

  24. Access Control to Class Members

More Related