Object-oriented language

From Higher Computing Science
(Redirected from Method)
Jump to: navigation, search

This article is unfinished. Please consider joining and adding to this article. Read about Page layout beforehand.

Key points

  • Object-oriented programs are designed around objects rather than sequences of instructions
  • Each type of item used in a OO program is described using code called a class
  • A class has a list of attributes and a list of methods
  • Each class can be used as the blueprint to create an object, for example the code in a class called Square could describe the attributes and methods of a square, and each square the program uses would be created as an object
  • Objects are closed off from other areas of the program – the design of the object determines which attributes and methods can be called from other parts of the program. This is called encapsulation.
  • Each class can be used as the base code for another class, and the programmer can add on new attributes and methods. For example, the Shape class could have two subclasses called Square and Triangle.
  • Creating a subclass from an existing class is called inheritance


Object-oriented languages are designed to make it easy to represent real-life objects in a program. This is very different from procedural programming because the subprograms and modular approach of OOP (Object-oriented Programming) is to separate code into objects, rather than just blocks of code.

To create a program in an OOL, time must be taken to design the classes. Classes are descriptions of each object. A class consists of attributes and methods.

An attribute is a single item of data attached to an object (like a variable, but related to each object in a program) A method is a function or procedure attached to an object.


One of the major advantages of using classes is that each class can be used to create many objects of the same design. Not only this, but the design can be built on using a subclass. A subclass takes all the attributes and methods of a class and adds to them. This means that programs can have common code for classes that have similarities, for example, a shape class and a square/circle/rectangle class.

Inheritance is when classes automatically gain attributes and methods from a subclass. The example below has a Shape class, with two subclasses, Circle and Square, that both inherit attributes (area, xcoordinate, ycoordinate, colour) and methods (init, change_colour, change_position).

The Shape class is extended with new attributes and methods to suit each shape.

Example of using subclasses to inherit code

Example of an Object-oriented design

A student is set the challenge of writing a computer program for playing "higher or lower" with a deck of cards.

Analysis and design


In an object-oriented program, the analysis and design stages are looking to create the classes needed for the game. A class is any item needed for the game that has its own set attributes and methods.

  • A player
  • A deck of cards
  • Each card
  • The game

For each of the classes, a list of the attributes and methods should be created.

Class Attributes Methods
Player Name, winning streak Create, award point
Deck Cards in the deck Create, shuffle, take card from deck
Card Suit, value Create, show value of card
Game Player1, Player2, Deck of cards Create, start game

There are many different solutions that the programmer could come up with. This simple game can be implemented using classes, giving a distinct set of advantages:

  • Once a card class is created, the same class can be used for all 52 cards simply by creating them with different suit and values.
  • Two players can be created from the same class. This means each player will store their own name and winning streak.
  • A deck is an array of Cards. The programmer can shuffle and take a card from the deck. This code is reusable in any other card game.
  • The game class design creates the objects needed for the game, and controls the game


Implementing can be carried out in any OOL. Below is an implementation of the code in Python.


Testing of classes can be easier than testing blocks of procedural code. Once a class has been tested, it can be re-used without testing elsewhere in a program. Code re-use also cuts down on the possibility of an incorrect line of code.

Encapsulations also makes sure that each class can only be accessed in a controlled fashion. Attributes inside a class can be protected from access from outside the class (private attributes). Methods can be used to control the access to data stored inside each object.


Classes can be represented as diagrams, like the simple class diagrams on this page, or more complex UML class structure diagrams. This makes it easy for programmers to see the relationship between classes.


Maintaining classes should be easier in an OOL because each class can be tested and updated separately.


Further information

Test yourself

Teaching resources