Inheritance

  • Characteristics of good software
    • Object-oriented independent characteristics
    • Object-oriented dependent characteristics
  • Principles leading to good software
  • How to achieve
  • Concept
  • Errors
  • Inheritance vs. delegation
  • Abstract classes
  • Interfaces

Characteristics of good software

Object-oriented independent characteristics

  • correctness: does program what it is supposed to do?
  • robustness: can program handle wrong user input / circumstances?
  • efficiency: does program optimally use resources

Object-oriented dependent characteristics

  • Re-usability
  • Maintainability
  • Extensibility
  • Compatibility

Principles leading to good software

Modularity: High coherence, low coupling (Split) Responsibility: Every class/method has 1 responsibility Locality: Responsibility is located at one place

Can easily be investigated e.g. by using class diagrams

How to achieve

Specialization / Generalization : create subclasses and superclasses. Suitable for classes where all members of subclass have a is a-relation with the super class (exclusive class hierarchy)

Concept

Declared (static) type: declared type of a variable Actual (dynamic) type: Actual type of a variable.

Both can differ. Used for dynamic binding which is the basis mechanism to allow polymorphism (different behavior for different subtypes).

Errors

Compiler checks syntactic logic. Compile errors occur if - method for class does not exist for declared class - ‘side’ cast

Virtual machine checks actual type of variable. Exception is thrown if - class cast (downcast) is not possible for variable

Inheritance vs. delegation

Inheritance means that a class inherits all attributes and methods of one superclass. This raises two issues.

  1. A method can not be deleted.
  2. Also, a instance of a class might take roles of different classes (Person->Student and Person->lecturer).

For both cases, it is better to use delegation instead of inheritance. Delegation means that a class receives a instance of another class as attribute. In other words, a class has a has a-relationship with the delegate class.

Regarding 1. Instances of different classes can point to the same delegated attribute instance Regarding 2. We control our delegated class by only implementing those methods that we want to exist

Abstract classes

For dynamic binding specification of method is necessary. However, implementation of dummy method is not. Therefore use abstract class. Has specification of method without body. Can also have implemented methods and attributes. But can not be instantiated. Use only if subclass relation is total (all subclasses cover whole spectrum of super class.

Interfaces

Interfaces are contracts that describe the relation between classes. It is a promise to the virtual machine that a certain class will have a implementation of a certain method. Interfaces have only abstract method signatures but, other than abstract methods, no concrete methods and attributes. They might have constants.

Classes can implement several interfaces. And importantly, a interface can be used for declaring a variable, parameter or return value.

By using interfaces, we separate the specification of a method from its implementation. This is a useful principle for OO-designed implementations.