Download presentation
Presentation is loading. Please wait.
Published byJeffrey Allen Modified over 9 years ago
1
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
2
2 Learning Objectives Describe with the aid of examples, the characteristics of a variety of programming paradigms. Explain the terms object-oriented and procedural as applied to high-level languages. Discuss the concepts and, using examples, show an understanding of data encapsulation, classes and derived classes, and inheritance.
3
3 Programming Paradigms Methods of programming.
4
4 Procedural Programming Paradigm Expect the user to write the instructions in sequence telling the computer exactly how to solve a problem. Reusing routines from a procedural language program is not easy (variable values are not stored solely in the procedure and can be changed by different programs so affecting the way procedures run in different programs). This and the fact that procedural languages do not model the real world are the main reasons why the Object Orientated Programming Paradigm (OOP) was created – see next slide. This and the fact that procedural languages do not model the real world are the main reasons why the Object Orientated Programming Paradigm (OOP) was created – see next slide. You have used Visual Basic in a procedural paradigm form up to now (it can be used in an OOP form as well). Other examples are C++, FORTRAN, COBOL, BASIC etc.
5
5 Classes Object Orientated Programming Paradigm (OOP) enables modelling of the real world by basing itself on: Actual real-world entities like actual breeds of dogs, names of people, or makes of cars etc... Classes of real-world entities objects like dog, person, car etc… with appropriate variables and methods of using those variables. One way only! (explained later) Objects Program Code
6
6 Examples of Object Orientated Programming Paradigm (OOP) Languages Eiffel, Smalltalk, Java, ….
7
7 Person Class Example Variables Methods
8
8 Instantiation An object being created as an instance (member) of a class which can then use variables and methods of using those variables from that class and any super- classes above it (explained later). Marton ….. nr. ….., str. ….. Mr Lee nr. ….., str. …..
9
Re-usable Code OOP produces re-usable code by creating a library (collection) of classes for creating objects in other programs. Classes Objects Program1 Objects Program2 Objects Program3
10
10 Inheritance Allows the creation of derived classes from an already existing class (which is now known as a super-class) so that the derived class can inherit (use) variables and methods of any super- classes above it. This means that variables and methods from super-classes can be reused in derived classes without repetition. This means that variables and methods from super-classes can be reused in derived classes without repetition.
11
11 Data encapsulation (data hiding) Diagram Classes Objects Program Code One way only!
12
12 Data encapsulation (data hiding) The act of preventing a program: Knowing or altering methods of a class. Knowing or altering methods of a class. Having access to object variables other than through the use of the methods provided in the class associated with them. Having access to object variables other than through the use of the methods provided in the class associated with them. This means object variables will either be: Unknown to the program if no methods exist to access them. Unknown to the program if no methods exist to access them. Inalterable to the program if no methods exist to change them. Inalterable to the program if no methods exist to change them. Difficult to inadvertently alter if methods do exist to alter them as they can only be altered in the way specified in the method/s. Difficult to inadvertently alter if methods do exist to alter them as they can only be altered in the way specified in the method/s.
13
13 Data encapsulation example code ObjectName.ObjectVariable =.... not allowed Only ObjectName.GetObjectVariable() is allowed. So the value of the variable can be used but access is one way only (object to program) so no change to the variable is possible. So the value of the variable can be used but access is one way only (object to program) so no change to the variable is possible. Also means that... = ObjectName.ObjectVariable*.... is not allowed only... = ObjectName.GetObjectVariable()*.... So no variable can even be referred to if there is no method to access it. So no variable can even be referred to if there is no method to access it. The program does not need to know how the GetObjectVariable works and cannot change the internal code of this method. The program does not need to know how the GetObjectVariable works and cannot change the internal code of this method.
14
14 Note: If a method allows change then OOP uses a method in the form ObjectName.SetObjectVariable() Note that to display all object variables of an object OOP uses a method of form ObjectName.OutputData()
15
15 Data Integrity Class methods cannot be changed by a program so no one program using a class can affect the running of other programs using the same class. It is difficult to inadvertently change (impossible if the no method exists to allow change) object variables. If a method is changed then it must be done in the definition of the class concerned and this can be done without needing to change the programs which use the class.
16
16 Polymorphism The concept of the same method having different meanings in different classes. So different versions of the same method can be used in different classes. So different versions of the same method can be used in different classes.
17
17 Message Passing between classes In order for inheritance and polymorphism to be possible a class needs to send variables to another class or ask another class to perform a method. This is done by passing messages between classes. This is done by passing messages between classes.
18
18 Example Person Variables Code class Person { //Declare the variables related to a Person //Declare the variables related to a Person string Name; string Name; string Address; string Address; //Create a constructor that copies the initial values into the object's variables //Create a constructor that copies the initial values into the object's variables Person (string N, string A) { Person (string N, string A) { Name = N; Name = N; Address = A; Address = A; }//end of constructor Person }//end of constructor Person
19
19 Example Code for Methods //Create a method to output the details of the person //Create a method to output the details of the person void write ( ) { void write ( ) { System.out.println("The person’s name is " + Name + “ and their address is " + Address); System.out.println("The person’s name is " + Name + “ and their address is " + Address); }//end of write method. }//end of write method. string getName( ) { string getName( ) { getName := Name; getName := Name; }//end of getName method. }//end of getName method. integer getAddress( ) { integer getAddress( ) { getAddress := Address; getAddress := Address; }//end of getAddress method. }//end of getAddress method.
20
20 Example Object Instantiation Code // Create two persons Marton = new Person(“Marton ….”, “str. …, nr. …,”); MrLee = new Person(“Mr Lee”, “str. …, nr. …,”);
21
21 Plenary In a particular object oriented programming language, the following classes are defined. With reference to the diagram explain the terms: Instantiation Instantiation Inheritance Inheritance Data encapsulation Data encapsulation Marton ….. nr. ….., str. ….. Mr Lee nr. ….., str. …..
22
22 Plenary Instantiation When an example (object) of a particular class is stated (an object is created) it is said to be instantiated and it can use the methods of that class and has the variables of that class. When an example (object) of a particular class is stated (an object is created) it is said to be instantiated and it can use the methods of that class and has the variables of that class. A pupil object with a real name, address, form and DOB is created from the Pupil class (and also from the Person class due to inheritance). A pupil object with a real name, address, form and DOB is created from the Pupil class (and also from the Person class due to inheritance).Inheritance Where one class is a subclass of another it can use its methods Where one class is a subclass of another it can use its methods Pupil can use getname() from Person Pupil can use getname() from Person Data encapsulation Data can only be accessed by the methods provided by the class Data can only be accessed by the methods provided by the class Name can only be accessed from the class Person Name can only be accessed from the class Person
23
23 Plenary Describe characteristics of an object- orientated approach to problem solving.
24
24 Plenary Produces new objects Variables and the permitted operations on that data are defined together (class) Produces re-usable code by creating a class library. Classes can share some characteristics (inheritance, derivation) Encapsulation of data to protect data integrity Polymorphism to use different versions of the same method (in different classes) Structure of data and the code in a class may be altered without affecting programs that use the class without affecting other classes. Objects in classes can pass messages from one to another.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.