1 / 31

Inheritance & Classification Hierarchies

Inheritance & Classification Hierarchies. Lecture-8. Classification Hierarchy of building. Building. Public. Domestic. Comercial. Employee Example. Director. Salaried Worker. Hourly Worker. Personal ID Name shareholding Secretary. Personal ID Name annual salary Department.

joshua
Download Presentation

Inheritance & Classification Hierarchies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance & Classification Hierarchies Lecture-8

  2. Classification Hierarchy of building Building Public Domestic Comercial

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

  4. 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

  5. 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

  6. 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

  7. 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

  8. Classification Hierarchy of building Building Public Domestic Comercial

  9. 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

  10. '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

  11. 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

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

  13. 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

  14. 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)‏

  15. 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

  16. 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

  17. 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

  18. 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

  19. 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

  20. 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;} };

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

  22. 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; }

  23. 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; } };

  24. 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

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

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

  27. Constructor Inheritance Example #include <iostream> 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; }

  28. 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; }

  29. Point3D a derived class #include "Point3D.h" #include <iostream> 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; }

  30. 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

  31. 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

More Related