Java - Objective-Oriented Programming principles
So, the last time we left off, we were introduced to writing our first piece of Java code and what that looks like. Now, we will dive into some of the core principles of Java; objected-oriented concepts. These core concepts include Abstraction, Encapsulation, Inheritance, and Polymorphism. With Java being an Object-Oriented Programming (OOP) language, to make things simple, it just means Java uses real-world objects which are represented while writing code. For instance, if you wish to write something dealing with payroll for employees, then you would use either people, employees, workers, or any other words to describe humans in the workplace.
Let's start with the first of the four pillars of OOP, Abstraction:
1) Abstraction is simply hiding internal details and showing only what is required. Let's use a TV as our example. The only thing revealed to the customer would be the buttons(in Java, we call these methods) needed to make changes to a device's functions such as turning the device off/on, changing stations and etc. The components of the remote are hidden from the user because the user does not need to know about the internal workings, just the functionality.
2) Encapsulation is the process of grouping data into a single section. In Java, curly braces {} keep our data within our class(es) contained. This allows for our information to be easier to read and decipher from different sections of our code.
领英推è
3) Inheritance, which we touched on in our previous article on Java, means to design a class(Parent) and create other classes(Child) to 'inherit' or reuse properties associated with the Parent class. Think specialization, for example, Toyota has the Corolla, Camry, Crown, and Prius brands which contain many of the same features as any standard car would have, but they are different in their own unique way.
4) Finally, Polymorphism is a concept in which we execute a single operation in different ways(think generalization). TVs (our general object) have different brands such as LG, Samsung, and Sony. Within Java, overriding a method(runtime polymorphism) allows the Java Virtual Machine(JVM) to call the appropriate method for a class(Child) that inherits from a Parent class; given the Child class has unique characteristics of its own.
Well, I hope the concept of OOP was explained to you in a way you can understand. Trust me, it takes time to really get these concepts down. Not to mention, many programmers disagree(lol) about polymorphism from time to time. The important thing is to continue to learn and try to understand these concepts the best way you can. So, until next time, keep learning and strap on those seat belts because we will start diving into coding examples(illustrations) to bring these ideas home.