OOP Basics
This stuff is general knowlege and terminology, but placed here for quick look ups.
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
- Interface (Type)
- Implementation (Object)
Loose Coupling - High Cohesion
Coupling: Dependence on another class. Tight coupling is very dependent on other classes and makes a system monolthic and hard to change. Loose coupling leads to classes that can be reused.
Cohesion: A class has high cohesion when it is designed around a set of related functions, and low cohesion when designed around unrelated functions. We want high cohesion.
We strive for 'loose coupling' and 'high-cohesion'. Like everything in software architecture, there are trade offs. Taken to extremes, we'd have an application with every function in a separate class that refuses to talk to other classes. We can't program on the extremes.
Type, Interface, Class, Object
While discussing OOP or Patterns you will hear 'type', 'interface', 'class', 'object' used a lot and understanding the differences is crucial. In some cases these are used interchangebly, and in many cases, they are used incorrectly. You can't help that others will use them incorrectly, but by having a firm grasp on these you will often be able to understand what they meant, and you can prevent adding to the confusion.
Interface:An interface is the set of all signatures (methods and properties) of a type or class.
There is also an 'interface construct' in some languages, such as C#, but generally in this site I will make a distinction between 'interface' and 'interface construct'. Think of an interface purely as being the set of signatures of the class or 'interface construct', and think that this class or 'interface construct' will not be directly instantiated, but will be used to derive subtypes which will define implementations, and be instatiated.
Class: A class is set of methods and properties and their implementations. A class may be derived from a particular interface or from multiple interfaces. A class can be instantiated.
Type: Type is tightly related to our definition of an interface. When we discuss a group of objects that have the same 'type', it means they share a common interface.
Object: An object is an instantiated class that has been called to life, and can be used until it is deallocated.
In general, we think of an interface as a class that has no implementation, can not be instantiated, and is used as the base type for other classes. This may be an 'interface construct', a pure virtual class, or a class with some implementation, but leaving other parts of the implementation to be determined by it's subclasses.
'Type' refers to all classes that inherit from the same interface.
'Class' is the written code that contains full implementation and can be directly instantiated.
'Object' is the result of instantiating a class, and an object can hold data in properties and can execute it's methods.
An object may have many types: inherits from many interfaces.
Very different objects may share the same type: truck and unicycle can inherit from 'vehicle' interface.
More Terminology:
Supertype: a base class or interface from which other classes will inherit.
Subtype: a class that inherits from a supertype. A subtype is more refined and more granular than its supertype.
Dynamic binding: (aka: late binding) The runtime association of a request to an object and one of its operations.