object oriented programming. oop the fundamental idea behind object-oriented programming is: the...

23
Object Oriented Programming

Upload: augustine-holland

Post on 18-Jan-2018

237 views

Category:

Documents


0 download

DESCRIPTION

Kinds of Objects  Human Entities: employee, customer, worker, manager…  Graphics program: point, line, circle…  Data storage: linked list, stack, matrix…  …

TRANSCRIPT

Page 1: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Object Oriented Programming

Page 2: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

OOP The fundamental idea behind object-oriented

programming is: The real world consists of objects. Computer programs

may contain computer world representations of the things (objects) that constitute the solutions of real world problems.

Real world objects have two parts: Properties (or state :characteristics that can change), Behavior (or abilities :things they can do).

To solve a programming problem in an object-oriented language, the programmer no longer asks how the problem will be divided into functions, but how it will be divided into objects.

The emphasis is on data

Page 3: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Kinds of Objects Human Entities: employee, customer, worker,

manager… Graphics program: point, line, circle… Data storage: linked list, stack, matrix… …

Page 4: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

4 4

What is a class?Essentially a struct with built-in functions

class Circle{

double radius = 0.0;const double pi = 3.141592;

double Area(){ return (pi * radius * radius);}

}

Page 5: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

A swap method? Does the following swap method work? Why or why

not?public static void main(String[] args) { int a = 7; int b = 35;

// swap a with b? swap(a, b);

Console.WriteLine(a + " " + b);}

public static void swap(int a, int b) { int temp = a; a = b; b = temp;

}

Page 6: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Value semantics value semantics: Behavior where values are

copied when assigned, passed as parameters, or returned. Primitive types use value semantics. When one variable is assigned to another, its value is

copied. Modifying the value of one variable does not affect

others.

int x = 5;int y = x; // x = 5, y = 5y = 17; // x = 5, y = 17x = 8; // x = 8, y = 17

Page 7: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Reference semantics (objects) reference semantics: Behavior where variables

actually store the address of an object in memory. When one variable is assigned to another, the object is

not copied; both variables refer to the same object. Modifying the value of one variable will affect others.

int[] a1 = {4, 15, 8};int[] a2 = a1; // refer to same array as a1a2[0] = 7;Console.WriteLine("[{0}]", string.Join(", ", myarr)); // [7, 15, 8]

index 0 1 2

value 4 15 8

index 0 1 2

value 7 15 8a1 a2

Page 8: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

References and objects Arrays and objects use reference semantics. Why?

efficiency. Copying large objects slows down a program. sharing. It's useful to share an object's data among

methods.

When an object is passed as a parameter, the object is not copied. The parameter refers to the same object. If the parameter is modified, it will affect the original

object

Page 9: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Objects object: An entity that encapsulates data and

behavior. data: variables inside the object behavior: methods inside the object

You interact with the methods;the data is hidden in the object.

Constructing (creating) an object:Type objectName = new Type(parameters);

Calling an object's method:objectName.methodName(parameters);

Page 10: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Classes• class: A program entity that represents either:

1. A program / module, or2. A template for a new type of objects.

– object-oriented programming (OOP): Programs that perform their behavior as interactions between objects.– abstraction: Separation between concepts and details.

Objects and classes provide abstraction in programming.

– Treat objects as black boxes – deal only with the interface

Page 11: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Blueprint analogyiPod blueprint

state: current song volume battery lifebehavior: power on/off change station/song change volume choose random song

iPod #1state: song = "1,000,000 Miles" volume = 17 battery life = 2.5 hrsbehavior: power on/off change station/song change volume choose random song

iPod #2state: song = "Letting You" volume = 9 battery life = 3.41 hrsbehavior: power on/off change station/song change volume choose random song

iPod #3state: song = "Discipline" volume = 24 battery life = 1.8 hrsbehavior: power on/off change station/song change volume choose random song

creates

Page 12: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

12 12

Encapsulation

By default the class definition encapsulates, or hides, the data inside it.

Key concept of object oriented programming.

The outside world can see and use the data only by calling the build-in functions. Called “methods”

Page 13: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Encapsulation To create software models of real world objects

both data and the functions that operate on that data are combined into a single program entity. Data represent the properties (state), and functions represent the behavior of an object. Data and its functions are said to be encapsulated into a single entity.

An object’s functions, called member functions typically provide the only way to access its data. The data is hidden, so it is safe from accidental alteration. This simplifies writing, debugging, and maintaining the

program.

Page 14: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Encapsulation encapsulation: Hiding implementation details from

clients.

Encapsulation enforces abstraction. separates external view (behavior) from internal view (state) protects the integrity of an object's data

Page 15: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Benefits of encapsulation Abstraction between object and clients

Protects object from unwanted access Example: Can't fraudulently increase an Account's

balance.

Can change the class implementation later Example: Point could be rewritten in polar

coordinates (r, θ) with the same methods.

Can constrain objects' state (invariants) Example: Only allow Accounts with non-negative

balance. Example: Only allow Dates with a month from 1-12.

Page 16: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

16 16

Class MembersMethods and variables declared inside a class

are called members of that class.

Member variables are called fields. Member functions are called methods.

In order to be visible outside the class definition, a member must be declared public.

As written in the previous example, neither the variable radius nor the method Area could be seen outside the class definition.

Page 17: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

17 17

Making a Method VisibleTo make the Area() method visible outside we would write

it as:

public double Area(){

return pi * radius * radius;}

We will keep the radius and pi field private.

Page 18: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

18 18

Interface vs. Implementation The public definitions comprise the

interface for the class A contract between the creator of the class and

the users of the class. Should never change.

Implementation is private Users cannot see. Users cannot have dependencies. Can be changed without affecting users.

Page 19: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

A Class Example Lets write a class named Authenticator for

password authentication as a console application

It should have two functions Check if given password is correct? Change a password

Page 20: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Access Modifiers private password;

private variables/ functions are not accessible outside the class

public variables/ functions allows access outside the class

Page 21: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Instantiating and Using Objects The class Authenticator contains everything

the compiler needs to know about to process this new variable type.

Constructor – create an instance Whenever a class is created, its constructor is

called A class may have multiple constructors that take

different arguments There is always a default one

Authenticator myAccess = new Authenticator();

Page 22: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

Static Each instance of a class has its own set of fields.

Authenticator alice = new Authenticator();Authenticator bob = new Authenticator();alice.ChangePassword("OldAlicePassword",

“NewAlicePassword);bob.ChangePassword("OldBobPassword", "NewBobPassword);

The two objects are different and have distinct values of password

Variable that applies to all can be done static variable belongs to class not its instances

private static uint minPasswordLength = 6;

Page 23: Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs

GUI design for Authenticator class Lets design a windows form application similar

to the one we did as console application