Object-Oriented Programming (OOP)
Specification
Object-oriented programming (OOP)
understanding of the terminology associated with OOP (including objects, properties, methods, classes, inheritance, polymorphism, containment (aggregation), encapsulation, getters, setters, instances)
understanding of how to solve a problem by designing appropriate classes
understanding of and ability to write code that demonstrates the use of OOP
Object-Oriented Programming
Procedural programming does not have any proper way for hiding data so it is less secure. Object-oriented programming provides data hiding so it is more secure. In procedural programming, overloading is not possible. Overloading is possible in object-oriented programming.
The 'L1 Abstraction and OOP 1' lesson slides, along with the Chapter 27 in the textbook (pp368-390) will present you with enough detail for the exam. However, being Paper 4, it is critical that you are able to express classes in both programming code and pseudocode.
Start with a simple introduction to OOP by watching this video.
As too few of you actually read this section in depth, here is a link to the official MS site for VB and OOP which gives additional information on that shown below.
Check the links at the bottom for more help and information
The video below introduces you to OOP set within .NET. Note the use of Windows Forms rather than console.
Coding Properties
Coding Methods
Using constructors
Inheritance
Inheritance example
Inheritance, as you should know allows a class to inherit properties and methods from a parent class, providing them through your own class, and for you to use in the inheriting class. A parent class can specify NotInheritable if no class may inherit from it. It can also, as shown further down this page, specify that a particular class must always inherit (that is, it cannot be instantiated on its own). Key to this is the Overrides procedure modifier (meaning that your class overrides the parent's own method). BY DEFAULT, public subs and functions are set to NotOverridable, meaning that you need to specify Overridable if a method can be overridden by an inheriting class.
Another important thing to state is that the constructor (Sub New // _init_ (in Python)) is not inherited (the truth is more complex). You must implement a constructor in two ways:
explicitly override the constructor using Overrides (e.g. Public Overrides Sub New()), or
include a MyBase.New() call as the first line in your regular constructor. If the parent class' constructor needs parameters, these must be passed in.
Here is an example of inheritance used in VB
Module Module1
'Program to show Concept of Inheritance
'If there is no constructor (New) defined in base class, VB will automatically
'handle it
Class Student
Private name As String
Private age As String
Public schoolname As String
Public Sub showschool()
Console.WriteLine(schoolname)
End Sub
End Class
Class Alevel
Inherits Student
Private grade As String
Public Sub New()
' you CANNOT inherit the constructor, you must define your own
' however, if it has no parameters, it is done automatically
MyBase.New
End Sub
Public Sub setGrade(ByVal a As String)
grade = a
End Sub
End Class
Sub Main()
'The moot point is being a typical A level student, we must have privilege to access the basic 'properties
'of students as well.How can we do this??
Dim studentname As New Alevel
studentname.setGrade("A")
'Here these 2 steps are normal class processing.But how do i get the property of student??
studentname.schoolname = "xxxxxxxxxx"
studentname.showschool()
'Hence because of inheritance I have been able to access schoolname and showschool() method 'of base class
'that is CLASS STUDENT.In addition i have also accessed my ownattributes and functions as 'well
'SO the inheritance is not only inheriting from base class but also being to able to work on our 'own class
'as well.Include both the points while answering.
Console.ReadLine()
End Sub
End Module
Another inheritance example
Module Module1
' class inheritance example for CIE CS
' L.Minett 2017
Class Person
Private _name As String
Private _DoB As Date
Private _PostCode As String
Public Sub New(Name As String, DoB As Date, PostCode As String)
' myClass is usually not specified as it is the default to
' reference methods and properties from the current class
MyClass.name = name
MyClass.DoB = DoB
MyClass.PostCode = PostCode
End Sub
'
' a public variable uses GET and SET methods without explicit definition
' however, this is the formal method and a way to ensure full control
' over the data given by code using this class
'
' GET and SET must be specified unless you declare WRITEONLY or READONLY
' which will then allow only specification of either SET or GET (respectively)
'
' An alternative is to use subs and functions, but this prevents use of assignment
' of values. e.g. [obj].[item]=[value]
' This, however, is the chosen method used by the text book
Public Property DoB() As Date
Get
Return _DoB
End Get
Set(ByVal value As Date)
If value > DateTime.Today Then
Throw New Exception("Date of Birth cannot be greater than today's date")
Else
_DoB = value
End If
End Set
End Property
Public Property name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property PostCode() As String
Get
Return _PostCode
End Get
Set(ByVal value As String)
If Len(value) < 2 > 8 Then ' allows half postcodes such as S2
Throw New Exception("PostCode is not valid")
Else
_PostCode = value
End If
End Set
End Property
End Class
Class Pupil
Inherits Person
' because the base class has a constructor that requires parameters, VB cannot
' automatically generate a constructor, so we need to declare one here
' remembering that inheritance does not inherit the constructor class.
' We can, however, still use the constructor from person by using MYBASE
' or we can specify OVERRIDES in the method heading, eliminating the need
' to call the MyBase.New()
Public Sub New(Name As String, DoB As Date, PostCode As String, PupilID As Integer)
' myClass is usually not specified as it is the default to
' reference methods and properties from the current class
MyBase.New() ' create an instance of our parent class (MUST be first)
MyClass.name = Name
MyClass.DoB = DoB
MyClass.PostCode = PostCode
End Sub
End Class
Sub Main()
' date constants (literals) always use US date format
Dim x As New Person("Lee", #6/27/1982#, "Nw7") ' date constants (literals) always use US date format
Console.WriteLine(x.name)
Console.WriteLine(x.DoB)
Console.ReadKey()
End Sub
End Module
Containment (aggregation)
Containment is a simple approach compared to inheritance, in which a class B, that needs access to the members of class A, contains an instance/object of class A, rather then inheriting the class A, which is an example of composition (the child object cannot exist without the parent). That is, an object(s) is created within your class. For example, shop items within a shop class or teachers and pupils within a school. We can show this with a containment class diagram. The OO lesson PP has further examples, as does the textbook pp386-390.
A quite detailed back history and look at the difference between containment and inheritance (composition) can be found here.
Overrides and MustInherit
When you override a class' method, you specify that it should call the newly defined method, and not any [with the same name] in a base class. This is what overrides does, it is a form of polymorphism. Do not confuse overriding with overloading, which allows a method to be called with different parameters.
Sometimes, when you create a base class (parent/super class) that cannot be instantiated on its own, but can only be used for inheritance in other child classes. For example, we create a vehicle class, from which we only want other classes to inherit from, and not be able to create an object of type vehicle. As part of the class' definition, we specify MustInherit.
Another example:
Public MustInherit Class shape
Public acrossLine As Double
Public MustOverride Function area() As Double
End Class
Public Class circle : Inherits shape
Public Overrides Function area() As Double
Return Math.PI * acrossLine
End Function
End Class
Public Class square : Inherits shape
Public Overrides Function area() As Double
Return acrossLine * acrossLine
End Function
End Class
Public Class consumeShapes
Public Sub makeShapes()
Dim shape1, shape2 As shape
shape1 = New circle
shape2 = New square
End Sub
End Class
You can declare shape1 and shape2 to be of type shape. However, you cannot create an object from shape because it lacks the functionality of the function area and is marked MustInherit.
Because they are declared as shape, the variables shape1 and shape2 are restricted to objects from the derived classes circle and square. Visual Basic does not allow you to assign any other object to these variables, which gives you a high level of type safety.
Private, Public and Protected
These keywords specify the visibility of a variable, constant, procedure and function:
Public specifies that these are visible outside of the class itself, by any code using the class. Setting variables to public is a cheap and simple way of creating class properties; however, it lacks the data integrity of using properties (set/get).
Private limits the visibility within the class itself. When using properties, etc., it is common to have internal variables set as private.
Protected modifies the visibility to internally, within the class, or to any class inheriting the class.
Arrays
When declaring objects in arrays, we generally do not use the NEW command if using a constructor. This is because each element in the array will need to be initialised with its own constructor call. For example:
Dim thisCar(5) as Car
for i= 0 to 5
thisCar(i)=New Car(list of initial parameters)
Next
Diagrams
There are a number of diagrams used in OOP. The OOP lesson PowerPoint (found in the files section under the main Unit 20 page) goes into further detail on these. These diagrams are not the most perfect examples, but exist to exemplify the basis of each diagram. Use the textbooks and lesson slides to see more practical examples specific to CAIE's examination standards.
Containment diagram
These can be shown in a simple form, with just the name of the class and the relationship between them (most often a 1 to many relationship), but equally, they can be shown as a class aggregaton diagram, whereby the details of the class are also shown (similar to a class diagram)
Class diagram
Inheritance Diagram