4 October 2020

Introduction to Object-Oriented Programming concept

Welcome to another tutorial on the "How to code in Java" tutorial series. In the last tutorial, we have gone through with what methods are in Java. In this tutorial, we will be continuing with the concept of Object-Oriented Programming, OOP. As this tutorial is aimed for beginners, we won't be going into too deep on the concept, just scratching the surface of the OOP concept would be enough for the moment.

Well, let's first explain what OOP is. Object-oriented programming or OOP is one of the many programming paradigms. The OOP concept is focused on using real-world entities to solve problems. While providing some concept that lets developers develop and maintains software easier:

Besides these concepts, some design concepts are also used to help in solving problems when using OOP concept. Such as composition, aggregation, coupling and many more. 

Programming languages like Java, C#, Python, PHP, etc., are examples of popular languages that support OOP.





Classes


Now, let's get started with a brief introduction to the OOP concepts, let's start with classes.

You could imagine a class as a blueprint to create objects. As class are the blueprints to create an object, we could use this blueprint to create multiple objects. You can also think a class as a way to classify a group of objects. 

For example, how do we humans classify that an animal is a dog? Well, dogs have tails, a pair of ears, a nose on the front, a big mouth, a pair of eyes, they can bark and many more. That's what a class is. All the dogs have the same traits, which is why they're in the same class. But all of them have their own uniqueness, which could be used to differentiate them from each other. For example, their fur and skin, their behaviour, and their preferences are all different.

A class is constructed from two things; properties and methods. As explained in the previous article, a method is like the possible behaviour of an object. Because a dog can bark, walk, sleep, eat, and many more, we will need to create a method for all of them depending on our needs.

Properties or attributes are the unique properties of an object, such as a dog's fur colour, its eye colour, it's weight, height and many more. Although some objects would have similar properties with another object, they are still different entities. That's because they can be used independently and modifying the properties inside one object won't affect the other objects (but not the case for static properties).

In some of the previous tutorial, we have already used some classes that's in the Java library. Such as the Scanner class. Classes that's already in Java's library can be used right away, you just need to import them into your Java program, no additional configuration is required. 


Objects 


Objects are created from a class, where each object is unique from each other. The methods that are defined in the class of an object can be used. While the properties in an object could be used or changed through the object. But the changed value will only affect the values inside the object and doesn't affect other objects (not the case for static properties and methods).


Abstraction


The abstraction concept is implemented when programmers show only important information and hide those that don't need to be seen. The concept hides the internal details and only shows the functionality of the object's behaviour.


Encapsulation


Encapsulation is the concept where the code and data are wrapped together. Encapsulation can be achieved by exposing public methods and properties for other classes to use. This makes sure that the methods, properties and attributes provide their intended use to other classes. 

The benefit of using encapsulation is that programmers could change the internal implementation of the code inside the class while not damaging other functioning codes.


Inheritance


Inheritance happens when a class has taken the properties and methods from another class while adding additional properties and/or methods to extend it. The new class that inherited the properties and methods from another class is called the derived/child/sub/descendants class. While the class that has been inherited from is called the base/parent/super/ancestor class.

Inheritance let programmer reuse code because they could extend the properties and methods to add, modify or use the methods or properties for the new class's object. 


Polymorphism


Polymorphism in general means "many shapes". In OOP, polymorphism means to be able to perform one action in many different ways. This allows us to redefine the method to provide the class with a completely new implementation of the action.

For example, all animal can speak but in different ways. Cat speaks by saying "meow", while dogs speak by barking.


Well, that's about all the introduction I could talk about on OOP, in the following tutorials will explain in more detail using Java to demonstrate the concepts. If you find this article helpful, please share it with others that might find it useful. Like always, if you have any questions, critics or suggestions, feel free to leave a comment in the comment section below. We'll be learning more about classes and object in the next tutorial. So stay tuned.

No comments:

Post a Comment