L4: Inheritance Inheritance Definition Example Other topics: Is A Test, Reasons for Inheritance, C++ vs. Java, Subclasses and Subtypes 7 Forms of Inheritance Discussions Chapter 8 and 10 of Budd. SFDV4001 / L4 - Inheritance / 1
Definition The property that instances of a child class (or subclass) can access both data and behaviour (methods) associated with a parent class. Review Chapter 8 and 10 of Budd. SFDV4001 / L4 - Inheritance / 2
Example: Syntax class A { public: int getaval(); int getbval(); protected: int b; private: int a; class B: public A { public: int dostuff(){ getaval(); getbval(); b = 2; a = 3; ; SFDV4001 / L4 - Inheritance / 3
class Base { Example:Derived Classes public: virtual void print() { cout << Base class << endl; class Derived:public Base { public: virtual void print() { cout << Derived class << endl; SFDV4001 / L4 - Inheritance / 4
Example: Derived Classes void func(base &b) { b.print(); int main() { Base b; Derived d; func(b); func(d); SFDV4001 / L4 - Inheritance / 5
The Is-A Test To test whether inheritance is appropriate, it should make sense to say B is an A. SFDV4001 / L4 - Inheritance / 6
Reasons for Inheritance As a means of code reuse. As a means of concept reuse (like an interface). SFDV4001 / L4 - Inheritance / 7
C++ versus Java All classes in Java inherit from a common base class (Object). Advantages: Disadvantages: C++ does not. SFDV4001 / L4 - Inheritance / 8
Subclasses & Subtypes Subtypes are subclasses where the subclass can be substituted for the parent class. Most statically typed languages assume all subclasses are also subtypes, but this is not necessarily true. SFDV4001 / L4 - Inheritance / 9
Forms of Inheritance Specialisation Specification Construction Extension Limitation Variance Combination (discussed in Lecture 5) SFDV4001 / L4 - Inheritance / 10
Inheritance for Specialisation Child class is a specialised variety of the parent class. This is the ideal form of inheritance. SFDV4001 / L4 - Inheritance / 11
Example Class Rectangle { public: virtual void draw(){ virtual void setsize(int _width, int _height){ private: int x,y, width, height; Class ColouredRectangle : public Rectangle { public: virtual void draw(){ ; private: Rectangle::draw(); fill(x,y,width,height,colour); RGB colour; SFDV4001 / L4 - Inheritance / 12
Inheritance for Specification Can use inheritance to guarantee that classes maintain a common interface. In Java, we do this by using interfaces. In C++, we use abstract classes. SFDV4001 / L4 - Inheritance / 13
Example class Abstract { public: virtual void dosomething() = 0; ; class Concrete : public Abstract { public: // must define dosomething otherwise // is also an abstract class virtual void dosomething(); ; SFDV4001 / L4 - Inheritance / 14
Inheritance for Construction A class inherits from another to simplify implementation even though there may be no logical connection between the two. SFDV4001 / L4 - Inheritance / 15
Example public class Stack extends Vector { public Stack() { public Object push(object item) { addelement(item); return item; public synchronized Object pop() { Object obj; int len = size(); obj = peek(); removeelementat(len - 1); return obj;... SFDV4001 / L4 - Inheritance / 16
Inheritance for Extension A child class only adds new behaviour to its parent it does not override any behaviour. SFDV4001 / L4 - Inheritance / 17
Inheritance for Limitation Occurs when the behaviour of the subclass is more restrictive than the behaviour of the parent. In this case, we would have to override methods in the parent to provide more limiting behaviour. SFDV4001 / L4 - Inheritance / 18
Example Class Rectangle { private: int width, height; public: virtual void draw(){ virtual void setsize(int _width, int _height){ Class Square : public Rectangle { public: virtual void setsize(int side){ setsize(side, side);; virtual void setsize(int _width, int _height) { if (_width!= _height) Error! Rectangle::setSize(_width, _width); SFDV4001 / L4 - Inheritance / 19
Inheritance for Variance Two or more classes have similar implementations but no clear hierarchical relationship. Eg class Mouse and class Tablet SFDV4001 / L4 - Inheritance / 20
Constructors In C++ default parent constructors are always called if they exist, unless otherwise specified. If no default constructor exists, child class must explicitly call the appropriate parent constructor. Constructors are never virtual. SFDV4001 / L4 - Inheritance / 21
Example class A { public: A(){cout << A default\n ;; A(int num){cout << A(num)\n ;; class B : public A { public: B(){cout << B default\n ;; B(int num): A(num){ cout << B(num)\n ;; B(char ch) {cout << B(ch)\n ; SFDV4001 / L4 - Inheritance / 22
Destructors In C++, if there is any chance that a class will be derived from, then the destructor should be made virtual. It is generally good practice to make all destructors virtual. SFDV4001 / L4 - Inheritance / 23
Example class Parent { ; public: virtual ~Parent(){ cout << parent\n ;; class Child: public Parent { ; public: ~Child(){cout << child\n ;; Parent *p = new Child(); delete p; SFDV4001 / L4 - Inheritance / 24
Good and Bad Good: specialisation specification extension Ugly: construction limitation Complicated: combination SFDV4001 / L4 - Inheritance / 25
Benefits of Inheritance Reusability Reliability Code sharing Consistency of interface Rapid prototyping Information hiding SFDV4001 / L4 - Inheritance / 26
Why is it so Cool? class Shape {virtual void draw() = 0;; class Rectangle: public Shape{draw(){ ;; class Circle : public Shape{draw(){ ;; vector<shape *> shapes; shapes.push_back(new Rectangle()); shapes.push_back(new Circle()); shapes.push_back(new Triangle()); for (i=0; i<num_shapes; i++) shapes[i]->draw(); SFDV4001 / L4 - Inheritance / 27
Costs of Inheritance Execution speed Program size Program complexity SFDV4001 / L4 - Inheritance / 28