Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. Script of our presentation:
  2.  
  3. 1) I start with a story about how we chose this theme ( 1st slide )
  4. 2) Tell everyone that the goal of our speech is not educational (2nd slide)
  5. 3) Encapsulation :
  6. Say we have a program. It has a few logically different objects which communicate with each other — according to the rules defined in the program.
  7.  
  8. Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions — called methods.
  9.  
  10. So, the object manages its own state via methods — and no other class can touch it unless explicitly allowed. If you want to communicate with the object, you should use the methods provided. But (by default), you can’t change the state.
  11.  
  12. Let’s say we’re building a tiny Sims game. There are people and there is a cat. They communicate with each other. We want to apply encapsulation, so we encapsulate all “cat” logic into a Cat class. It may look like this:
  13.  
  14. PICTURE WITH A CAT
  15.  
  16. Here the “state” of the cat is the private variables mood, hungry and energy. It also has a private method meow(). It can call it whenever it wants, the other classes can’t tell the cat when to meow.
  17.  
  18. What they can do is defined in the public methods sleep(), play() and feed(). Each of them modifies the internal state somehow and may invoke meow(). Thus, the binding between the private state and public methods is made
  19.  
  20. 4) Abstraction:
  21. Abstraction can be thought of as a natural extension of encapsulation.
  22.  
  23. In object-oriented design, programs are often extremely large. And separate objects communicate with each other a lot. So maintaining a large codebase like this for years — with changes along the way — is difficult.
  24.  
  25. Abstraction is a concept aiming to ease this problem.
  26.  
  27. Applying abstraction means that each object should only expose a high-level mechanism for using it.
  28.  
  29. Think — a coffee machine. It does a lot of stuff and makes quirky noises under the hood. But all you have to do is put in coffee and press a button.
  30.  
  31. Another real-life example of abstraction?
  32. Think about how you use your phone:
  33.  
  34. PIC OF A PHONE
  35.  
  36. You interact with your phone by using only a few buttons. What’s going on under the hood? You don’t have to know — implementation details are hidden. You only need to know a short set of actions.
  37.  
  38. 5)Inheritance : !!!!!!!!!!!!!! VITALY'S PART BEGINS HERE !!!!!!!!
  39.  
  40. OK, we saw how encapsulation and abstraction can help us develop and maintain a big codebase.
  41.  
  42. But do you know what is another common problem in OOP design?
  43.  
  44. Objects are often very similar. They share common logic. But they’re not entirely the same. Ugh…
  45.  
  46. So how do we reuse the common logic and extract the unique logic into a separate class? One way to achieve this is inheritance.
  47.  
  48. It means that you create a (child) class by deriving from another (parent) class. This way, we form a hierarchy.
  49.  
  50. The child class reuses all fields and methods of the parent class (common part) and can implement its own (unique part).
  51.  
  52. For example:
  53.  
  54. PIC WITH TEACHERS
  55.  
  56. 6) Polymorphism:
  57.  
  58. We’re down to the most complex word! Polymorphism means “many shapes” in Greek.
  59.  
  60. So we already know the power of inheritance and happily use it. But there comes this problem.
  61.  
  62. Say we have a parent class and a few child classes which inherit from it. Sometimes we want to use a collection — for example a list — which contains a mix of all these classes. Or we have a method implemented for the parent class — but we’d like to use it for the children, too.
  63.  
  64. This can be solved by using polymorphism.
  65.  
  66. Simply put, polymorphism gives a way to use a class exactly like its parent so there’s no confusion with mixing types. But each child class keeps its own methods as they are.
  67.  
  68. This typically happens by defining a (parent) interface to be reused. It outlines a bunch of common methods. Then, each child class implements its own version of these methods.
  69.  
  70. Any time a collection (such as a list) or a method expects an instance of the parent (where common methods are outlined), the language takes care of evaluating the right implementation of the common method — regardless of which child is passed.
  71.  
  72. Take a look at a sketch of geometric figures implementation.
  73.  
  74. PICTURE
  75.  
  76. Having these three figures inheriting the parent Figure Interface lets you create a list of mixed triangles, circles, and rectangles. And treat them like the same type of object.
  77.  
  78. Then, if this list attempts to calculate the surface for an element, the correct method is found and executed. If the element is a triangle, triangle’s CalculateSurface() is called. If it’s a circle — then cirlce’s CalculateSurface() is called. And so on.
  79.  
  80. If you have a function which operates with a figure by using its parameter, you don’t have to define it three times — once for a triangle, a circle, and a rectangle.
  81.  
  82. You can define it once and accept a Figure as an argument. Whether you pass a triangle, circle or a rectangle — as long as they implement CalculateParamter(), their type doesn’t matter.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement