1 / 76

Concept of Encapsulation

Concept of Encapsulation. What is encapsulation? - data and functions/methods are packaged together in the class normally with the same scope of the accessibility in the memory Why encapsulation

annora
Download Presentation

Concept of Encapsulation

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. YG - CS170

  2. YG - CS170

  3. YG - CS170

  4. YG - CS170

  5. YG - CS170

  6. YG - CS170

  7. YG - CS170

  8. Concept of Encapsulation • What is encapsulation? - data and functions/methods are packaged together in the class normally with the same scope of the accessibility in the memory • Why encapsulation - Objects no longer need to be dependent upon any “outside” functions and data after the creation - Provide ADT (Abstract Data Types) - Easier in coding - Promote standards in OOP YG - CS170

  9. YG - CS170

  10. YG - CS170

  11. YG - CS170

  12. YG - CS170

  13. Classes vs. Objects • Class is a data structure that encapsulates data and functions. • Data in a class are called member data, class variables, instance variables, or fields. • Functions in a class are called member functions or methods. Constructors are special methods. • Objects are the instance of a class. Creation of the objects and instantiation of the objects are all referring to memory allocation to hold the data and methods of the objects. In Java, all objects are created dynamically using new operator. For example: ClassName ObjName = new ClassName(); YG - CS170

  14. YG - CS170

  15. YG - CS170

  16. YG - CS170

  17. YG - CS170

  18. YG - CS170

  19. YG - CS170

  20. YG - CS170

  21. YG - CS170

  22. YG - CS170

  23. Characteristics of Constructors • A special method that has the same name of the class without return type, not even void. • Constructor will be automatically called whenever an object is created. • Like a method, constructor can be overloaded for the flexibility of the object creation. • A super class constructor can be called by a sub class object as: super(); YG - CS170

  24. YG - CS170

  25. YG - CS170

  26. get methods • In OOP, particularly in Java, get() means to return one of object’s member data • get() usually has a return type and takes no argument • Use meaningful name for a get() method YG - CS170

  27. set methods • In OOP, particularly in Java, set() means to set object’s member data • set() usually has void return and takes argument(s) to set the member data • set() also checks the validity of the data before it sets • Use meaningful name for a set() method YG - CS170

  28. YG - CS170

  29. YG - CS170

  30. YG - CS170

  31. YG - CS170

  32. YG - CS170

  33. YG - CS170

  34. YG - CS170

  35. YG - CS170

  36. YG - CS170

  37. YG - CS170

  38. YG - CS170

  39. YG - CS170

  40. YG - CS170

  41. YG - CS170

  42. YG - CS170

  43. YG - CS170

  44. YG - CS170

  45. YG - CS170

  46. YG - CS170

  47. YG - CS170

  48. YG - CS170

  49. YG - CS170

  50. YG - CS170

More Related