Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Programming in Java: Object and Classes

129 views

Published on

Programming in Java: Object and Classes

Published in: Education
  • Be the first to comment

Programming in Java: Object and Classes

  1. 1. OBJECTS AND CLASSES Martin Chapman martin.chapman@kcl.ac.uk
  2. 2. •Fundamental principle: •The difference between blueprints and buildings
  3. 3. •Fundamental principle: •The difference between blueprints and buildings data type variable variable
  4. 4. •Fundamental principle: •The difference between blueprints and buildings int num1 = 9 num2 = 20
  5. 5. •Fundamental principle: •The difference between blueprints and buildings class object object
  6. 6. •There are two predominant ways to understand Object Orientation: •Understand how we do it. •Understand why we do it.
  7. 7. How do we do it?
  8. 8. class object
  9. 9. class object
  10. 10. class object
  11. 11. class object
  12. 12. class object
  13. 13. 1. Make a class with methods and variables 2. Make an object (a variable) to link to that class 3. Reference the object to use the methods you made in Step 1.
  14. 14. 1. Make a class with methods and variables 2. Make an object (a variable) to link to that class 3. Reference the object to use the methods you made in Step 1.
  15. 15. Among other reasons... •It makes sense •Code reuse •State and behaviour •It gives us control •Constructors •Encapsulation
  16. 16. Among other reasons... •It makes sense •Code reuse •State and behaviour •It gives us control •Constructors •Encapsulation
  17. 17. Dave Victoria
  18. 18. Codereuse
  19. 19. Codereuse
  20. 20. Codereuse
  21. 21. davesCar victoriasCar Codereuse
  22. 22. blue davesCar victoriasCar Codereuse
  23. 23. blue black davesCar victoriasCar Codereuse
  24. 24. Codereuse
  25. 25. blue black davesCar victoriasCar Codereuse
  26. 26. Among other reasons... •It makes sense •Code reuse •State and behaviour •It gives us control •Constructors •Encapsulation
  27. 27. Drive Colour Speed Number of wheels Number plate Height Stateandbehaviour Paint
  28. 28. •All the information we want our program to have about this object can be categorised as: •“How the object is” (~Nouns / Adjectives). •Colour, numberplate, speed and number of wheels etc. •“What can be done to the object” / “What the object can do” (~Verbs). •Drive and be painted Stateandbehaviour
  29. 29. •All the information we want our program to have about this object can be categorised as: •“How the object is”. •Colour, numberplate, speed and number of wheels etc. •“What can be done to the object” / “What the object can do”. •Drive and be painted STATE BEHAVIOUR Stateandbehaviour
  30. 30. STATE BEHAVIOUR BEHAVIOUR BEHAVIOUR BEHAVIOUR Stateandbehaviour
  31. 31. VARIABLES METHOD METHOD METHOD METHOD Stateandbehaviour Behaviour Behaviour Behaviour Behaviour State
  32. 32. FIELDS METHOD METHOD METHOD METHOD Stateandbehaviour StateVariables (Properties)
  33. 33. Among other reasons... •It makes sense •Code reuse •State and behaviour •It gives us control •Constructors •Encapsulation
  34. 34. CONSTRUCTOR A method which has the same name as the class is called an explicit Constructors
  35. 35. Constructors
  36. 36. Constructors
  37. 37. Constructors
  38. 38. Among other reasons... •It makes sense •Code reuse •State and behaviour •It gives us control •Constructors •Encapsulation
  39. 39. Encapsulation Private Public Only visible inside the class Visible anywhere
  40. 40. Encapsulation
  41. 41. Encapsulation
  42. 42. Encapsulation Interface
  43. 43. Encapsulation
  44. 44. Encapsulation
  45. 45. • Object orientation, in its simplest form, is code within code. • By programming in an OO manner, we are designing code in a way that both makes sense and gives us control. • OOP makes sense because it allows for efficient reuse and an accurate representation of the world using state and behaviour. • OOP gives us control through constructors and because it enforces encapsulation (a black-box approach to object design in which operations are performed through well-defined method interfaces). SUMMARY

×