Inheritance What is inheritance?

Slides:



Advertisements
Similar presentations
Operator overloading redefine the operations of operators
Advertisements

OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Copyright 2006 Oxford Consulting, Ltd1 February C++ Inheritance Data Abstraction and Abstract Data Types Abstract Data Type Encapsulated data.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Topics Recap of the Object Model Inheritance Polymorphism – virtual functions Abstract classes, Pure virtual functions Design issues UML examples Templates.
OBJECT-ORIENTED PROGRAMMING. What is an “object”? Abstract entity that contains data and actions Attributes (characteristics) and methods (functions)
Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Software Testing and Quality Assurance Lecture 28 – Testing Class Hierarchies.
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Inheritance and Polymorphism CS351 – Programming Paradigms.
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
Chapter 12: Adding Functionality to Your Classes.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Polymorphic support in C# Let us now examine third pillar of OOP i.e. Polymorphism.. Recall that the Employee base class defined a method named GiveBonus(),
Object Oriented Programming in C++ Chapter 6 Inheritance.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 OOP - An Introduction ISQS 6337 John R. Durrett.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Overview of C++ Polymorphism
1 Inheritance Inheritance is a natural way to model the world in object-oriented programming. It is used when you have two types of objects where one is.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
CSC241 Object-Oriented Programming (OOP) Lecture No. 14.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
CPS120: Introduction to Computer Science Lecture 16A Object-Oriented Concepts.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Object-Oriented Programming Concepts
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Module 5: Common Type System
Polymorphism.
Inheritance & Polymorphism
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Week 4 Object-Oriented Programming (1): Inheritance
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Understanding Inheritance
Inheritance Basics Programming with Inheritance
Inheritance Constructors & Overriden Functions
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Computer Programming with JAVA
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Adapter Design Pattern
Object-Oriented Programming
Overview of C++ Polymorphism
CPS120: Introduction to Computer Science
Lecture 10 Concepts of Programming Languages
COP 3330 Object-oriented Programming in C++
Chapter 11 Class Inheritance
Object-Oriented Programming (OOP) Lecture No. 27
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
C++ Object Oriented 1.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Inheritance What is inheritance? An “Is a” relationship between two classes of objects in which a class (the derived class) inherits all members of another class (the base class) Can be used to develop a hierarchy of classes based on abstractions in a top down fashion, e.g. the Location-Point-Circle-Arch hierarchy introduced before It is also needed for code refinement, expandability, and reuse by a) Refining the implementation of a class by building a refined derived class, were some member functions are redefined, b) Expanding the functionality of the a class by adding new functions c) Reusing the code in a base class in several derived classes

Inheritance Types of inheritance Types of inheritance can be defined depending on the access attributes of class members as viewed in the derived class, each class may have private members (can only be accessed by class members or friends), protected members (as private but can also be accessed by derived classes) public members ( can be accessed by all) The types of inheritance are: a) Public derivation, which is the default derivation type for structs class D: public B { // define class D…}, also struct D: B{// define D} leaves the access level unchanged, i.e., public members of B are public in D protected members of B are protected in D private members of B remain private to B (can not be accessed in D but they are still inherited in D)

Inheritance Types of inheritance (cont.) b) private derivation, which is the default derivation type for classes, class D: private B { // define class D… }, or class D: B {// define D..} converts the public and protected members of the base class into private members of the derived class public members of B are private in D protected members of B are private in D private members of B remain private to B c) protected derivation, class D: protected B { // define class D… } protected members of the derived class public members of B are protected in D protected members of B are protected in D In all cases, note that private members of B remain private to B

Inheritance Types of inheritance (cont.) suppose we have the following members of a base class class Parent{ private: d1;m1; protected: d2;m2; public: d3; m3;} An object of type Parent can access the public members d3 and m3 class child_1: public Parent{private: d4;m4; protected: d5; m5; public: d6;m6;} Members of child1 have also direct access to d2,m2, d3, and m3, the protected and public members of Parent An object of type child1can access d6, m6, d3, and m3

Inheritance class child2: private Parent{private:d7;m7; protected: d8;m8; public: d9;m9;} Members of child2 also have direct access to d2, d3, m2, and m3 of members of Parent same as members of Child_1 An object of type child2 can only access d9, and m9 class child3: protected Parent{private: d10; protected d11; public: d12; m12;} Members of child3 and classes derived from child3 can have also direct access to d2,m2, d3, and m3 of Parent Objects of type child 3 can access only d12, and m12 Note that d1, and m1 are private to Parent and remain private to Parent

Inheritance Examples in inheritance class B{ int a; public: int b,c; int Bfun(void);}; class X: private B {int d; public: B::c; //c inherited as private now is public int Xfun(void){ int I = Bfun(); b = 100; // OK, b is declared as public in B a = 10; // Error a is declared as private in B} Friend Relations under inheritance The access privilege of friend functions of a class is similar to that of the member functions of the class. Friend functions of a derived class can access the inherited protected and public members of the base class. Class Base {int x; friend int fbase(void); protected: float g; public: fun();..}; class X: private Base { int a,b; friend int xf(void); public: X(int); fun();…}; int xf(void){ X z(10); z.a = 100; z.g = 0.01; // OK z.x = 100; //error.. }

Inheritance Friend Relations under inheritance (cont.) A friend function to the base class can access the base class members inherited in the instance of an object of the derived class, int fbase(void){ X z(100); z.x = 10; z.g = 0.001; //OK z.a = 100; // Error….} Multiple Inheritance A class can have several base classes class B1{int x,y; public: B1(int I):x(I){y = 0;} }; class B2{int x; public: B2(int I){x = I;} }; class X : public B1, private B2{int a,b; public: X(int I, int j): B1(I), B2(j) {a=b=0;} }; X z; // the instance of z contains B1::x, B1::y, B2::x, X::a, and X::b

Inheritance Multiple Inheritance (cont.) Suppose that both classes B1 AND B2 are derived from the same base class B, i.e., class B{ int u,v; public: B(int i){u=v=i;} }; class B1: public B {……. public: B1(int I):B(I),x(I){y=0;} class B2: public B {…… public: B2(int I): B(I) {x = I;} X z; // the instance of z contains B1::x, B2::x, X::a,b, and two different copies of the inherited data members of B, one is initialized thr’ B1 and the other is initialized thr’ B2. Virtual Base Class is used to prevent the above problem by having only one copy of the base inherited by any derived class in complex class hierarchy, class B1: virtual public B{….}; class B2: virtual public B{…} The virtual base class is initialized by its most derived class in the class hierarchy, the initialization of B from B1 and B2 are completely ignored