Inheritance Inheritance is the capability of one class of things to inherit the capabilities or properties from another class. Consider the example of.

Slides:



Advertisements
Similar presentations
CLASS INHERITANCE Class inheritance is about inheriting/deriving properties from another class. When inheriting a class you are inheriting the attributes.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
Need for Inheritance Capability of inheriting features. Transitive relationship. Reusablity of class.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Inheritance Definition Relationships Member Access Control Data Encapsulation Overloading vs. Overriding Constructors & Destructors.
Learners Support Publications Inheritance: Extending Classes.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
(10-1) OOP: Inheritance in C++ D & D Chapter 20 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance. 2 The process of deriving new class from the existing one is called inheritance Then the old class is called base class and the new class.
Object-Oriented PHP (1)
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
Inheritance: Extending classes Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Data Structures Using C++1 Chapter 2 Object-Oriented Design (OOD) and C++
Chapter 11: Inheritance and Composition. Objectives In this chapter, you will: – Learn about inheritance – Learn about derived and base classes – Redefine.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 11 More about classes and OOP. Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance.
CS212: Object Oriented Analysis and Design Lecture 14: Reusing classes in C++
1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.
Inheritance - Multilevel MEENA RAWAT PGT (COMP) KV ONGC Chandkheda 1.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Inheritance and Access Control CS 162 (Summer 2009)
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
Inheritance Inheritance allows a programmer to derive a new class from an existing one The existing class is called the super class, or parent class,
 A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is same as the class.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Class and Structure. 2 Structure Declare using the keyword struct purpose is to group data Default visibility mode is public A struct doesn't have a constructor.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance in Java. Access Specifiers private keywordprivate keyword –Used for most instance variables –private variables and methods are accessible.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Types of Inheritance in C++. In C++ we have 5 different types of inheritance: – Single Inheritance – Multiple Inheritance – Hierarchical Inheritance –
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.
Chapter 11: Inheritance and Composition. Introduction Two common ways to relate two classes in a meaningful way are: – Inheritance (“is-a” relationship)
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Learners Support Publications Constructors and Destructors.
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming in Java, 2e Sachin Malhotra Saurabh Choudhary.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS1201: Programming Language 2
MSIS 670 Object-Oriented Software Engineering
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Introduction Types Syntax Visibility Modes Example Programs
Constructors and Destructors
Inheritance:Concept of Re-usability
Institute of Petroloeum Technology, Gandhinagar
NAME 436.
B.FathimaMary Dept. of Commerce CA SJC Trichy-02
Final and Abstract Classes
Inheritance in C++ Inheritance Protected Section
Programming in C# CHAPTER 5 & 6
Computer Science II for Majors
Presentation transcript:

Inheritance Inheritance is the capability of one class of things to inherit the capabilities or properties from another class. Consider the example of cars. The class ‘Car’ inherits some of its properties from the class ‘Automobiles’ which inherits some of its properties from another class ‘Vehicles’.

VEHICLES CAR AUTOMOBILES (MOTOR DRIVEN) PULLED VEHICLES BUSRIKSHAW CART

Why Inheritance? Capability to express the inheritance relationship which makes it ensure the closeness with the real- world models.Capability to express the inheritance relationship which makes it ensure the closeness with the real- world models. Reusability of code. Inheritance allows the addition of additional features to an existing class without modifying it.Reusability of code. Inheritance allows the addition of additional features to an existing class without modifying it. It is transitive in nature. For example, if a new class ‘GraduateStudent’ has been declared as a sub-class of ‘Student’ which itself is a sub-class of ‘Person’ then ‘GraduateStudent’ must also be a ‘Person’ i.e. inheritance is transitive.It is transitive in nature. For example, if a new class ‘GraduateStudent’ has been declared as a sub-class of ‘Student’ which itself is a sub-class of ‘Person’ then ‘GraduateStudent’ must also be a ‘Person’ i.e. inheritance is transitive. Note : A subclass defines only those features that are unique to it.

Different Forms of Inheritance Single Inheritance : When a subclass inherits only from one base class, it is known as single inheritance. For Example :Single Inheritance : When a subclass inherits only from one base class, it is known as single inheritance. For Example : X Y Base Class Sub Class

Multiple Inheritance : When a sub class inherits from multiple base classes, it is known as multiple inheritance.Multiple Inheritance : When a sub class inherits from multiple base classes, it is known as multiple inheritance. Hierarchical Inheritance : When many sub classes inherits from a single base class, it is known as hierarchical inheritance.Hierarchical Inheritance : When many sub classes inherits from a single base class, it is known as hierarchical inheritance. Multilevel Inheritance : The transitive nature of inheritance is reflected by this form of inheritance. When a subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance.Multilevel Inheritance : The transitive nature of inheritance is reflected by this form of inheritance. When a subclass inherits from a class that itself inherits from another class, it is known as multilevel inheritance. Hybrid Inheritance : combines two or more forms of inheritance. When a sub class inherits from multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is known as hybrid inheritance.Hybrid Inheritance : combines two or more forms of inheritance. When a sub class inherits from multiple base classes and all of its base classes inherit from a single base class, this form of inheritance is known as hybrid inheritance.

Single Inheritance class derived-class-name : visibility mode base-class nameclass derived-class-name : visibility mode base-class name { ://members of derived class://members of derived class };}; The visibility mode controls the visibility and availability of inherited base class members in the derived class. e.g. Class Sub : public Super //public derivation { ://members of Sub (the derived class) }; Note : If no mode specified, private is the default mode.

Multiple Inheritance e.g. class Sub : public Super!, private SuperB { ://members of Sub class };

Visibility Modes Visibility Mode Inheritable public member becomes (in derived class) Inheritable protected member becomes (in derived class) Private member of base class are not directly accessible to derived class. publicpublicprotected protectedprotectedprotected privateprivateprivate

Accessibility of Base Class Members Access Specifier Accessible from own class Accessible from derived class (inheritable) Accessible from objects outside class publicYesYesYes protectedYesYesNo privateYesNono

Significance of Visibility Modes Derive publicly when the situation wants to derived class to have all the attributes of the base class, plus some extra attributes.Derive publicly when the situation wants to derived class to have all the attributes of the base class, plus some extra attributes. Derive privately when the derived class requires to use some attributes of the base class and these inherited features are intended not to be inherited further.Derive privately when the derived class requires to use some attributes of the base class and these inherited features are intended not to be inherited further. Derive protectedly when the features are required to be hidden from the outside world and at the same time required to be inheritabe.Derive protectedly when the features are required to be hidden from the outside world and at the same time required to be inheritabe.

Inheritance and Constructors & Destructors When an object of a derived class is created, first the base class constructor is invoked, followed by the derived class constructors. When an object of a derived class expires, first the derived class destructor is invoked, followed by the base class destructor.When an object of a derived class is created, first the base class constructor is invoked, followed by the derived class constructors. When an object of a derived class expires, first the derived class destructor is invoked, followed by the base class destructor. Passing Arguments through to a Base Class Constructor :Passing Arguments through to a Base Class Constructor : Derived::Derived(type1 x,type2 y…): Base(x,y)Derived::Derived(type1 x,type2 y…): Base(x,y) { : }

Example class Base {class Base { int a;int a; float b;float b; public:public: Base(int I,float j) { a = I; b = j; } :}; class Derived : public Base { public : Derived( int p, float q) : Base (p, q) { } };

Inheritance And Access Control Access control in Publicly Derived ClassAccess control in Publicly Derived Class Access control in Privately Derived ClassAccess control in Privately Derived Class Access control in Protectedly Derived ClassAccess control in Protectedly Derived Class Note : A derived class inherits all the members of the base class; however, the derived class has the direct access privilege only to the non-private members of the base class.Note : A derived class inherits all the members of the base class; however, the derived class has the direct access privilege only to the non-private members of the base class. Size of class is 1 byte if it do not have any data member in it.Size of class is 1 byte if it do not have any data member in it. The private members of the base class are visible in the derived class but they are not directly accessible.The private members of the base class are visible in the derived class but they are not directly accessible.

Making a private member inheritable By making the visibility mode of the private member as public.By making the visibility mode of the private member as public. By making the visibility mode of the private member as protected.By making the visibility mode of the private member as protected.

Constructors in Multiple Inheritance In multiple inheritance the base classes are constructed in the order in which they appear in the declaration of the derived classes.In multiple inheritance the base classes are constructed in the order in which they appear in the declaration of the derived classes. General form :General form : Derived-constructor(arg_list) : base1 (arg_list1),Derived-constructor(arg_list) : base1 (arg_list1), Base2 (arg_list2),Base2 (arg_list2), Base3 (arg_list3),Base3 (arg_list3), :a:a Base4(arg_listN)Base4(arg_listN)

Virtual Base Class When two or more objects are derived from a common base class, you can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. This is accomplished by putting the keyword virtual before the base class’ name when it is inherited.When two or more objects are derived from a common base class, you can prevent multiple copies of base class from being present in an object derived from those objects by declaring the base class as virtual when it is inherited. This is accomplished by putting the keyword virtual before the base class’ name when it is inherited. Relationship between classes : When a class inherits from another class, the derived class has is-a relationship with its base class. When a class contains object of another class-type, then containing/enclosing class has has-a relationship with contained/enclosed class.Relationship between classes : When a class inherits from another class, the derived class has is-a relationship with its base class. When a class contains object of another class-type, then containing/enclosing class has has-a relationship with contained/enclosed class.

Nesting of Classes When a class contains objects of other class types as its member, it is referred to as Containership or containment or Aggregation.When a class contains objects of other class types as its member, it is referred to as Containership or containment or Aggregation. Example : class X { : }; class Y { : }; class Z { X ob1; Y ob2; };