Java is a programming language based on OOP concepts (object-oriented programming). In other words, it uses objects to define data and their behavior. This makes the code more flexible and increases readability. It also helps developers understand the code better. If you are a beginner, check out this list of the best free Java courses on the internet!
In this article, we are going to present the basic Java OOP concepts. One of the important features of Java Object-Oriented programming is the object. An object represents a real-life entity that is an instance of a class. It contains both, attributes and methods (behaviors). Objects usually have two or three attributes and methods (behavior).
An example of a real-life object:
- Object: a car
- Attributes: a name, model, color
- Methods (behavior): driving, turn on, turn off
The main Java OOP Concepts
Before we make a Car object, we need to make the class called Car. We are going to use this class as a template to create our object. As you can see, the class Car has three attributes, name, model, and color. The class also contains three methods, driving(), turnOn(), and turnOff(). So, now we have a template. The only thing we need to do is to create an instance of the class – the car object!
public class Car {
// Attributes
String name;
String model;
String color;
// Methods
public void driving() {
}
public void turnOn() {
}
public void turnOff() {
}
public static void main(String[] args) {
Car m = new Car(); // This is an instance of the class Car
}
}
To create a new Car object, we need the keyword new. The keyword should be placed within the main method. When we type new Car(), we actually call the default constructor of the Car class to create our first object. Next, we assign the object to the variable m which is type Car. That’s it! We have created our first car object.
In the previous example, we have created an object called Car. Through that object, we can add new names and colors to the car or activate a certain behavior. Now is the time to talk about the four basic principles of Java OOP.
Abstraction
Abstraction is one of the main OOP concepts in Java. We use abstraction to define which type of data should be visible and which can be hidden. For example, when we use a mobile phone, we simply know its behavior, but we don’t know how the internal mechanism works. For this, we use abstract classes and interfaces.
An abstract class contains only the method declaration and no implementation. For this, we need to use inheritance and implement the functionality of the abstract method by extending the abstract class.
On the other hand, an interface contains a set of abstract methods. It doesn’t contain a method implementation, it only has a method definition. To implement the functionality of these abstract methods, we need to use inheritance.
One difference between an abstract class and an interface is that an interface cannot have abstract methods.
Encapsulation
Encapsulation is another Java OOP concept that protects a class’ attributes and methods from external interference. It doesn’t allow other classes to access or update these attributes and methods directly.
To achieve this, we declare variables as private variables. Therefore, we use access modifiers such as public, private, or protected. We cannot access private variables directly from other classes. So, encapsulation helps us to hide sensitive data from users. This is also called data hiding.
Inheritance
Simply put, inheritance is when one class can reuse methods and variables of another class. This class can reuse the data and functions of other classes by extending to that class using the keyword extend.
The parent class is called the base class or superclass and the class that extends the parent class is called the child class or subclass or the derived class. In other words, using this concept of OOP in Java, we can define a new class using an existing class by inheriting all the variables and methods.
Polymorphism
Polymorphism, as the name implies, means “multiple forms.” This is another important concept of Java OOP that is often used in software development in which we can implement the same method differently.
For example, each type of bike may have a different speed capacity. Slow bikes like the TVS 50 are slow compared to medium-speed bikes like the Honda Access. Even this type of bike is slow compared to fast bikes like the Pulsar. Therefore, although all three bicycles have speed, the implementation differs from each other.