Concepts of Object-Oriented


 

When reading the following concepts of object-oriented (OO) technology, using a simple bank account example, you will see what an intuitive and powerful technology it is and how it facilitates your development of complex systems 

Object

  •  A software component that has state (also known as variables) and behavior (also known as methods).
  • The components are not based on procedures, like conventional programming

Example: A bank account may have State: balance and account name and Behavior: credit, debit, and transfert.

Methods

  • A piece of code that defines the behavior of an object. 
  • Contained within an object, but made available to other objects so that the chosen behavior can be performed on that object

Example: Credit and debit

Messages 

  • The way objects interact with each other. 
  • The name of the message receiver, the method, and any parameters the method requires. 

Example: If you want "Account A" to be credited by $100, you send the message credit with a parameter of $100 to the object "Account A." Providing "Account A" understands the message, the action will be carried out.

Classes 

  • Groups similar objects. 

Example: "Account A" and "Account B" are instances of the same class providing they have the same state, the same behavior, and respond to those methods in the same way. 

Instance

  • An object that belongs to a class.

Example: "Account A" is an instance of the Account class.

Subclass

  • A specialization of a class. 

Example: If you want an account that has all the behavior and state of the account class but a higher rate of interest, use the template of the standard account to create a Gold Account with the additional state and behavior that define the new account as a Gold Account.

Inheritance

  • A mechanism by which a subclass can gain all the state and behavior from another class that is higher up the hierarchical tree. 
  • The new class does not have to be programmed from scratch and the subclass will be consistent with the super-class (the class above the new class in the class hierarchy) so fewer errors occur. 

Example: The Gold Account will inherit all the methods and variables from the Account class, but to differentiate, can then add its own variables and methods. For example, with a Gold Account, you may be permitted an overdraft.

Encapsulation

  • A procedure where the variables of one object only respond to messages sent by other objects if the original object understands that behavior. 
  • Example: Account A will respond to a "debit" message from Account B because it has the debit method as parts of its behavior. If Account B sent the message "minus," then Account A would not respond because the minus message is not part of its behavior.
  • The variables cannot be altered directly (except by the object itself) which prevents an object's variables from being corrupted by another object. It also means that other objects do not need to know about the internal configuration of an object, just the methods it can use to access it. 

Example: An instance of a standard account can only have its balance altered if another object sends it the credit or debit message. If you decide to change the way an account is debited (maybe you want people to be charged a fee for debits of less than £10), you just alter the standard account class. Other objects still use the debit method without knowing that the way it works has changed.

Abstraction

  • A reusable template for building customized software. 

In order to follow the same format, include an example.

Polymorphism

  • Being able to send the same message to different objects, even though the objects may respond differently.
  • Objects are very independent of each other and new objects can be added without extensive changes to existing objects.
  • The system can be altered/extended to meet new needs very easily.
Example: The rate of interest may be calculated differently for the Account class and the Gold Account class but you could conceivably have a button to "calculate interest" on the GUI that can be applied to both classes of account (they will both respond to this message but in different ways).