OOP Concepts with Java
OOP stands for Object Oriented Programming which is very important for programming. You will not be able to create systems using the object-oriented programming model if you are unfamiliar with OOP concepts. The idea of objects serves as the central idea in the object-oriented programming paradigm. How do objects work? An object is an instance of a class. It has attributes and functions. They resemble actual objects. Your home, car, laptop, etc. are all examples of objects. They have some unique characteristics and ways to carry out certain tasks. Other than Java OOPs are used in other programming languages such as C++, Common Lisp, C#, Dart, Eiffel, Fortran 2003, Haxe, Java, JavaScript, Kotlin, logo, MATLAB, Objective-C, Object Pascal, Perl, PHP, Python, R, Raku, Ruby, Scala, SIMSCRIPT and etc.
Abstraction, encapsulation, inheritance, and polymorphism are the four major principles of Java’s object-oriented programming (OOP). Let’s closely look at one by one.
Abstraction
The object-oriented programming concept of abstraction “shows” only necessary properties and “hides” extraneous data. Abstraction’s fundamental goal is to shield people from pointless information. It aids in lowering programming effort and complexity. That enables the user to add more complex functionality on top of the abstraction offered without being aware of all the complexity that is concealed.
We can find two types of abstractions Data Abstraction and Control Abstraction.
- Data Abstraction
The majority of the time, we design sophisticated data types and conceal their implementation. Without going into the specifics of how these data types were implemented, we merely reveal the procedures for manipulating them.
- Control Abstraction
Control abstraction gathers and presents all of the application’s control assertions as a single entity. When we need to employ this control unit to accomplish a working feature, we use this feature.
Few real-world examples:
- Only the break will be seen in a car, internal parts will not be displayed.
- The interior of the car won’t be shown to us; just the straining wheel.
- Users only interact with the display and buttons in the ATM machine. The whole process behind it is hidden from the user.
//abstract class
abstract class Car{
abstract void accelerate();
}
//concrete class
class Toyota extends Car{
void accelerate(){
System.out.println("Toyota::accelerate");
}
}
class Main{
public static void main(String args[]){
Car car = new Toyota();
car.accelerate();
}
}
Encapsulation
Through encapsulation, limits data access for public methods by hiding data. Implementation to do this, accessor methods are made public and instance variables are kept private. Encapsulation can also be viewed as a barrier that stops code from the outside of the barrier from accessing the data. The accessor methods that we use are called getters and setters.
For example, let’s get a bank account. The attributes of the bank account should not be publicly visible. We can use encapsulation here.
public class Account {
private int id;
private double amount;
private String accountHolderName;
private String accountType;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
public String getAccountHolderName() {
return accountHolderName;
}
public void setAccountHolderName(String accountHolderName) {
this.accountHolderName = accountHolderName;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
}
public class Bank {
public static void main(String[] args) {
Account account = new Account();
account.setId(1);
account.setAmount(10000.00);
account.setAccountHolderName("John");
System.out.println(account.getAccountHolderName());
}
}
Inheritance
It is used to define the relationship between two classes and the process of deriving a class from another class. The child class inherits all of the characteristics and actions of the parent class. It allows for the reuse of code. You can use it to link your domain model to a database, declare various sorts of exceptions, and even customize existing frameworks.
public class Animal {
int legCount;
int bodyColor;
public void eat() {
System.out.println("Animal is eating");
}
public void sleep() {
System.out.println("Animal is Sleeping");
}
}
public class Cat extends Animal {
// cat extend from the Animal parent class
// all attributes and methods of Animal will inherites to Cat
@Override
public void eat() {
System.out.println("Cat is eating");
}
@Override
public void sleep() {
System.out.println("Cat is Sleeping");
}
}
Inheritance can be divided into four categories:
- Single inheritance
- Hierarchical inheritance
- Multilevel inheritance
- Multiple inheritance
Single inheritance
Single inheritance means just extending a class from another class. Only two classes participate. In the below example, class B will be the child class and class A will be the parent class.
Hierarchical inheritance
If you extend multiple classes from a single class that will be Hierarchical inheritance. There will be more than one child class for one parent class. In this example, class A will be the parent, and classes B and C will be the child classes.
Multilevel inheritance
In hierarchical inheritance, there should be involved more than two classes. One class extends from another class and that class extends from another class and so on. In this example class A will be the parent of class B and class B will be the parent of class C.
Multiple inheritance
If we extend a class from more than one class at the same time that will be multiple inheritances. In multiple inheritances, there should be more than one parent class. According to this example, the C class will be the child class and A and B classes will be the parent classes.
Java doesn’t support multiple inheritance for classes. But we can implement multiple inheritance using interfaces.
Polymorphism
It is the ability to behave in multiple forms. This type of situation happens when we have many classes which are related to each other by inheritance. This allows us to perform the same action in different ways. There are two types of polymorphism you can find in java.
- Static Polymorphism or Compile Time Polymorphism — (Method Overloading)
- Dynamic Polymorphism or Run Time Polymorphism — (Method Overriding)
Method Overloading
Overloading refers to using the same method name with a different parameter(we can call it different method signatures) inside the same class. This feature enables us to have many methods with the same name. The readability and reusability are enhanced by method overloading. And it’s simple to memorize.
public class Calculator {
public int addition(int x, int y) {
return x + y;
}
public int addition(int x, int y, int z) {
return x + y + z;
}
public int addition(int x, int y, int z, int w) {
return x + y + z + w;
}
}
Method Overriding
When we use inheritance one class inherits its methods and attributes to another class. In that child class, we can do the method overriding. That means we can change the method body in the child class as we want.
For example, Consider a superclass called Animal
that has a method called makeSound ()
. Cats, Dogs, Lions, and Birds are examples of Subclasses of Animal.
Each has its own animal sound (the lion roar, the cat meows, etc.)
public class Animal {
int legCount;
int bodyColor;
public void makeSound() {
System.out.println("Animal is making sound");
}
}
public class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("cat is making sound meows");
}
}
If you follow the article from the beginning now you have a better understanding of four OOP concepts and you can implement those concepts with JAVA. And I specially added some examples as well because those are going to be important for any software developer facing interviews.
See you in the next blog post. Bye Bye🍻🍸❤️❤️