1 / 26

Object Oriented Programming

Explore the history of ENIAC, learn about object-oriented programming, data encapsulation, information hiding, and the benefits of abstraction. Discover how to share data and use constants in classes.

pflaherty
Télécharger la présentation

Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Oriented Programming CSC 171 FALL 2001 LECTURE 11

  2. 1943 – 1947 Work on ENIAC at the Univ. of Pennsylvania John Mauchly & J. Presper Eckert. The world's first electronic digital computer was developed to compute World War II ballistic firing tables. History: ENIAC Electronic Numerical Integrator and Computer

  3. Computer Science • What is computer science?

  4. Computer Science “fundamentally, computer science is the science of abstraction – creating the right model for a problem and devising the appropriate mechanizable technique to solve it.” - A. Aho and J. Ullman

  5. Acts of mind - Locke • “The acts of the mind, wherein it exerts its power over simple ideas, are chiefly these three:

  6. Aggregation 1. Combining several simple ideas into one compound one, and thus all complex ideas are made

  7. Comparision 2. The second is brining two ideas, where simple or complex, together, and setting them by one another so as to take aview of them at once,without uniting them into one, by which it gets all its ideas of relations.

  8. Abstraction 3. The third is separating them from all other ideas that accompany them in their real existence: this is called abstraction, and thus all general ideas are made.” John Locke, An Essay Concerning Human Understanding, (1690)

  9. Object Orientation • Objects are the means of aggregation and abstraction, in an OO programming language • Aggregation – bundling data elements • Abstraction - inheritance

  10. Data Encapulation • OOP encapulates • data • behavior

  11. Interfaces • We define systems in terms of many objects • Each type of object has certain behaviors • methods • Each object has certain data • Instance variables • Objects communicate with other via well defined interfaces

  12. Information Hiding When we design objects which communicate exclusively via interfaces - we insulate (abstract) the outward functionality from the inward implementation. This insulation is the principle of information hiding – the inward details of the implementation are hidden from the outside objects

  13. Benefits of Information Hiding • Information hiding promotes program modifiability. • Clients are not required to know the internals of a class in order to use it. • So, if the class changes inside, then the client need not be changed • The client and the object are said to be “loosely coupled”

  14. Example – A bank account

  15. A specific instance

  16. Multiple instances

  17. Bank Account - behaviors public class BankAccount { private double balance; public BankAccount() { balance = 0; } public BankAccount(double initialBalance){ balance = initialBalance; } public void deposit(double amount){ balance = balance + amount; } public void withdraw(double amount){ balance = balance - amount; } public double getBalance(){return balance;} }

  18. this • Java conserves storage by maintaining only one copy of each method per class • The same method is invoked by every object • Every object has its own copy of its instance variables • Every object, by default has a reference to itself • “this” is the name of every object’s reference to itself

  19. Bank Account - this public class BankAccount { private double balance; public BankAccount() { this.balance = 0; } public BankAccount(double balance){ this.balance = balance; } public void deposit(double amount){ this.balance = this.balance + amount; } public void withdraw(double amount){ this.balance = this.balance - amount; } public double getBalance(){ return this.balance;} }

  20. Sometimes, classes share • Consider a variation of the BankAccount public class BankAccount { . . . . private double balance; private int accountNumber; // we want to assign sequential numbers }

  21. Sharing data between objects of the same class • We want to set the account number automatically public class BankAccount { private double balance; private int accountNumber; private int lastAssignedNumber = 0; //Will this work? public BankAccount() { lastAssignedNumber++; accountNumber = lastAssignedNumber; //??????? }

  22. Static/class variables • We don’t want each instance of the class to have its own value for lastAssignedNumber • We need to have a single value that is the same for the entire class. • These are called class or static variables

  23. Sharing data between objects of the same class • We want to set the account number automatically public class BankAccount { private double balance; private int accountNumber; private static int lastAssignedNumber = 0; public BankAccount() { lastAssignedNumber++; accountNumber = lastAssignedNumber; }

  24. Finally • Some types of variables are fixed constants, that we do not want to change • Like conversion factors • We can use the keyword “final” to prevent changes • So we have a “constant variable” • Sort of like • “jumbo shrimp” • “freezer burn”

  25. Classes share constants • Consider a variation of the BankAccount public class Converter { private final static double miles2km = 0.6; }

More Related