If you are going to bother inheriting from a class, then you should weigh the cost of your code complexity against it. It is definitely worth it sometimes
If you wrote both the class and sub-class, then you're likely over engineering the problem (unless you have a sufficiently large program you're working on)
// Scorable.swift
import Foundation
protocol Scorable {
var totalScore : Int {get set}
}
// ScorableViewController.swiftimport UIKit
class ScorableViewController: UIViewController, Scorable {
//overrides here...
//This class exists to share the following code.
var totalScore : Int = 0
var currentChoice : Selection? = nil
final var choseOption : Bool {
get {
return currentChoice != nil
}
}
final override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
precondition(choseOption, "must choose option before proceeding")
precondition(segue.identifier != nil, "identifier is nil")
precondition(!segue.identifier!.isEmpty, "identifier is empty")
var dest = segue.destination as! Scorable
dest.totalScore = Selection.add(score: totalScore, selection: currentChoice!)
}
}I also frequently use multiple inheritance and what are called "mix-in" classes in python to make more complex customizations reusable.
Some hand-picked examples of well-implemented inheritance in open source:
An example of where inheritance makes great sense is Django CBV (Class-based views), https://docs.djangoproject.com/en/1.11/topics/class-based-vi..., as well as Django REST Framework class views (http://www.django-rest-framework.org/api-guide/views/)
An example of where inheritance in the form of runtime metaprogramming makes sense is SQLAlchemy declarative and Django ORM.
An example where inheritance in the form of mixins make sense would be Django ORM, such as TimeStampedModel in django-extensions https://github.com/django-extensions/django-extensions/blob/....
An example where classes make sense in standard libraries would be (impure) interfaces such as csv.Dialect (https://github.com/python/cpython/blob/6aee6fb/Lib/csv.py#L2...)
An example of where inherited classes make sense in abstraction would be types (https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalch...) and dialects in SQLAlchemy (https://github.com/zzzeek/sqlalchemy/tree/master/lib/sqlalch...)
As a closing note, It works a lot better when the use of inheritance symbolically represents actual standards (SQL, HTTP, and/or standard libraries API's) that developers already are familiar with, rather than abstract concepts meaningful only to the writer.
IMHO OOP is a trap.
In game development though, it is way easier to develop a full fledged class hierarchy. I think it is because OOP is supposed to model a physical world of objects, which is generally what you'll be simulating in games too.