Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs."— Presentation transcript:

1 Object Oriented Programming

2 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

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

4 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); } }

5 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; }

6 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 = 5 y = 17; // x = 5, y = 17 x = 8; // x = 8, y = 17

7 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 a1 a2[0] = 7; Console.WriteLine("[{0}]", string.Join(", ", myarr)); // [7, 15, 8] index012 value4158 index012 value7158 a1 a2

8 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

9 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 );

10 Classes class: A program entity that represents either: 1.A program / module, or 2.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

11 Blueprint analogy iPod blueprint state: current song volume battery life behavior: power on/off change station/song change volume choose random song iPod #1 state: song = " 1,000,000 Miles " volume = 17 battery life = 2.5 hrs behavior: power on/off change station/song change volume choose random song iPod #2 state: song = "Letting You" volume = 9 battery life = 3.41 hrs behavior: power on/off change station/song change volume choose random song iPod #3 state: song = "Discipline" volume = 24 battery life = 1.8 hrs behavior: power on/off change station/song change volume choose random song creates

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”

13 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.

14 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

15 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 Account s with non-negative balance.  Example: Only allow Date s with a month from 1-12.

16 16 Class Members Methods 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.

17 17 Making a Method Visible To 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.

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.

19 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

20 Access Modifiers  private password;  private variables/ functions are not accessible outside the class  public variables/ functions allows access outside the class

21 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 calledclass  A class may have multiple constructors that take different arguments  There is always a default one  Authenticator myAccess = new Authenticator();

22 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;

23 GUI design for Authenticator class  Lets design a windows form application similar to the one we did as console application


Download ppt "Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs."

Similar presentations


Ads by Google