Jump to content

A-level Computing/AQA/Paper 1/Fundamentals of programming/OOP Techniques

From Wikibooks, open books for an open world

Techniques in OOP

[edit | edit source]

Techniques

  • encapsulation
  • aggregation
* association aggregation
* composition aggregation
  • overriding

Best practices in OOP

[edit | edit source]

OOP is probably the most widely-used programming paradigm today. As a result, there are many different ways it is used, and many ideas on good and bad styles of programming within OOP languages. However, there are three core ideals that are generally agreed to be valuable in most or all OOP situations:

  • Encapsulate your data, only allowing access on a need-to-know basis
  • Where you have the choice: choose Composition rather than Inheritance
  • When connecting different pieces of code together, use interfaces, rather than directly referencing them

The three ideals are typically abbreviated as:

  • "Encapsulate what varies"
  • "Favour composition over inheritance"
  • "Program to interfaces, not implementation"

Original OOP article follows, with only minor changes e.g. sp corrections


OO - PIIE

[edit | edit source]

When talking about OOP you must remember the following:

00 - PIIE!

Where:

  • 00 = Object Orientation and
  • PIIE = Polymorphism / Inheritance / Instantiation / Encapsulation

Encapsulation

[edit | edit source]
You can only access private attributes and methods through an interface (public methods)

You noticed that we didn't have to use the dim statement for the attributes and we used the word private instead. What this means is that these attributes are not directly accessible once you have instantiated the class. Let's take our polo class as an example:

polo.fuel = 100 'this would be acceptable (but not in the exam!)

In the example we access the fuel attribute of the polo class and give the car 100 units of fuel because fuel is declared as public, there are no restrictions in accessing it. However, when we try the following we run into trouble:

polo.maxSpeed = 100 'this would be unacceptable

The reason that this wouldn't work is because we have declared the maxSpeed attribute as private. If something is declared as private you can't access it externally, but how do you access it? The only way to access a private method or attribute is to use an interface, or public method. In the car code example we have:

public sub setSpeed(byVal s as integer) 'declaring an interface
   maxSpeed = s
end sub

Because this method is public we can call it through: polo.setSpeed(100). And because setSpeed is declared inside the car object, it can have access to all the private attributes and methods.

We also need to find out the speed of a car to display to the user, we can do this by creating a get routine:

public function getSpeed() 'declaring an interface (a function as we are returning a value)
   return maxSpeed
end function

Because this method is public we can call it through: polo.getSpeed(). And because getSpeed is declared inside the car object, it can have access to all the private attributes and methods.

In general attributes should always be declared as private and only accessible through interfaces, using a setX command to give the private variable X a new value, and using the getX command to return the value of X. You should never directly access X!

Exercise: Encapsulation

Declare a colour attribute for the car that can only be accessed through an interface

Answer:

private colour as string 'this must be private!
Write an interface to set the colour

Answer:

public sub setColour(byVal c as string) 'declaring an interface, make sure it's public!
   colour = c
end sub
Write an interface to return the colour

Answer:

public function getColour() 'it must be a function to return a value
   return colour
end function

Create an actor class with the following:

  • attributes: health, dexterity, x, y, strength
  • methods: walkforward(), eat(), gethit(), displaystats(), setHealth(p), setLocation(px,py)

Answer:

Class actor
 Private health As Integer
 Private name As String
 Private x As Integer
 Private y As Integer

 Public Sub setName(ByVal p)
  name = p
 End Sub

 Public Sub walkforward()
  x = x + 1
 End Sub

 Public Sub eat()
  health = health + 10
  Console.WriteLine("Nom Nom Nom")
 End Sub

 Public Sub gethit()
  health = health - 10
  Console.WriteLine("Argh!")
 End Sub

 Public Sub displaystats()
  Console.WriteLine("Character :" & name)
  Console.WriteLine("Health :" & health)
  Console.WriteLine("Location :" & x & ", " & y)
 End Sub

 Public Sub setHealth(ByVal p)
  health = p
 End Sub

 Public Sub setLocation(ByVal px, ByVal py)
  x = px
  y = py
 End Sub

End Class

For the actor class declared above instantiate:

  • Wizard called Barry with 100 Health starting at 4,5
  • Orc called Herbert with 35 Health starting at 20,2, then report on his status

Answer:

dim wizard as new actor  'it doesn't have to be named wizard, it could have another name
dim orc as new actor     'it doesn't have to be named orc, it could have another name

wizard.setName("Barry") 'remember to use your get and set routines!
wizard.setHealth(100)
wizard.setLocation(4,5)

orc.setName("Herbert")
orc.setHealth(35)
orc.setLocation(20,2)
orc.displaystats()
Programming Get and Set Routines

Encapsulation is such a common thing that some languages have short cuts for making get and set routines. In VB.NET this involves using a property:

Private _fuel As Integer

Public Property Fuel() As Integer
    Get
        Return _fuel
    End Get
    Private Set(ByVal value As Integer)
        _fuel = value
    End Set
End Property

As we will never call _fuel directly it doesn't matter that it has an underscore at the beginning. Using this code we can perform the following methods without having the fuel attribute as public:

lada.fuel = 23
Console.Writeline(lada.fuel)

Inheritance diagrams

[edit | edit source]
CPT-OOP-inheritance.svg
inheritance diagram of vehicles, all sharing attributes and functions from the parent class 'Vehicle'.
  • The Truck inherits the Vehicle and adds its own attributes and methods
  • The Car inherits the Vehicle and adds its own attributes and methods
  • The Electric inherits the Car (and therefore the Vehicle) and adds its own attributes and methods
  • The Petrol inherits the Car (and therefore the Vehicle) and adds its own attributes and methods

Note the direction of arrows, you'll get marked down for putting them in the wrong direction.

Exercise: Inheritance Diagrams


What is the parent class of car in the above diagram?

Answer:

vehicle

Draw an inheritance diagram for the following classes:

motor bikes, bikes, vehicles, pedal bikes, trikes.

Answer:


Draw an inheritance diagram for the following classes:

Monster, Character, Dragon, Hero, Orc

Answer: