2CPP12 - Method Overriding


Published on

This is an intermediate conversion course for C++, suitable for second year computing students who may have learned Java or another language in first year.

Published in: Software, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

2CPP12 - Method Overriding

  1. 1. METHOD OVERRIDING Michael Heron
  2. 2. Introduction • In the last lecture we talked about method overloading. • In this one we are going to talk about method overriding. • The two are often confused, but are entirely different systems. • One is based on the idea of unique identification of methods. • The other is based on being able to specialise behaviours through inheritance.
  3. 3. Method Overriding • When we override a method, we specialise it as the child class as part of an inherited relationship. • The usual rules of virtual functions apply here for C++. • Usually what we do here is subtly alter the functionality and then pass the results of our specialisation onto the parent method.
  4. 4. Method Overriding • The chain of invocation for a method in C++ depends on its virtuality. • If it is not virtual, the base method gets called always. • If it is virtual, the most specialised implementation gets called. • For virtual methods, we then have control over the chain of invocation. • We can decide how invocations filter up and down the chain.
  5. 5. The Chain of Invocation • This is a fancy way of saying ‘which functions get executed when’. • A well over-ridden function will tend to pass function calls back onto their parent classes. • There is nothing to stop you entirely handling a function in the most specialised instance. • It’s generally not very good design. • Violates the principles of encapsulation.
  6. 6. Employee (Base Class) class Employee { private: int payscale; public: int query_payscale(); void set_payscale (int p); virtual bool can_hire_and_fire(); virtual bool has_authority (string); }; bool Employee::has_authority (string action) { if (action == "work") { return true; } return false; }
  7. 7. Clerical (Derived Class) class Clerical: public Employee { public: bool can_hire_and_fire(); void file_papers(); bool has_authority(string str); }; bool Clerical::has_authority (string action) { if (action == "paperwork") { return true; } return Employee::has_authority (action); }
  8. 8. Manager (Derived Class) class Manager : public Employee { public: bool can_hire_and_fire(); bool has_authority (string); }; bool Manager::has_authority (string action) { if (action == "managing") { return true; } return Employee::has_authority (action); }
  9. 9. Main Program int main(int argc, char** argv) { Employee *cler = new Clerical(); Employee *man = new Manager(); string actions[3] = {"paperwork", "work", "managing"}; for (int i = 0; i < 3; i++) { cout << "Managers authority for " << actions[i] << " " << man->has_authority (actions[i]) << endl; cout << "Clerical authority for " << actions[i] << " " << cler->has_authority (actions[i]) << endl; } }
  10. 10. Overriding in Java • Java provides a special keyword, super to refer to a parent class. • C++ offers no equivalent due to its support of multiple inheritance. • Otherwise works identically: • return super.has_authority (action); • Overriding simplified by virtue of no virtual functions.
  11. 11. Notes on the Chain • No need for parent classes to define the method. • Will be passed back up to the most specialised parent that does. • Scope resolution is used to redirect function calls to parent classes. • You can bypass parents if you like, but this is not good practise.
  12. 12. Overriding and Encapsulation • Does overriding break encapsulation? • Some say it does, and that child classes should not make calls to parent classes in this way. • Inheritance is about specialisation. • Not replacement. • Properly used, over-riding enhances encapsulation. • We don’t need to worry about how our methods will be interpreted, just that they will be.
  13. 13. Overloading versus Overriding • Overriding only works as a mechanism of inheritance. • You can only override a parent’s implementation. • You can’t override a method in the class in which it is defined. • Overloading can work in conjunction with overriding. • The has_authority method we are using is overridden. • We could provide a second syntax for that that would be overloaded. • This has implications for clean object design.
  14. 14. Common Overriding Errors • Mis-spelled function names • Won’t be picked up when invoked. • Mismatched parameter lists • Will overload rather than override. • Failing to maintain the chain of invocation. • Make sure you always pass it on. • Make sure you always pass it on correctly. • One of the things that super does is resolve to the parent class without you needing to specify it. • If you change an inherit chain in C++, you also need to update all references.
  15. 15. Why Override Functions? • Information hiding ensures we don’t have access to private data fields. • In many cases, we simply can’t configure objects without overriding. • No need to excessively duplicate functionality. • We handle what is new, and let the parent class handle the rest. • Properly distributes authority through an object inheritance chain.
  16. 16. Binding and Polymorphism • All of this is made possible through the use of inheritance and polymorphism. • It’s worth spending a few minutes talking about how this is actually done. • C++ makes a distinction between the static type of an object and its dynamic type. • Shape *myShape = new Circle(); • myShape has a static type of Shape • myShape has a dynamic type of Circle
  17. 17. Binding and Polymorphism • The static type is determined at compile time. • The dynamic type is determined at run-time. • And it is determined on an ongoing basis. • This process is known as dynamic binding. • Dynamic binding has a toll. • Virtual function lookups at runtime are expensive. • Virtual functions must be placed and maintained in a virtual function table. • Static binding is used for functions that are not virtual. • The function to be executed is decided at compile time and is not changed.
  18. 18. Binding Styles • There is a trade-off between static and dynamic. • Java makes all methods virtual by default. • Static binding is more efficient. • The compiler can do all sorts of tricks to make it work more efficiently. • Dynamic binding is more flexible. • The interface is consistent but the implementation will change.
  19. 19. Static Methods in Java • Static methods in Java are the Java implementation of static binding. • Static binding is used for static method calls. • This is why Java has problems with people calling non- static methods from static contexts. • The non-static method is virtual. • The static method is static. • You can’t even override a static method in Java.
  20. 20. Summary • Overriding is separate and distinct from overloading. • Overloading is in addition • Overriding is instead of • Maintaining the chain of invocation is important. • Overriding is predicated on effective use of inheritance and polymorphism.