Skip to main content

22.02.18 - Inheritance and Class Diagrams

Can use inheritance/sub type polymorphism for re-use and adaptation Inheritance is one of the most important ideas in the object oriented paradigm. - Underpins many other things

Inheritance: Sub-classing

Inheritance: IS-A relationships.

  • A class can be a specialisation of another class
  • Use extends to get inheritance. bear 'IS-AN' Animal, so it gets everything in Animal and can change it or add to it

Polymorphism

Means many forms - not fixed to specific types

Parametric Polymorphism:

Code works with multiple types, or regardless of types

  • What functional programmers mean
  • Java generics

Ad-hoc polymorphism (function overloading)

  • Same function name, multiple versions differing by parameter types
  • Function may work differently depending upon the type

Sub-type polymorphism (sub classing)

  • Usually what is meant by polymorphism in OO terms
  • Often called inheritance or sub-classing
  • Super-class/base class = Original class
  • Sub-class/derived class = New type

Public, Private, Protected

  • private - Only this class can access this
  • nothing(package) - This class + this package can access this
  • protected - The class + this package + sub-classes can access this
  • public - Anything can access this