1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

24
1 Programming Programming Paradigms Paradigms O O bject bject O O rientated rientated P P rogramming rogramming P P aradigm aradigm ( ( OOP OOP ) )

Transcript of 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

Page 1: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

11

Programming ParadigmsProgramming Paradigms

OObject bject OOrientated rientated PProgramming rogramming PParadigm (aradigm (OOPOOP))

Page 2: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

22

Learning ObjectivesLearning Objectives

Describe with the aid of examples, the Describe with the aid of examples, the characteristics of a variety of programming characteristics of a variety of programming paradigms.paradigms.

Explain the terms object-oriented and procedural Explain the terms object-oriented and procedural as applied to high-level languages.as applied to high-level languages.

Discuss the concepts and, using examples, Discuss the concepts and, using examples, show an understanding of data encapsulation, show an understanding of data encapsulation, classes and derived classes, and inheritance.classes and derived classes, and inheritance.

Page 3: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

33

Programming ParadigmsProgramming Paradigms

Methods of programming.Methods of programming.

Page 4: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

44

Procedural Programming ParadigmProcedural Programming Paradigm

Expect the user to write the instructions in sequenceExpect the user to write the instructions in sequence telling the computer exactly how to solve a problem.telling the computer exactly how to solve a problem.Reusing routines from a procedural language program Reusing routines from a procedural language program is not easy is not easy (variable values are not stored solely in the (variable values are not stored solely in the procedure and can be changed by different programs so affecting procedure and can be changed by different programs so affecting the way procedures run in different programs).the way procedures run in different programs).

This and the fact that procedural languages do not model the real This and the fact that procedural languages do not model the real world are the main reasons why the world are the main reasons why the OObject bject OOrientated rientated PProgramming rogramming PParadigm (aradigm (OOPOOP) was created – see next slide.) was created – see next slide.

You have used Visual Basic in a procedural paradigm You have used Visual Basic in a procedural paradigm form up to now (it can be used in an OOP form as form up to now (it can be used in an OOP form as well).well).Other examples are C++, FORTRAN, COBOL, BASIC Other examples are C++, FORTRAN, COBOL, BASIC etc.etc.

Page 5: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

55

Classes

OObject bject OOrientated rientated PProgramming rogramming PParadigm (aradigm (OOPOOP) enables modelling of ) enables modelling of

the real world by basing itself on: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

Page 6: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

66

Examples of Object Orientated Examples of Object Orientated PProgramming rogramming PParadigm (aradigm (OOPOOP) )

LanguagesLanguages

Eiffel, Smalltalk, JavaEiffel, Smalltalk, Java, …., ….

Page 7: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

77

Person Class ExamplePerson Class Example

Variables

Methods

Page 8: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

88

InstantiationInstantiationAn object being An object being created as an instance created as an instance (member) of a (member) of a class which can then class which can then use variables and use variables and methods of using those methods of using those variables from that variables from that class and any super-class and any super-classes above it classes above it (explained later)(explained later). .

Marton …..nr. ….., str. …..

Mr Leenr. ….., str. …..

Page 9: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

Re-usable CodeRe-usable Code

OOP produces re-usable code by creating OOP produces re-usable code by creating a library (collection) of classes for creating a library (collection) of classes for creating objects in other programs.objects in other programs.

Classes

Objects

Program1

Objects

Program2

Objects

Program3

Page 10: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1010

Inheritance Inheritance Allows the creation of Allows the creation of derived classes from an derived classes from an already existing class already existing class (which is now known as a (which is now known as a super-class) so that the super-class) so that the derived class can inherit derived class can inherit (use) variables and (use) variables and methods of any super-methods of any super-classes above it.classes above it. This means that This means that

variables and methods from variables and methods from super-classes can be reused super-classes can be reused in derived classes without in derived classes without repetition.repetition.

Page 11: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1111

Data encapsulation (data hiding)Data encapsulation (data hiding)DiagramDiagram

Classes

Objects

Program Code

One way only!

Page 12: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1212

Data encapsulation (data hiding)Data encapsulation (data hiding)

The act of preventing a program: 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 Having access to object variables other than

through the use of the methods provided in the through the use of the methods provided in the class associated with them.class associated with them.

This means object variables will either be: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 Difficult to inadvertently alter if methods do exist to alter them

as they can only be altered in the way specified in the as they can only be altered in the way specified in the method/s.method/s.

Page 13: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1313

Data encapsulation example codeData encapsulation example code

ObjectName.ObjectVariable = ....ObjectName.ObjectVariable = .... not allowed not allowed Only Only ObjectName.GetObjectVariable() ObjectName.GetObjectVariable() is allowed.is allowed. So the value of the variable can be used but access is So the value of the variable can be used but access is

one way only (object one way only (object toto program) so no change to the program) so no change to the variable is possible.variable is possible.

Also means that Also means that ... = ... = ObjectName.ObjectVariable*....ObjectName.ObjectVariable*.... is not allowed only is not allowed only

... =  ObjectName.GetObjectVariable()*....... =  ObjectName.GetObjectVariable()*.... So no variable can even be referred to if there is no So no variable can even be referred to if there is no

method to access it.method to access it. The program does not need to know how the The program does not need to know how the

GetObjectVariable works and cannot change the GetObjectVariable works and cannot change the internal code of this method.internal code of this method.

Page 14: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1414

Note:Note:

If a method allows change then OOP uses a If a method allows change then OOP uses a method in the form method in the form ObjectName.SetObjectVariable()ObjectName.SetObjectVariable()

Note that to display all object variables of an Note that to display all object variables of an object OOP uses a method of form object OOP uses a method of form ObjectName.OutputData()ObjectName.OutputData()

Page 15: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1515

Data IntegrityData Integrity

Class methods cannot be changed by a program Class methods cannot be changed by a program so no one program using a class can affect the so no one program using a class can affect the running of other programs using the same class.running of other programs using the same class.

It is difficult to inadvertently change (impossible It is difficult to inadvertently change (impossible if the no method exists to allow change) object if the no method exists to allow change) object variables. variables.

If a method is changed then it must be done in If a method is changed then it must be done in the definition of the class concerned and this the definition of the class concerned and this can be done without needing to change the can be done without needing to change the programs which use the class.programs which use the class.

Page 16: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1616

PolymorphismPolymorphism

The concept of the The concept of the same method having same method having different meanings in different meanings in different classes.different classes. So different versions So different versions

of the same method of the same method can be used in can be used in different classes.different classes.

Page 17: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1717

Message Passing between classesMessage Passing between classes

In order for inheritance and polymorphism In order for inheritance and polymorphism to be possible a class needs to to be possible a class needs to send variables to another class or ask send variables to another class or ask another class to perform a method.another class to perform a method. This is done by passing messages between This is done by passing messages between

classes.classes.

Page 18: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1818

Example Person Variables Code Example Person Variables Code

class Person {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 //Create a constructor that copies the initial values into the object's variablesinto 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

Page 19: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

1919

Example Code for MethodsExample 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 + System.out.println("The person’s name is " + Name +

“ and their address is " + Address);“ 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.

Page 20: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

2020

Example Object Instantiation CodeExample Object Instantiation Code

// Create two persons// Create two persons

Marton = new Person(“Marton ….”, “str. … Marton = new Person(“Marton ….”, “str. … , nr. …,”);, nr. …,”);

MrLee = new Person(“Mr Lee”, “str. … , nr. MrLee = new Person(“Mr Lee”, “str. … , nr. …,”);…,”);

Page 21: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

2121

PlenaryPlenaryIn a particular object In a particular object oriented programming oriented programming language, the following language, the following classes are defined.classes are defined.

With reference to the With reference to the diagram explain the diagram explain the terms:terms:

InstantiationInstantiation Inheritance Inheritance Data encapsulationData encapsulation Marton …..

nr. ….., str. …..Mr Leenr. ….., str. …..

Page 22: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

2222

PlenaryPlenary

InstantiationInstantiation When an example (object) of a particular class is stated (an 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 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.methods of that class and has the variables of that class.

A pupil object with a real name, address, form and DOB is A pupil object with a real name, address, form and DOB is created from the Pupil class (and also from the Person class created from the Pupil class (and also from the Person class due to inheritance).due to inheritance).

InheritanceInheritance Where one class is a subclass of another it can use its Where one class is a subclass of another it can use its

methodsmethods Pupil can use getname() from PersonPupil can use getname() from Person

Data encapsulationData encapsulation Data can only be accessed by the methods provided by the Data can only be accessed by the methods provided by the

classclass Name can only be accessed from the class PersonName can only be accessed from the class Person

Page 23: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

2323

PlenaryPlenary

Describe characteristics of an object-Describe characteristics of an object-orientated approach to problem solving.orientated approach to problem solving.

Page 24: 1 Programming Paradigms Object Orientated Programming Paradigm (OOP)

2424

PlenaryPlenary

Produces new objectsProduces new objectsVariables and the permitted operations on that data are Variables and the permitted operations on that data are defined together (class)defined together (class)Produces re-usable code by creating a class library.Produces re-usable code by creating a class library.Classes can share some characteristics (inheritance, Classes can share some characteristics (inheritance, derivation)derivation)Encapsulation of data to protect data integrityEncapsulation of data to protect data integrityPolymorphism to use different versions of the same Polymorphism to use different versions of the same method (in different classes)method (in different classes)Structure of data and the code in a class may be altered Structure of data and the code in a class may be altered without affecting programs that use the class without without affecting programs that use the class without affecting other classes.affecting other classes.Objects in classes can pass messages from one to Objects in classes can pass messages from one to another.another.