1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’

Slides:



Advertisements
Similar presentations
Dynamic Polymorphism Lecture-11. Method Polymorphism In a monomorphic language there is always a one to one relationship between a function name and it’s.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
C++ Inheritance Gordon College CPS212. Basics OO-programming can be defined as a combination of Abstract Data Types (ADTs) with Inheritance and Dynamic.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
1 Object Oriented Programming Development - Week8 z Rob Manton z z Room D104.
C++ Inheritance Systems Programming.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
CS-2135 Object Oriented Programming
Object-Oriented PHP (1)
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,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Lecture 9 Concepts of Programming Languages
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Inheritance using Java
Chapter 12: Adding Functionality to Your Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
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.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Chapter 8 More Object Concepts
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Inheritance in Classes tMyn1 Inheritance in Classes We have used the Box class to describe a rectangular box – our definition of a Box object consisted.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
1 Object Oriented Programming Development - Week7 z Rob Manton z z Room D104.
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.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Information Systems Engineering
Peyman Dodangeh Sharif University of Technology Fall 2014.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
CSE 341, S. Tanimoto Java brief review - 1 Java Brief Review Java’s strengths Object-oriented terminology Inheritance Interfaces An example with inheritance.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
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.
Programming Fundamentals1 Chapter 8 OBJECT MANIPULATION - INHERITANCE.
INHERITANCE : Extending Classes. Rickshaw cart Bus Car Pulled Vehicles Inheritance Inheritance Vehicles Inheritance is the capability of one class of.
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
ISBN Object-Oriented Programming Chapter Chapter
Coming up: Inheritance
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
EEL 3801 Part VII Fundamentals of C and C++ Programming Inheritance.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
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.
MAITRAYEE MUKERJI Object Oriented Programming in C++: Hierarchy / Inheritance.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Inheritance & Classification Hierarchies Lecture-8.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Advanced Programming in Java
Object-Oriented Programming
Week 4 Object-Oriented Programming (1): Inheritance
Understanding Inheritance
Lecture 22 Inheritance Richard Gesick.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Advanced Programming Behnam Hatami Fall 2017.
Lecture 10 Concepts of Programming Languages
Presentation transcript:

1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’ zClasses can inherit attributes and methods from other classes zInheriting class can add extra attributes and or methods of its own

2 What is the purpose of Inheritance? zSpecialisation Extending the functionality of an existing class zGeneralisation sharing commonality between two or more classes zImproved efficiency and greater robustness

3 Terminology zDerived class or subclass or child class. A class which inherits some of its attributes and methods from another class zBase class or superclass or parent class. A class from which another class inherits zancestor. A class’s ancestors are those from which its own superclasses inherit zdescendant. A class’s descendants are those which inherit from its subclasses

4 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

5 Terminology - a classification Hierarchy Generalisation Specialisation Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

6 Terminology - a classification Hierarchy Generalised ‘base class’ Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block

7 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Building (AKO)

8 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block A ‘kind of’ Commercial building (AKO)

9 Terminology - a classification Hierarchy Building CommercialPublicDomestic Office block Factory CathedralHospital Office block Apartment Block Arrow in diagram means ’inherits from’

10 Designing your classification hierarchy: ‘A kind of’ or ‘a part of’? Car Vehicle A car is ‘a kind of’ vehicle car class can inherit from vehicle class

11 Car Wheel Vehicle  A car is ‘a kind of’ vehicle car class can inherit from vehicle class A wheel isn’t ‘a kind of’ car. A wheel is ‘a part of’ a car -this is dealt with by aggregation Designing your classification hierarchy: ‘A kind of’ or ‘a part of’?

12 yNeed to analyse whether differences between objects are dependant on type (such as a house being different to a factory) or state. (different values of the class’s attributes) Building Short Building Tall Building  short building and tall building might vary only in the value of the height attribute - don’t need separate classes Designing your classification hierarchy: Different classes or different states?

13 What do objects inherit? Line Attributes: start position end position Methods: draw Coloured Line Attributes: colour Methods: set colour A ‘coloured line’ is a kind of line the coloured line class inherits all the attributes and methods of the line class and adds attributes and methods of its own An object of the ‘coloured line’ class has all the attributes and methods of the ‘line’ base class as well as the attributes and methods added by the derived class

14 Specialisation Extending the functionality of an existing class eg a coloured line is a specialised kind of line

15 Specialisation A class is both yclosed in that it has an encapsulated, private part which cannot be affected by external manipulation yand open in that it allows itself to be used as part of a larger software unit.

16 Generalisation Sharing commonality between two or more classes xIf we were modelling animals in a zoo would we create a separate class for each animal type? xThis would duplicate attributes such as legs and methods such as getAge() CowWhaleEagleElephant

17 Generalisation xHelpful to place common elements (attributes and methods) in a shared base class and organise problem into an inheritance hierarchy. CowWhaleEagleElephant Animal MammalBird

18 Generalisation xSometimes this leads to the creation of abstract classes which can’t be instantiated directly CowWhaleEagleElephant Animal MammalBird Abstract classes

19 Generalisation xconcrete classes can be instantiated directly CowWhaleEagleElephant Animal MammalBird Concrete classes

20 C++ Syntax zThe colon (:) operator to denote inheritance zPublic and private derivation of object methods from a base class zThe ‘protected’ keyword to allow derived classes to access inherited attributes

21 C++ Syntax class BaseClass { private: int x; public: void setX(int x_in){x=x_in;} int getX(){cout<<x;} } A simple base class with one private attribute x and two public methods

Derived Class 22 class DerivedClass:public BaseClass { private: int y; public: void setY(int x_in){x=x_in;} int getY(){cout<<y;} } A simple derived class with one private attribute y and two public methods

Implementing Derived Class Object 23 main() { DerivedClass obj1; obj1.SetY(10); obj1.SetX(5); obj1.getX(); obj1.getY(); } Obj1 is Derived class object, Which can access the BaseClass Public Methods(SetX, getX)

when the Derived Class Object is Created z Space is allocated (on the stack or the heap) for the full object (that is, enough space to store the inherited data members from the base class and defined data members in the derived class itself.) z The base class constructor is called to initialize the data members inherited from the base class. zThe derived class constructor is then called to initialize the data members added in the derived class z The derived-class object is then usable 24

When the Derived Class Object is Destroyed zWhen the object is destroyed (goes out of scope or is deleted) the derived class destructor is called on the object first zThen the base class destructor is called on the object zFinally the allocated space for the full object is reclaimed 25

Types of Derivation zThe class can be derived in three visibility modes x public xprivate xprotected 26

Public Derivation zWhen a class is derived in the public mode it does not change the mode of the inherited members in the derived class. The public members remain public and protected members remain protected for the derived class in public inheritance. 27

Private Derivation zWhen a class is derived in the private mode it changes the mode of the inherited members in the derived class. The public and protected members become the private members for the derived class. So the inherited members can be accessed only through the member functions of the derived class. 28

Protected Derivation zWhen a class is derived in protected mode it changes the mode of the inherited members in the derived class. The public and protected members become the protected members for the derived class in protected inheritance. So the inherited members can be accessed only through the member functions of the derived class. 29

30 C++ Syntax: public derivation class DerivedClass: public BaseClass { private: int y; public: void setY(int y_in) {y=y_in;} int getY(){cout<<y;} } A derived class. The colon operator means the derived class inherits from the base class

31 C++ Syntax: public derivation class DerivedClass: public BaseClass { private: int y; public: void setY(int y_in) {y=y_in;} int getY() {cout<<y;} } the public derivation means that objects of the derived class can access the public methods and attributes of the base class This is the most usual type of derivation

32 C++ Syntax: public derivation class BaseClass { private: int x; public: void setX(int x_in) {x=x_in;} int getX() {cout<<x;} } class DerivedClass: public BaseClass { private: int y; public: void setY(int y_in){y=y_in;} int getY(){cout<<y;} } main() { BaseClass base_object; DerivedClass derived_object; base_object.setX(7); derived_object.setX(12); derived_object.setY(1); base_object.getX(); derived_object.getY(); derived_object.getX(); return 0; } Object of the derived class can access methods of the derived class and also methods of the base class

33 C++ Syntax: private derivation zclass DerivedClass: private BaseClass z{ zprivate: yint y; zpublic: yvoid setY(int y_in); yint getY(); z} Another derived class - the private derivation means that objects of the derived class can’t access the public methods and attributes of the base class - but the methods of the derived class can! This is the least common type

34 C++ Syntax: the ‘protected’ keyword zAn object of a publicly derived class can access the public methods of the base class, but not the private attributes zChanging the private keyword in the base class to protected makes the attributes available to derived classes

Access Specifiers Access Specifier Accessible from own class Accessible from derived class Accessible from objects from outside the class publicYes protectedYes No privateYesNo 35

Effect of inheritance on the visibility of member 36

37 C++ Syntax: inheriting destructors zConstructors are not inherited. They are called implicitly or explicitly by the child constructor. zThe compiler creates a default constructor (one with no arguments) and a default copy constructor (one with an argument which is a reference to the same type). z But if we want a constructor that will accept an int, we have to define it explicitly

CM Advantages of Inheritance When one class is inherited from another class the code that provides a behavior required in the derived class need not have to be rewritten that is code be reused which increases reliability.

CM Advantages of Inheritance Code sharing can occur at several levels. For example, At a higher level, many users can use the same class. At lower level, code can be shared by two or more classes.

CM Advantages of Inheritance When multiple classes inherit from the same base class, it guarantees that the behavior they inherit will be the same in all classes.

CM Advantages of Inheritance Inheritance permits the construction of reusable software components. Using inheritance one can concentrate on understanding the portion of the new system.

CM Advantages of Inheritance The development time in developing a system will be reduced rapidly.