Programming with Objects Intro to object-orientation and how to use objects in Java James Brucker
The Realities of Software <ul><li>Useful, real-world software  </li></ul><ul><li>1. Change </li></ul><ul><li>2. Complexity...
The Problem <ul><li>&quot; Programming is fun, but developing quality software is hard.  In between the nice ideas, the re...
Intrinsic Complexity of Software <ul><li>About the future of software development... </li></ul><ul><li>&quot; T here is no...
Miller’s Law <ul><li>At any one time, a person can concentrate on at most  7    2  chunks  (units of information) </li></...
Benefit of Object-Orientation (1) <ul><li>Encapsulate   complexity </li></ul><ul><ul><li>divide program into  classes </li...
Benefit of Object-Orientation (2) <ul><li>Encapsulate  change </li></ul><ul><ul><li>each class presents only a simple  pub...
Benefit of Object-Orientation (3) <ul><li>Reuse   code </li></ul><ul><ul><li>classes are  reusable </li></ul></ul><ul><ul>...
Benefit of Object-Orientation (4) <ul><li>Better abstraction   </li></ul><ul><ul><li>objects make  good models  for things...
 
Objects and Programs <ul><li>An OO program consists of  objects  that interact with each other. </li></ul>
Classes <ul><li>A  class  is a  blueprint  or  definition  for a kind of object . </li></ul><ul><li>A class defines the  a...
Objects <ul><li>Objects  are  instances  of a class. </li></ul><ul><li>In Java, to create a new object use &quot;new&quot;...
3 Fundamental Characteristics of Objects <ul><li>Objects have: </li></ul><ul><li>state  - the current condition of an obje...
Bank Account Example <ul><li>BankAccount  class  is the definition for bank accounts. </li></ul>class name : BankAccount a...
Bank Account Object <ul><li>An object is an actual  instance  of the class. </li></ul><ul><li>rich is a bank accoount </li...
Object Properties <ul><li>Example: </li></ul><ul><li>For the &quot;rich&quot; object, what are... </li></ul><ul><ul><li>st...
HondaCivic Example <ul><li>The definition of a HondaCivic consists of: </li></ul><ul><ul><li>specifications </li></ul></ul...
HondaCivic class <ul><li>But, the Honda Civic owner doesn't need to know all these details.  He needs to know about a Hond...
Buying A Honda Civic <ul><li>You go to the Honda dealer and say...  </li></ul>I want a Honda Civic Yes, sir.  This way, pl...
Buying A Honda Civic (2) <ul><li>the dealer offers you the  class  for a Honda Civic...  </li></ul>Here you are!  All the ...
Buying A Honda Civic (3) <ul><li>but you can't drive blue prints and documents  </li></ul>That's not exactly what I had in...
Buying A Honda Civic (4) Silver, 4 door,  automatic transmission, tinted windows, ... yourcar =  new HondaCivic (&quot;sil...
Review: Class versus Objects HondaCivic Defines the properties and behavior for all instances (objects) of this class. Spe...
Object Identity Example <ul><li>Primitive data types  don't have identity.  </li></ul><ul><li>You cannot distinguish two p...
Object Identity Example <ul><li>Objects  do  have identity.  </li></ul><ul><li>You  can  distinguish two objects even when...
Object Identity Example (2) <ul><li>Two Honda Civic cars can be distinguished even if they exactly the same features and s...
Second Program <ul><li>/** Print an impersonal greeting message </li></ul><ul><li>*  @author James Brucker  </li></ul><ul>...
Constructor for Objects <ul><li>/** Print an impersonal greeting message </li></ul><ul><li>*  @author James Brucker  </li>...
Methods define Behavior <ul><li>/**  Print an impersonal greeting message </li></ul><ul><li>*  @author James Brucker  </li...
Another method <ul><li>/**  Print an impersonal greeting message </li></ul><ul><li>*  @author James Brucker  </li></ul><ul...
Creating Greeting Objects <ul><li>/**  Test the Greeting class.  </li></ul><ul><li>*/ </li></ul><ul><li>public class   Tes...
Sending a Message to an Object <ul><li>/**  Test the Greeting class.  </li></ul><ul><li>*/ </li></ul><ul><li>public class ...
Sending messages TestGreeting Greeting create sayHello( ) sayGoodbye( )
3 Fundamentals of  Object-Oriented Paradigm <ul><li>Encapsulation  - a class includes both the data and operations that op...
Examples
Upcoming SlideShare
Loading in...5
×

Programming with Objects Intro to object-orientation and how ...

836
-1

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
836
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
33
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Programming with Objects Intro to object-orientation and how ...

  1. 1. Programming with Objects Intro to object-orientation and how to use objects in Java James Brucker
  2. 2. The Realities of Software <ul><li>Useful, real-world software </li></ul><ul><li>1. Change </li></ul><ul><li>2. Complexity </li></ul>
  3. 3. The Problem <ul><li>&quot; Programming is fun, but developing quality software is hard. In between the nice ideas, the requirements or the &quot;vision&quot;, and a working software product, there is much more than programming . </li></ul><ul><li>Analysis and design, defining how to solve the problem, what to program, capturing this design [...], review, implement, and evolve is what lies in the core of this book. &quot; </li></ul><ul><li>[Forward to Larman, Applied UML and Patterns, 3E. Forward by Philipe Kruchten (IBM Rational)] </li></ul>
  4. 4. Intrinsic Complexity of Software <ul><li>About the future of software development... </li></ul><ul><li>&quot; T here is no single development, in either technology or in management technique, that by itself promises even one order of magnitude improvement in productivity, in reliability, in simplicit y .&quot; </li></ul><ul><li>&quot; No Silver Bullet &quot; by Frederick Brooks . Computer , 1987 </li></ul><ul><li>See also page 5-6 for Brooks comments about object-oriented approach. </li></ul>
  5. 5. Miller’s Law <ul><li>At any one time, a person can concentrate on at most 7  2 chunks (units of information) </li></ul><ul><li>To handle larger amounts of information, use stepwise refinement </li></ul><ul><ul><li>Concentrate on the aspects that are currently the most important </li></ul></ul><ul><ul><li>Postpone aspects that are currently less critical </li></ul></ul><ul><ul><li>Every aspect is eventually handled, but in order of current importance </li></ul></ul><ul><li>This is an incremental process </li></ul>
  6. 6. Benefit of Object-Orientation (1) <ul><li>Encapsulate complexity </li></ul><ul><ul><li>divide program into classes </li></ul></ul><ul><ul><li>each class has its own responsibilities and data </li></ul></ul><ul><ul><li>class has only one purpose </li></ul></ul><ul><ul><li>class has simple interface </li></ul></ul><ul><ul><li>hide implementation details </li></ul></ul>
  7. 7. Benefit of Object-Orientation (2) <ul><li>Encapsulate change </li></ul><ul><ul><li>each class presents only a simple public interface </li></ul></ul><ul><ul><li>class hides implementation details </li></ul></ul><ul><li>as a result... </li></ul><ul><ul><li>we can localize the effect of change </li></ul></ul>
  8. 8. Benefit of Object-Orientation (3) <ul><li>Reuse code </li></ul><ul><ul><li>classes are reusable </li></ul></ul><ul><ul><li>polymorphism lets us interchange parts </li></ul></ul><ul><ul><li>inheritance lets us build new classes that reuse code from old classes. </li></ul></ul>
  9. 9. Benefit of Object-Orientation (4) <ul><li>Better abstraction </li></ul><ul><ul><li>objects make good models for things in the real world (problem domain) </li></ul></ul><ul><ul><li>let us think about the problem instead of the code </li></ul></ul><ul><ul><li>simplify the problem so we can think about problem without too many details </li></ul></ul>
  10. 11. Objects and Programs <ul><li>An OO program consists of objects that interact with each other. </li></ul>
  11. 12. Classes <ul><li>A class is a blueprint or definition for a kind of object . </li></ul><ul><li>A class defines the attributes and behavior of objects. </li></ul>Cat birthday color sex sleep( ) eat( Food ) play( ) chase( Object ) attributes are characteristics of objects. In Java: variables, &quot;fields&quot; behavior is what the object can do. In Java: methods
  12. 13. Objects <ul><li>Objects are instances of a class. </li></ul><ul><li>In Java, to create a new object use &quot;new&quot;, e.g. </li></ul><ul><li> Cat somecat = new Cat( ); </li></ul><ul><ul><li>create the object in memory </li></ul></ul><ul><ul><li>invoke the constructor of Cat class to initialize values. </li></ul></ul><ul><li>Object creation can use values, too: </li></ul><ul><li>Cat kitten = new Cat( Color.BLACK, &quot;male&quot;, ... ); </li></ul>
  13. 14. 3 Fundamental Characteristics of Objects <ul><li>Objects have: </li></ul><ul><li>state - the current condition of an object </li></ul><ul><li>behavior - the actions or messages an object can accept </li></ul><ul><li>identity - every object is distinguishable, even if two objects have the same state </li></ul><ul><li>How to use the methods of an object: </li></ul><ul><li>// call a method in the same object: </li></ul><ul><li>turnLeft( ); </li></ul><ul><li>canMove( ); </li></ul>
  14. 15. Bank Account Example <ul><li>BankAccount class is the definition for bank accounts. </li></ul>class name : BankAccount attributes : accountNumber, name, balance behavior : getBalance( ), credit(amount), debit(amount), getName( ) UML class diagram BankAccount name: String accountNumber: String balance: double <<constructor>> BankAccount( name ) getBalance( ) credit(amount: double ) debit( amount: double ) getName( )
  15. 16. Bank Account Object <ul><li>An object is an actual instance of the class. </li></ul><ul><li>rich is a bank accoount </li></ul><ul><li>Example : </li></ul>BankAccount rich = new BankAccount(&quot;Taksin Shinawat&quot; ); rich.credit( 2000000000 ); rich.credit( 1000000000 ); // needs money to buy a football team... rich.debit( 500000000 ); // does he have enough to buy Hawaii? rich.getBalance( ) ; // = 2,500,000,000 UML object diagram rich: BankAccount name = Taksin Shinawat accountNumber = 000001 balance = 250000000 getBalance( ) credit(double amount) debit(double amount) getName( )
  16. 17. Object Properties <ul><li>Example: </li></ul><ul><li>For the &quot;rich&quot; object, what are... </li></ul><ul><ul><li>state ? </li></ul></ul><ul><ul><li>behavior ? </li></ul></ul><ul><ul><li>identity ? </li></ul></ul>
  17. 18. HondaCivic Example <ul><li>The definition of a HondaCivic consists of: </li></ul><ul><ul><li>specifications </li></ul></ul><ul><ul><li>design documents </li></ul></ul><ul><ul><li>blue prints </li></ul></ul><ul><ul><li>list of parts </li></ul></ul><ul><ul><li>list of qualified suppliers </li></ul></ul><ul><ul><li>instructions for assembly </li></ul></ul><ul><ul><li>inspection procedure and check lists </li></ul></ul><ul><ul><li>operating instructions </li></ul></ul><ul><ul><li>maintenance manual and procedures </li></ul></ul>
  18. 19. HondaCivic class <ul><li>But, the Honda Civic owner doesn't need to know all these details. He needs to know about a Honda Civic's properties (public attributes) and behavior . </li></ul><ul><li>For example (simplified): </li></ul>properties (attributes) behavior HondaCivic bodyStyle bodyColor trimColor engineSize doors transmissionType turnLeft( ) turnRight( ) brake( ) accelerate( ) isEngineOn( ) fuelAmount( )
  19. 20. Buying A Honda Civic <ul><li>You go to the Honda dealer and say... </li></ul>I want a Honda Civic Yes, sir. This way, please...
  20. 21. Buying A Honda Civic (2) <ul><li>the dealer offers you the class for a Honda Civic... </li></ul>Here you are! All the documents and blue prints for a Honda Civic. ... that will be 1,000,000,000 Baht, please. Construction and operation of a Honda Civic: complete documents.
  21. 22. Buying A Honda Civic (3) <ul><li>but you can't drive blue prints and documents </li></ul>That's not exactly what I had in mind. I want a car I can drive ... I see... you want an instance of Honda Civic -- a Honda Civic object .
  22. 23. Buying A Honda Civic (4) Silver, 4 door, automatic transmission, tinted windows, ... yourcar = new HondaCivic (&quot;silver&quot;, 4door, automatic,... ); attributes behavior yourcar : HondaCivic bodyStyle = sedan bodyColor = silver trimColor = silver engineSize = 1600cc doors = 4 transmissionType = auto turnLeft( ) turnRight( ) brake( ) accelerate( ) isEngineOn( ) fuelAmount( )
  23. 24. Review: Class versus Objects HondaCivic Defines the properties and behavior for all instances (objects) of this class. Specific realization of the class
  24. 25. Object Identity Example <ul><li>Primitive data types don't have identity. </li></ul><ul><li>You cannot distinguish two primitives when the values are same. </li></ul><ul><ul><li>primitive variables represent values not objects </li></ul></ul>double x = 10; double y = 10; if ( x == y ) System.out.print(&quot;same&quot;); // true
  25. 26. Object Identity Example <ul><li>Objects do have identity. </li></ul><ul><li>You can distinguish two objects even when the values are the same. </li></ul><ul><ul><li>object variable is a reference to an object </li></ul></ul>Double x = new Double(10); Double y = new Double(10); if ( x == y ) System.out.print(&quot;same&quot;); // false
  26. 27. Object Identity Example (2) <ul><li>Two Honda Civic cars can be distinguished even if they exactly the same features and same state (brand new) </li></ul>!=
  27. 28. Second Program <ul><li>/** Print an impersonal greeting message </li></ul><ul><li>* @author James Brucker </li></ul><ul><li>*/ </li></ul><ul><li>public class Greeting { </li></ul><ul><li>private String who; </li></ul><ul><li>/** constructor for new objects */ </li></ul><ul><li>public Greeting ( String name ) { </li></ul><ul><li>who = name; // save the name </li></ul><ul><li>} </li></ul><ul><li>public void sayHello( ) { </li></ul><ul><li>System.out .println(&quot;Hello, &quot;+who); </li></ul><ul><li>} </li></ul><ul><li>public void sayGoodbye( ) { </li></ul><ul><li>System.out .println(&quot;Goodbye, &quot; </li></ul><ul><li>+who); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Any text placed inside /* .... */ is ignored by the compiler. This is a Javadoc comment. An attribute of this class. Each object will have a variable named &quot; who &quot; that contains data unique to each object (instance ) of this class . An attribute is a property of an object -- information the object uses to do its job.
  28. 29. Constructor for Objects <ul><li>/** Print an impersonal greeting message </li></ul><ul><li>* @author James Brucker </li></ul><ul><li>*/ </li></ul><ul><li>public class Greeting { </li></ul><ul><li>private String who; </li></ul><ul><li>/** constructor for new objects */ </li></ul><ul><li>public Greeting ( String name ) { </li></ul><ul><li>who = name; // save the name </li></ul><ul><li>} </li></ul><ul><li>public void sayHello( ) { </li></ul><ul><li>System.out .println(&quot;Hello, &quot;+who); </li></ul><ul><li>} </li></ul><ul><li>public void sayGoodbye( ) { </li></ul><ul><li>System.out .println(&quot;Goodbye, &quot; </li></ul><ul><li>+who); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>A constructor for the class. The constructor is a method that is called when a new object is created. A constructor usually initializes the attributes of an object, but does not return any value ... not even a void.
  29. 30. Methods define Behavior <ul><li>/** Print an impersonal greeting message </li></ul><ul><li>* @author James Brucker </li></ul><ul><li>*/ </li></ul><ul><li>public class Greeting { </li></ul><ul><li>private String who; </li></ul><ul><li>/** constructor for new objects */ </li></ul><ul><li>public Greeting ( String name ) { </li></ul><ul><li>who = name; // save the name </li></ul><ul><li>} </li></ul><ul><li>public void sayHello( ) { </li></ul><ul><li>System.out .println(&quot;Hello, &quot;+who); </li></ul><ul><li>} </li></ul><ul><li>public void sayGoodbye( ) { </li></ul><ul><li>System.out .println(&quot;Goodbye, &quot; </li></ul><ul><li>+who); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>A method of the class. Methods can access the attributes of the object that they belong to. This method uses the who attribute. The value of who was set by the constructor. Methods are the actions that an object can perform. Methods define behavior or responsibilities of objects.
  30. 31. Another method <ul><li>/** Print an impersonal greeting message </li></ul><ul><li>* @author James Brucker </li></ul><ul><li>*/ </li></ul><ul><li>public class Greeting { </li></ul><ul><li>private String who; </li></ul><ul><li>/** constructor for new objects */ </li></ul><ul><li>public Greeting ( String name ) { </li></ul><ul><li>who = name; // save the name </li></ul><ul><li>} </li></ul><ul><li>public void sayHello( ) { </li></ul><ul><li>System.out .println(&quot;Hello, &quot;+who); </li></ul><ul><li>} </li></ul><ul><li>public void sayGoodbyte( ) { </li></ul><ul><li>System.out .println(&quot;Goodbye, &quot; </li></ul><ul><li>+who); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Another method of the class. No main method!
  31. 32. Creating Greeting Objects <ul><li>/** Test the Greeting class. </li></ul><ul><li>*/ </li></ul><ul><li>public class TestGreeting { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li>Greeting a = new Greeting(&quot;John&quot;); </li></ul><ul><li>Greeting b = new Greeting (&quot;Nok&quot;); </li></ul><ul><li>a. sayHello ( ); </li></ul><ul><li>b. sayHello ( ); </li></ul><ul><li>b. sayGoodbye ( ); </li></ul><ul><li>a. sayGoodbye ( ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Create a Greeting object . Create another Greeting object . We'll create a separate &quot;test&quot; class (in its own file) that creates and uses Greeting objects.
  32. 33. Sending a Message to an Object <ul><li>/** Test the Greeting class. </li></ul><ul><li>*/ </li></ul><ul><li>public class TestGreeting { </li></ul><ul><li>public static void main(String[] args) { </li></ul><ul><li>Greeting a = new Greeting(&quot;John&quot;); </li></ul><ul><li>Greeting b = new Greeting (&quot;Nok&quot;); </li></ul><ul><li>a. sayHello ( ); </li></ul><ul><li>b. sayHello ( ); </li></ul><ul><li>b. sayGoodbye ( ); </li></ul><ul><li>a. sayGoodbye ( ); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Invoke sayHello method of the objects. Output: Hello, John. Hello, Nok. Goodbye, Nok. Goodbye, John. Notice that each object remembers its own attributes. In object-oriented speaking, people say &quot; we send a message to an object &quot; to ask it to do something..
  33. 34. Sending messages TestGreeting Greeting create sayHello( ) sayGoodbye( )
  34. 35. 3 Fundamentals of Object-Oriented Paradigm <ul><li>Encapsulation - a class includes both the data and operations that operate on the data. It can hide the implementation details. </li></ul><ul><li>A user of the class only sees the public interface. </li></ul><ul><li>Polymorphism - many classes can perform the same behavior and we can use them interchangably. </li></ul><ul><li>Inheritance - one class can build on top of another class and inherit all its methods and attributes. </li></ul>
  35. 36. Examples
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×