官术网_书友最值得收藏!

Object-Oriented Programming in Kotlin

Kotlin is an object-oriented programming (OOP) language with support for higher-order functions. If you don't know what higher-order functions mean, then do not worry, as there is, Chapter 5Higher-Order Functions and Functional Programming, dedicated to them. If you have been using a functional language prior to Kotlin, then you will find many functional programming constructs supported in Kotlin.

Over time, software complexity has increased, and OOP abstraction has allowed us to model problems we have to solve in terms of objects. You can view each object as a minicomputer on its own—it has a state and can perform actions. An object through its available actions exhibits some sort of behavior; therefore, there is a clear analogy between objects/entities and real life.

The first characteristic of an object-oriented abstraction was defined by Alan Key, one of the creators of the first successful OOP language—Smalltalk. In his book, The Early History Of Smalltalk, he makes the following points:

  • Everything is an object: An object is nothing but a block of memory allocated and configured according to a design/definition. From the problem space you have to solve, you take all the logical entities and translate them into objects in your program.
  • Objects communicate by sending and receiving messages (in terms of objects): Your program will be a set of objects performing different actions as a result of calling methods that each one exposes.
  • Objects have their own memory (in terms of objects): This should be read as, you can create an object by composing other objects.
  • Every object is an instance of a class (which must be an object): Think of a class as a blueprint specifying what the type can do.
  • The class holds the shared behavior for its instances (in the form of objects in a program list): This means all the objects of a particular type can receive the same messages; in other words, they expose the same methods.

Kotlin provides full support for the preceding points but also supports fully the three pillars of any modern OOP language—encapsulation, inheritance, and polymorphism. Encapsulation means that a group of related fields and methods is treated as an object. Inheritance describes the capability of creating a new class from an existing one. Polymorphism means you can use different classes interchangeably despite the fact that each one implements its methods differently. Throughout this chapter, we will go into a bit more detail about how language constructs support this.

OOP abstraction is meant to help us alleviate the problems encountered with large code bases. This makes it easier for us to understand, maintain, and evolve code bases and keep them bug-free by providing us with the following:

  • Simplicity: Program objects model the real world, thus reducing complexity and streamlining the program structure
  • Modularity: Each object's internal workings are decoupled from other parts of the system
  • Modifiability: Changes inside an object do not affect any other part of a program if you have done your design right
  • Extensibility: An object's requirements change quite often, and you can quickly respond to them by adding new objects or modifying existing ones
  • Reusability: Objects can be used in other programs

In this chapter you will learn:

  • How to define and use classes and interfaces
  • When to choose interfaces over abstract classes
  • When to choose inheritance over composition
主站蜘蛛池模板: 绥德县| 义马市| 康保县| 丰顺县| 花莲市| 大兴区| 乳源| 山阴县| 太仆寺旗| 武鸣县| 保山市| 马公市| 清新县| 新宁县| 武平县| 昆明市| 南澳县| 桦川县| 抚宁县| 宾川县| 寿宁县| 盐源县| 祁门县| 手游| 林芝县| 正镶白旗| 通许县| 女性| 河池市| 玉田县| 香格里拉县| 芜湖县| 湘潭县| 阜康市| 苗栗市| 苏尼特右旗| 阳西县| 南溪县| 余江县| 黄大仙区| 宁津县|