Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Computer Science & Information Technology G6DICP - Lecture 11 Classes & Objects.

Similar presentations


Presentation on theme: "School of Computer Science & Information Technology G6DICP - Lecture 11 Classes & Objects."— Presentation transcript:

1 School of Computer Science & Information Technology G6DICP - Lecture 11 Classes & Objects

2 2 Classes and OO Programming An OO program consists of at least one (usually many more) inter-dependent classes. An OO program consists of at least one (usually many more) inter-dependent classes. A class is a user-defined data type that describes the properties and capabilities of the real-world objects with which the program has to deal. A class is a user-defined data type that describes the properties and capabilities of the real-world objects with which the program has to deal. A class is a generic type of “thing”, whereas a specific “thing” (an instance of the class) is an Object. A class is a generic type of “thing”, whereas a specific “thing” (an instance of the class) is an Object. A class definition consists of both the properties (ie data) and the capabilities (ie program code) of that class. A class definition consists of both the properties (ie data) and the capabilities (ie program code) of that class.

3 3 An Analogy Consider the class Pen Consider the class Pen  ie pens in general, not any one pen in particular. Properties (variables) Properties (variables) Colour Colour Thickness Thickness Capabilities (methods) Capabilities (methods) Remove lid Remove lid Draw line Draw line Draw circle Draw circle Replace lid Replace lid Instances of class Pen Instances of class Pen  ie specific pens, belonging to specific people. Tim’s boardwriter Tim’s boardwriter Tim’s biro Tim’s biro Alan’s pen Alan’s pen Instances of the class Pen are called Objects. Instances of the class Pen are called Objects.

4 4 Using Classes Definition Definition Declaration of variables Declaration of variables Instance variables - specific to one instance of a class Instance variables - specific to one instance of a class Class variables - shared by all instances of that class Class variables - shared by all instances of that class Definition of methods Definition of methods A method is the program code required to perform a specific task with the class. A method is the program code required to perform a specific task with the class. Methods consist of statements. Methods consist of statements. Instantiation Instantiation 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 can be used without instantiation. Some classes (especially utility classes) have methods and variables that can be used without instantiation.

5 5 The Purpose of OO Programming Classes should model the real world - thus leading to efficient coding. Classes should model the real world - thus leading to efficient coding. Classes are reusable. Classes are reusable. Libraries of reusable classes (“class libraries”) make sophisticated functionality easily available. Libraries of reusable classes (“class libraries”) make sophisticated functionality easily available. This is a “construction kit” approach to programming. This is a “construction kit” approach to programming. For example - consider graphical programs: For example - consider graphical programs: Window class Window class Text box Text box Button class Button class


Download ppt "School of Computer Science & Information Technology G6DICP - Lecture 11 Classes & Objects."

Similar presentations


Ads by Google