Inheritance Dr. Bhargavi Goswami Department of Computer Science

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
LOGO Lecturer: Abdullahi Salad Abdi May 1, Object Oriented Programming in C++
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
Learners Support Publications Inheritance: Extending Classes.
Chapter 10: Introduction to Inheritance
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance and Polymorphism CS351 – Programming Paradigms.
Inheritance: Extending classes Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Coming up: Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Classes, Interfaces and Packages
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Final and Abstract Classes
Inheritance and Polymorphism
Inheritance, Polymorphism, and Virtual Functions
Inheritance in Java.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming Language Concepts (CIS 635)
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Understanding Inheritance
Java Programming Language
Inheritance, Polymorphism, and Virtual Functions
Dr. Bhargavi Dept of CS CHRIST
Constructors and destructors
Operator overloading Dr. Bhargavi Goswami
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
By Muhammad Waris Zargar
Derived Classes in C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Object-Oriented Programming
Java Programming, Second Edition
Object-Oriented Programming in PHP
CISC/CMPE320 - Prof. McLeod
Inheritance:Concept of Re-usability
Chapter 8: Class Relationships
Object-Oriented Programming
Inheritance and Polymorphism
Institute of Petroloeum Technology, Gandhinagar
Chapter 9 Inheritance.
CIS 199 Final Review.
Review of Previous Lesson
CPS120: Introduction to Computer Science
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
COP 3330 Object-oriented Programming in C++
C++ Object Oriented 1.
Computer Science II for Majors
Presentation transcript:

Inheritance Dr. Bhargavi Goswami Department of Computer Science Christ University Bangalore. bhargavigoswami@gmail.com 9426669020

Inheritance Purpose: Reusability of code. Save time and money of coding. Increase reliability Reduce frustration of error solving and testing. Inheritance: reusing the properties of existing ones and deriving new class from old one. Old class is called base class New class is called derived class or subclass. Inheritance provide derived class to inherit some or all traits of base class using access specifiers.

Inheritance access specifier

Types of inheritance

Defining derived classes

Single inheritance: public Suppose D is derived class from class B. D inherits all the public members of B as public. But, D can access B’s private members using public functions of B in main. Eg. 8-1- singleInhertancePublic .cpp

Single inheritance: private Suppose D is derived class from class B. D inherits all the public members of B as public and private members are private. To access functions of B, D’s functions should have call statements of B’s functions. Eg. 8-2- singleInhertancePrivat e.cpp

WHAT IS PRIVATE, PROTECTED AND PUBLIC class alpha { private: //optional, by default private ---- //visible to member functions ---- //within its class protected: //visible to member functions ---- //of its own and that of ---- //derived class public: //visible to all functions ---- // in the program ---- };

WHAT IS PRIVATE, PROTECTED AND PUBLIC A public member is accessible from anywhere outside the class but within a program. You can set and get the value of public variables without any member. A private member variable or function cannot be accessed, or even viewed from outside the class. Only the class and friend functions can access private members. A protected member variable or function is very similar to a private member but, it provided one additional benefit that they can be accessed in child classes which are called derived classes.

Private v/s protected BASIS FOR COMPARISON PRIVATE PROTECTED Inheriting property to the derived class Derived class cannot access base class private members. Derived class can access base class protected members. Accessibility The private members of the class are inaccessible out of the class scope. The protected members of the class are inaccessible out of the class scope except the class derived immediately. Accessible from own class Yes Accessible from derived class No Accessible from outside Flexibility Low High

PRIVATE, PROTECTED AND PUBLIC: Conclusion in short private: base protected: base + derived public: base + derived + any other member Access specifier Class Subclass world Private Yes No Protected Public

Making private function inheritable

Inheritance access specifier

Never to forget

Friend and inheritance

Access control and inheritance

Multilevel inheritance Here, A is base class, B is intermediate base class and C is derived class. See example of student, test and result class. Eg. 8-3- multilevelInheritance.cpp

Multiple inheritance Multiple Inheritance: A class that can inherit the attributes of two or more classes. See below syntax. Eg. 8-4-multipleInheritance Limitations: ambiguity in handling same named function inherited from multiple base classes.

Situation: Same function name in base and derived class Can base class and derived class have same function names? Yes. Then what will happen if derived class obj invokes that common named function? Ans. Derived class function will supersedes base class definition. (obviously we don’t use things of others if we already have it) Then when the base class function definition will be executed? In case the derived class function is not redefined, base class function will be called.

Resolution of ambiguity in multiple inheritance Suppose there are two classes N and M as base class and class P as derived class with multiple inheritance. If there is a common function called display() in class N and M, which one will be called by object of P? Answer is in next program. Eg. AmbiguityResolutionMultiple.cpp

HIERARCHICAL INHERITANCE

HIERARCHICAL INHERITANCE

HYBRID INHERITANCE There can be a situation where we need to apply multiple types of inheritance. Suppose sports credit is considered in final result. How to implement it? Ans: using hybrid inheritance. Eg. 8-5-hybridInheritance.cpp

Virtual base class Consider a situation where all three kind of inheritance are involved. i.e Multiple, Multi- level, Hierarchical. Grandparents Parent 1 Parent 2 Child

Virtual base class Now, child inherits grand parents via three separate paths. Means, child will have duplicate copy of grandparents. Does this introduce ambiguity? (Yes/No) Yes. Needs to be avoided? Yes. But how? By declaring common base class as virtual. Lets see how.

Virtual base class class A {}; class B1 : virtual public A {}; class B2 : public virtual A {}; class C : public B1, public B2 {}; Virtual and Public can be use in any order. See example program 8.6, virtual base class. A B1 B2 C

Abstract class Abstract class is designed only to act as base class inherited by other classes. No direct object is created for abstract class. Designed for? Programming concepts, projects. Has great significance in logical program design and real programming scenario. It holds common characteristics of all derived classes. It must have one pure virtual function. What is pure virtual function? A pure virtual function (or abstract function) in C++ is a virtual function for which we don’t have implementation, we only declare it. A pure virtual function is declared by assigning 0 in declaration.

Abstract class If we do not override the pure virtual function in derived class, then derived class also becomes abstract class. An abstract class can have constructors. A class is abstract if it has at least one pure virtual function We can have pointers and references of abstract class type. Eg. AbstractClassUsingPureVirtualFun.cpp

Abstract class vehicle Class Data-type-d1 Private Member variable virtual void spec() = 0; Pure virtual function LMV Class void spec() Public function HMV Class void spec() Public function TW Class void spec() Public function

Virtual v/s abstract The methods in virtual class CAN be overridden in derived classes, while abstract class methods MUST be overridden. Particulars Virtual Abstract Methods Methods may be overridden by derived class. All methods must be overridden by derived class. Instance Can be instantiated. Cannot be instantiated. Patent Class Generally implemented as solution to multiple inheritance. Partial implementation of concept in parent and partial in derived.

Constructors and inheritance If base class has unparameterized constructor, to access that, any action is required in derived class? If base class has parameterized constructor, what action is required in derived class? Answer: For unparameterized constructor in base class, no action is required. But, mandatory action is required for parameterized constructor. Means? Parameters has to be passed from derived class to base class constructors. What if derived and base both class has constructor? What order they execute? Ans: First Base Class Constructors and then Derived Class Constructors.

Constructors and inheritance But, what if it is multiple inheritance? The order will be in accordance with declaration statement. And what if it is multi level inheritance? Constructor will be according to order of levels. Who is responsible for providing initial values to base class constructors? Derived class. How do they do that? Supply all the required initial values when object is declared. How the arguments are passed to base class? Order of arguments are followed. And if one is virtual base class and one is ordinary? Higher priority is given to virtual base class. Confused? Don’t worry. Lets see two example to clarify. Two Methods: a) With colon, b) Initialization list.

Alternate of inheritance: Initialization list Inheritance is the only way to derive properties of base class? Is there any alternate of inheritance? (Yes/No) Ans: Yes. How? Objects can be a collection of many other objects. Means? Class can contain objects of other classes as its members. Eg: class alpha{……}; class beta{……}; class gamma { alpha a; //object of class alpha beta b; //object of class beta …… }; We call it containership of nesting. For this we need initialization list to provide values to all the members of base classes. See next slide for example.

Alternate of inheritance For the same example we inherit properties without inheritance. Note: Y we do not provide datatype to argument list? Because it is function call and not direct access to members. Eg. 8-7-ConstructorsInDerivedClass.cpp Eg. 8-8-InitializationListInConstructors.cpp stx: class gamma { ..... alpha a; beta b; public: gamma (arglist) : a(arglist1), b(arglist2) //constructor body } }; eg: gamma(int x, int y, float z) : a(x), b(x,z) { //write action or print stmt }

Chapter Over. Thank you. See u soon.