Inheritance & Classification Hierarchies Lecture-8.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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.
Inheritance & Classification Hierarchies Lecture-8.
Inheritance In C++  Inheritance is a mechanism for building class types from other class types defining new class types to be a –specialization –augmentation.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Inheritance Inheritance Reserved word protected Reserved word super
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
Inheritance The objectives of this chapter are: To explore the concept and implications of inheritance Polymorphism To define the syntax of inheritance.
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,
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to Data Structures, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Object Oriented Programming Concepts.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Inheritance using Java
Abstract Data Types Using Classes Lecture-5. Abstract Data Types Using Classes Representing abstract data types using C++ We need the following C++ keywords.
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
1 What is Inheritance? zThe mechanism of deriving a new class from an old one is called inheritance zClasses organised into a ‘classification hierarchy’
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes & Objects Lecture-6. Classes and Objects A class is a 'blueprint' for all Objects of a certain type (defined by ADT) class defines the attributes.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter -6 Polymorphism
Classes, Interfaces and Packages
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
Inheritance CMSC 202, Version 4/02.
7. Inheritance and Polymorphism
Inheritance and Polymorphism
Object-Oriented Programming
Polymorphism.
Object Oriented Programming in Java
Object Lifetime and Dynamic Objects
Lecture 9-2: Interacting with the Superclass (super);
An Introduction to Inheritance
ATS Application Programming: Java Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Inheritance Chapter 5.
Class Inheritance (Cont.)
Inheritance Basics Programming with Inheritance
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Comp 249 Programming Methodology
Learning Objectives Inheritance Virtual Function.
Java Programming Language
Lecture 22 Inheritance Richard Gesick.
Week 6 Object-Oriented Programming (2): Polymorphism
Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
Computer Programming with JAVA
Inheritance Inheritance is a fundamental Object Oriented concept
Overriding Methods & Class Hierarchies
Object-Oriented Programming (OOP) Lecture No. 22
Fundaments of Game Design
Inheritance.
Review of Previous Lesson
Inheritance -I.
Object-Oriented PHP (1)
Chapter 7 Inheritance.
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Inheritance & Classification Hierarchies Lecture-8

Classification Hierarchy of building Building Comercial PublicDomestic

Director Personal ID Name shareholding Secretary getId GetName Getshareholding GetSecretary Salaried Worker Personal ID Name annual salary Department getId GetName Getsalary getDepartment Hourly Worker Personal ID Name hourly rate Qualification getId GetName Getrate getqualification Employee Example

Inheritance & Classification Hierarchies Inheritance is one of the most powerful features of O-O programming By organising classes into Classification Hierarchies we can add extra dimensions to encapsulation by grouping ADT's and enables classes to inherit from other classes thus extending the attributes and methods of the class which inherits The inheriting class may then also add extra functionality and attributes to produce a new object

Terminology of inheritance The following terms are used to describe the different inheritance properties of an object Derived Class or Sub Class or Child Class  A Class which inherits some of its attributes and methods from another class Base Class or superclass or Parent Class  A Class from which another class inherits Ancestor  A Classes ancestors are those from which its own superclass inherit Descendant  A classes descendants are those which inherit from its superclasses

Why do we need Inheritance ? What is the purpose of inheritance and why should we wish to inherit the attributes and methods of other Objects ? There are two main reasons for using inheritance in O-O which are  Specialisation Extending the functionality of an existing class  Generalisation Sharing commonality between two or more classes

Classification Hierarchy The product of inheritance is a classification Hierarchy This is a relationship between classes where one class is said to be 'a kind of' other class As the class hierarchy is traversed from top to bottom we move from generalisation to specialisation of classes This is done by adding functionality to extend what exists at each level of the class hierarchy from the base class

Classification Hierarchy of building Building Comercial PublicDomestic

Class Hierarchy Diagram The base of this diagram is the Building class From this the Commercial, Public and Domestic inherit the base features ( what could these be ?)‏ After this there are specialised versions of the classes using the 'kind of' relationship where Public classes such as Hospital and Library inherit the base classes of a Building and then add special features specific to the type of object The same then applies for the other classes This tree runs from a generalisation of the base class to a specialisation of the actual object

'a kind of' or 'a part of' Each level of a classification hierarchy contains more specific types of class each one of which must be 'a kind of' the class from which it inherits This distinction is important as the difference between 'a kind of' and 'a part of' is very different The distinction between different Objects and the same Objects with different states This mean analysis to see if difference of Objects are dependent upon the Object type or a state of that Object  For Example Equilateral Triangle and Isosceles Triangle are inappropriate Object classifications as they are both really triangles (with just the angle between vertices being different) which we could store in a general triangle class

What Do Objects Inherit ? A class does not contain any state values, only a 'blue print' for what value are to be contained Therefore a 'derived class' inherits all of the attributes from the 'base class' A derived class is by definition identical to the base class, but it can be built on to extend and modify the base class Objects of the derived class do not inherit anything from the objects of the base class As far as objects are concerned there is no hierarchy

Example Line +startPosition: +endPosition: +Draw() Line Object start = (x,y)‏ end = (x,y)‏ draw(star,end)‏ ColoredLine +color: +setcolor() Line Object start = (x,y)‏ end = (x,y)‏ color=? setcolor(col)‏ draw(star,end)‏

Example Explained The example shows two classes line and ColouredLine Because ColouredLine contains all the attributes and methods of Line it can be a derived class ColouredLine then extends the base class line to add the colour attribute and the method setcolour()‏ However ColouredLine still understands the methods draw and the attributes start position and end position The Line class, however, does not now the attribute colour

Specialisation – extending functionality Specialisation allows one base class to be extended to produce a special new class to do a specific function. For example the line base class may be extended to produce the coloured line as in the previous example or to produce an new line class called dottedline. This use of base classes allows the re-use of software components by declaring a base class and then creating new objects which extend the base class to the desired need. It is possible to create as many derived classes as required from one base class. It is also possible to inherit from multiple classes (but this will be dealt with in another lecture)‏

Generalisation - sharing commonality When analysing a problem we sometimes find the following  Some Objects share the same attributes  Some Objects share the same methods  Some Objects require similar functionality So what is the best way of partitioning the problem ?  Lots of Classes some of which share the same attributes and methods  Fewer classes and avoid duplication but some Objects will have redundant data

Abstract Classes Sometimes the base class does not contain enough data to instantiate an object In this case the base class can be thought of as an 'abstract class' And the derived class will inherit the behaviour of the abstract base class and enough additional information to instantiate an object. However the base class doesn't necessarily have to be abstract

Inheritance Using C++ Base Class has one attribute an integer x Two methods to set and get the value of x This class can be instantiated in the usual way and the methods called on the class At present there is no inheritance as we only have one class class BaseClass { private : int x; public : void setX(int x_in){ x=x_in;} int getX(){ return x;} }; First we will define a simple Class called BaseClass

Adding a derived class We can extend the BaseClass by adding a new attribute y and methods setY and getY The derived class will now have two attribute X and Y and associated methods The syntax for a derived class to inherit the base class is as follows class derived_class_name: public/private base_class_name

Adding a derived class The derivation type may be either public or private Public is the most common as it allows  Objects of the derived class access to the public parts (usually methods)‏  as well as the public parts of it own class Private is less common, and means that the derived class object may only use the methods defined in the derived class, not those inherited from the base class. However derived class methods may utilise base class methods to implement their own behavior

DerivedClass Example The following DerivedClass definition demonstrates the syntax for inheritance class DerivedClass : public BaseClass { private : int y; public : void setY(int y_in){y=y_in;} int getY() { return y;} };

DerivedClass Example The following code demonstrates the use of the two classes #include void main(void)‏ { BaseClass base_object; DerivedClass derived_object; base_object.setX(7); derived_object.setX(12); derived_object.setY(1); }

Accessing inherited attributes In the previous class the derived object cannot directly access the attributes of the base class This is because they are in the private part of the class definition The derived class can only use the methods in the public definition of the base class This means the following code in the derived class would not compile void DerivedClass::xEqualsy()‏ { x=y; }

The protected keyword To allow a derived class to access the attributes of the base class the “private” keyword is replaced with “protected” keyword class BaseClass { protected : int x; class DerivedClass : public BaseClass { void xEqualsy()‏ { y=x; } };

Inheriting Constructors A derived class will always inherit the constructor of the base class A derived class will also have it's own constructor The base class constructor is called first followed by each derived class going down the hierarchy tree This is because each class constructor must allocate the memory required for the attributes of the class If the constructor for the base class has parameters then the derived class must have a constructor of the same type so that it may follow up the hierarchy tree

Inheritance Example Point2D x-coordinate y-coordinate Point2D() ~Point2D() GetX() GetY() Print() Point3D x-coordinate y-coordinate z-coordinate Point3D() ~Point3D() GetX() GetY() GetZ()‏ Print() Point2D Point3D

Constructor Inheritance Example (1)‏ class Point2D { protected : float x; float y; public : Point2D(float xin,float yin); ~Point2D(); float GetX(void); float GetY(void); void Print(void); }; Point2D.h

Constructor Inheritance Example #include using namespace std; #include "Point2D.h" float Point2D::GetX(void)‏ { return x; } float Point2D::GetY(void)‏ { return y; } Point2D::Point2D(float xin,float yin)‏ { x=xin; y=yin; } Point2D::~Point2D()‏ {cout << "destructor called for Point2D"<<endl;} void Point2D::Print( void)‏ {cout <<"["<<x<<","<<y<<"]"<<endl; }

Point3D a derived class #ifndef __POINT3D_H__ #define __POINT3D_H__ #include "Point2D.h" class Point3D : public Point2D { private : float z; public : Point3D(float xin, float yin,float zin); ~Point3D(); float GetZ(void); void Print(void); }; #endif; }

Point3D a derived class #include "Point3D.h" #include using namespace std; Point3D :: Point3D(float xin, float yin, float zin) : Point2D(xin,yin)‏ { z=zin; } Point3D ::~Point3D()‏ { cout << "destructor called for Point3D"<<endl; } float Point3D ::GetZ(void)‏ { return z; } void Point3D::Print(void)‏ { cout <<"["<<x<<","<<y<<","<<z<<"]"<<endl; }

Derived Class Explained The Constructor definition must explicitly refer to the name of the base class constructor It must include the inherited parameters by using the : operator The Point3D constructor has to re-iterate the inherited constructor parameter and the : operator is used to indicate inheritance However in this case the parameters xin and yin are not used in the derived class only the base class

Inheriting destructors Derived Classes also inherit the base classes destructors However destructors are called in reverse order to the constructors So all the derived classes destructors are called first And the base class destructor is called last As destructors have no parameters there is now problem with the definition of correct parameter lists