Introductioon to Object Oriented Programming In Kotlin


Kotlin supports Object Oriented Programming(OOP) as a paradigm. OOP is a powerful way to approach programming. Abstraction using OOP has helped alleviate Software Complexity over the last few decades.

Programming methodologies have changed dramatically since the invention of computers. In the early years of computers, programming was acheieved by toggling binary machine instructions.

Then programs started growing in complexity and assembly language was invented.Assembly programming involved using symbolic representations of the machine instructions.

Then the era of high-level languages started with the invention of FORTRAN and COBOL. These provided even better ways of handling complexity. A few years later structured languages like C were invented and provided programmers with even better tools and methodologies of structing large programs.

However, structural programming with even a language as powerful as C was reaching its breaking point and Object Oriented programming was introduced with Smalltalk programming language.

OOP took the best ideas of structured programming and combined them with several new concepts. The end result was a better way of organizing program with several advantages.

With OOP model problems in terms of Objects. An Object basically has state or data and also can perform actions.

Advantages of Object Oriented Programming

1.OOP programs are simple. This is from the fact that the domain objects in OOP model real world entities. This gives a simple mental model to streamline the program structure.
2.OOP programs are maintable. If you are to change a given object, this doesn't affect other objects. Hence objects can be modified easily. This makes OOP programs very maintainable especially large complex programs.
3.OOP programs are modular. The internal workings of each object is decoupled from the other objects.
4.Objectc Orineted Programs are reusable. Already designed objects can be reused several times by other objects in the same program or in other programs.
5.OOP programs are extensible. You can add new objects as new functionalities are required. Even existing objects can be extended with new functionality easily.

Pillars of Object Oriented Programming

Kotlin supports the three main pillars of Object Oriented Programming:

  • Encapsulation - Group of related fields and data are treated as an object.
  • Polymorphism - You can use different classes interchangeably even if each has different method implementations
  • Inheritance - Classes can derive properties from other classes.

Encapsulation

Encapsulation as a programming mechanism that involves binding together code and the data it manipulates. This decouples them from outside classes. Encapsulation involves grouping together code and data as a self-contained entity. This linked together of code and data is called an object.
Inside of the object, code or data may be private or public to that object.

  • Code/Data that is private is only known and accessible by only another part of that object. Pricate code or data cannot be accessed by pieces of program outside that object.
  • Code/data that is public is accessible by other parts of the program as well, not only the current object.

Generally in OOP public parts of an object are used to provide a controlled interface to the private elements.
The basic unit of encapsulation in object oriented programming is a class. Classes are the blueprint for the creation of objects. Objects are therefore the instances of a class. Classes specify both data and code that will operate on that data.
The collective constitution of code and data inside a class are what is called the class's members. The data that is defined by the class are referred to as the fileds. We can also use the terms member variables and instance variables.
The code that will operate on these members are defined inside function members. These function members are what we called methods. The program's logic are usually defined inside of the methods.

Polymorphism

Polymorphism comes from the word polymorphic, which means many forms. It's one of the main pillars of object oriented programming. It allows one interface to access a general class of actions. Take the example of say a jumbo jet. We can consider the jet engine as the interface. The engine will work no matter what engine from which manufacturer is used. As long as it is a jumbo jet engine. No matter whether it's built by Rolls Royce, General Electric or Pratt and Whitney. The interface for starting, stopping or operating the engine is the same for the pilot. The pilot does not need the knowledge of the different ways in which the engine manufacturers designed their engines. Once the pilot knows how to operate the engine, he won't care so much the type that's been installed.

In programming terms consider a list data structure. The algorithm for manipulating the list is the same irrespective of whether it holds integer or floating point numbers. The data differs but the implementation is the same.

Typically polymorphism is applied when creating generic interfaces to group related activities. Whereby you define one interface that has many methods.

Thus polymorphism helps alleviate software complexity. It enables one interface to define a general class of action. Then the compiler selects the specific action/method depending on the situation.

Inheritance

Object Oriented Programming involves working with objects. Just like a child can acquire genetic properties from the parent, these objects can also acquire the properties of another object. Object Oriented Programming and indeed Kotlin, support the concept of hierarchical classification. This is a top down classification where information flows from the parent to the child.
Inheritance this helps in tackling the issue of software complexities. This is because not every object has to explicitly define all of its properties. It only needs to define its unique properties. The other general attributes can be inherited from the parent.

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:




Recommendations


What do You Think

Dear readers drop us your comments below. We are building a community of students and learners. Start by dropping us your suggestions below. What tutorials do you want us to do for example? Where can we improve? What are some awesome resources out there? Do you have any code you want to share with us?
By the way that example or snippet you have lying in your computer can really help beginner programmers. We can share it here with other students.

Previous Post Next Post