Upload
godwin-stephens
View
215
Download
0
Tags:
Embed Size (px)
Citation preview
School of Computer Science & Information Technology
School of Computer Science & Information Technology
G6DICP - Lecture 11G6DICP - Lecture 11
Classes & ObjectsClasses & Objects
2
Classes and OO ProgrammingClasses and OO Programming An OO program consists of at least one (usually An OO program consists of at least one (usually
many more) inter-dependent classes.many more) inter-dependent classes. A class is a user-defined data type that describes A class is a user-defined data type that describes
the properties and capabilities of the real-world the properties and capabilities of the real-world objects with which the program has to deal.objects with which the program has to deal.
A class is a generic type of “thing”, whereas a A class is a generic type of “thing”, whereas a specific “thing” (an instance of the class) is an specific “thing” (an instance of the class) is an Object.Object.
A class definition consists of both the properties A class definition consists of both the properties (ie data) and the capabilities (ie program code) (ie data) and the capabilities (ie program code) of that class.of that class.
3
An AnalogyAn Analogy Consider the class Pen Consider the class Pen
ie pens in general, not any ie pens in general, not any one pen in particular.one pen in particular.
Properties (variables)Properties (variables) ColourColour ThicknessThickness
Capabilities (methods)Capabilities (methods) Remove lidRemove lid Draw lineDraw line Draw circleDraw circle Replace lidReplace lid
Instances of class Pen Instances of class Pen ie specific pens, belonging ie specific pens, belonging
to specific people.to specific people. Tim’s boardwriterTim’s boardwriter Tim’s biroTim’s biro Alan’s penAlan’s pen
Instances of the class Pen Instances of the class Pen are called Objects.are called Objects.
4
Using ClassesUsing Classes
DefinitionDefinition Declaration of variablesDeclaration of variables
Instance variables - specific to one instance of a classInstance variables - specific to one instance of a class Class variables - shared by all instances of that classClass variables - shared by all instances of that class
Definition of methodsDefinition of methods A method is the program code required to perform a specific task with the A method is the program code required to perform a specific task with the
class.class. Methods consist of statements.Methods consist of statements.
InstantiationInstantiation Usually an instance of a class needs to be created before it can be used.Usually an instance of a class needs to be created before it can be used. Some classes (especially utility classes) have methods and variables that Some classes (especially utility classes) have methods and variables that
can be used without instantiation.can be used without instantiation.
5
The Purpose of OO ProgrammingThe Purpose of OO Programming Classes should model the real world - thus Classes should model the real world - thus
leading to efficient coding.leading to efficient coding. Classes are reusable.Classes are reusable. Libraries of reusable classes (“class libraries”) Libraries of reusable classes (“class libraries”)
make sophisticated functionality easily available.make sophisticated functionality easily available. This is a “construction kit” approach to This is a “construction kit” approach to
programming.programming. For example - consider graphical programs:For example - consider graphical programs:
Window classWindow class Text boxText box Button classButton class