Welcome to "Coding Center"!! Coding Center is the place where you will find something new to learn about Java and Python. If you are looking to brush up on your skills before going for an interview, this article is for you...

In this article, we are going to talk about the most basic and most commonly asked question in the interview. OOPs are the principles on which Java stands as they are the very core of how Java started in this world as a programming language. Now, let's see what these OOPs are.

What are OOPs

OOPs stands for Object-Oriented Principles and these principles tell that everything in Java is going to revolve around Objects and hence object-oriented. There are four principles here-

1. Abstraction

Abstraction means that we only have to show users what they need to see and what is essential, the rest of the data need not be shown to the user. Now, this is all theoretical but what actually is Abstraction in Java. Let's take a real-world scenario to understand this better.

When Android came to this world, our parents were still using their old-fashioned phones. But, when they moved to an Android phone, they didn't need to know how much RAM it has or how much internal disk space, or how good its camera is. They just needed to know how to call someone and that's it. This is what Abstraction is, cutting out all unnecessary information to the user and giving only necessary information.

Now, the big question is how do we implement Abstraction in Java. There are two ways to implement Abstraction in Java.
  1.  Interfaces
  2.  Abstract Classes
This is again a big interview question, what's the difference between Interface and Abstract class, and when do we use which and which one is better and why.

2. Encapsulation

Encapsulation, in a literal sense, means to capsulate or bundle a bunch of things together. Encapsulation also means data-hiding which is why we create a bundle so that we can prevent this data from being accessed by the code outside this bundle. Let's take a real-world scenario to understand this better.

You might have seen or ate a capsule for medication purposes and if that capsule is transparent you can see that there are bunch of medicines clubbed together to form a capsule but if that capsule is not transparent then you can't tell much about it. That's how encapsulation works in Java also.

We can implement Encapsulation by using access modifiers. For example- If we declare all the variables in a classs as private using private access modifier and then use public getter setter methods to get the information.

3. Inheritance

Inheritance, in a literal sense, means to inherit something and this is also how we inherit data/methods between classes. Inheritance is a way in Java through which a class(child class) can inherit the features of another class(parent class). Now, this is all theoretical but what actually is Inheritance in Java. Let's take a real-world scenario to understand this better.

Let's say I have a car and a house of my own and a daughter is born to my family. Now, as she is the only child, she is going to inherit my car and my house. Here, we have a parent-child relationship and when that is established then she is entitled to what I have or what I can give. This is how exactly Inheritance in Java works.

We can implement Inheritance in Java using extends keyword.

4. Polymorphism

Polymorphism is made up using two Greek words - poly(many) and morphs(forms). In java, it means same name but different functionalities. If we have multiple methods of same name and they are all doing different different things then that is Polymorphism.

Let's take a real world example here, I'm a Developer, a son, an uncle and a brother. So, here though I'm that very same person but I'm going to be different in different situations.

There are 2 types of Polymorphism in Java-
  1.  Overloading aka Static Binding
  2.  Overriding aka Dynamic Binding

No comments:

Post a Comment

| Designed by Colorlib