Object Oriented Programming (OOPs) Concept in Java.

Introduction

Object-Oriented Programming (OOPs) is the concept of objects based programming, that contains data(fields) and its behavior (method), in a single location(object).

Using object-oriented programming is to increase the flexibility and maintainability of programs.

Truly object-oriented programming language is represented everything as an object,

Main aims of Object-oriented programming to implement real-world entities like inheritance, hiding(Abstraction and Encapsulation), polymorphism, etc in programming.

 

 

List of OOP Concepts in Java.

  • Object
  • Class
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation

 

There are four main OOP concepts in Java.

  • Polymorphism
  • Inheritance
  • Abstraction
  • Encapsulation

 

Let’s get started with the first Object and Class OOP concept.

What is the Class?

We can simply say a Class is a user-defined data type, which has a group of objects and common properties.
or we can say it is a blueprint or template from which objects are created. It is a logical entity.

 

What is Object?

We can simply say, an object is an instance of a class. It is a real-world entity like a pen, dog, table, etc.  An object in Java is the physical as well as a logical entity.

For more details of the Object and method click here.

 

Now let’s get discuss four main OOP concepts.

What is Polymorphism?

Polymorphism is one of the OOPs features, it allows us to perform a single action in different ways.

Polymorphism word comes from 2 Greek words. Poly and morphs, Poly means many and morphs means forms. That’s why can say, polymorphism means many forms.

 

Simple polymorphism example.

public class Employee{
public void test(){
System.out.println("Animal is making a sound"); 
}
}

public class Office extends Employee{
@Override
public void going(){
System.out.println("Going to Office");
}
}

public class Market extends Employee {
@Override
public void going() {
System.out.println("Gogin to Market");
}
}

In the above example with the same method name, we are performing different activities.  As you can see that although we had the same action for all subclasses going(), there were different ways to do the same action.

 

Types of polymorphism in Java.

There are two types of polymorphism in Java.

  • Compile-time polymorphism
  • Runtime polymorphism

For details of polymorphism and its type click here.

 

What is Inheritance?

Inheritance is a mechanism, where one class acquires all the properties and behaviors (methods and fields) of another class is called Inheritance.

We use extends Keyword to inherit the properties of one class to another class.

Using the extends keyword, the child class will be able to inherit all the properties of the parent class but except for the private properties of the parent class.

 

The syntax of Java Inheritance Example:

class Parent {
.....
.....
}

class Child extends Parent {
.....
.....
}

 

In the above example Parent class is called a superclass, child class is called a subclass, where child class extends the Parent class and gets all properties and behaviors of the parent class.

 

Why use inheritance in java?

  • For Method Overriding
  • For Code Re-usability

For more details of Inheritance click here.

 

What is Abstraction?

Abstraction is a process of hiding the implementation details, and showing only functionality to the user.

The main purpose of abstraction is to hide the unnecessary details from the users.

In another way, we can say abstraction is shown only essential things to the user and hides the internal details from the user.

Let’s try to understand with an example, we are withdrawing money from ATM, so we are entering the ATM card and PIN and amount and you get the money, but we don’t know what is internally going on in this process.

So in this process. We are only able to see essential things like entering the password, enter the amount. But we are not able to see the internal process of that transaction.

 

How to achieve Abstraction in java?

There are two ways to achieve abstraction in java.

  • Abstract class (0 to 100%)
  • Interface (100%)

For more details of abstraction click here.

 

What is Encapsulation?

It is wrapping code(method) and data (variables) together into one single unit.

A real-life example of Encapsulation is a Capsule. In that capsule, several medicines are mixed, and it is wrapped into one outer layer.

 

How to create a fully Encapsulation class in java?

By making all the data members of the class as private. We are creating public setter and getter methods, in the class to set and get the data.

The Java Entity class (Bean class), is fully encapsulated in java.

 

Advantage of Encapsulation in Java.

There are many advantages of Encapsulation in java.

  • Make read-only and write-only classes using encapsulation.
  • Achieve data hiding.
  • The encapsulated classes are better for unit testing.
  • Easy and fast.
  • Control over data.

For more details of Encapsulation click here.

 

Hope this was helpful for you. If you have any questions, please feel free to leave a comment. Thank you for reading.

 

 

14 Replies to “Object Oriented Programming (OOPs) Concept in Java.”

  1. A lot of thanks for each of your hard work on this web page. Kim really likes going through investigation and it’s really easy to understand why. Many of us know all of the dynamic method you make powerful strategies on this web blog and as well as improve participation from people on that issue so our girl is in fact learning a lot of things. Have fun with the remaining portion of the new year. You are conducting a really good job.

  2. I’m just commenting to let you be aware of of the exceptional discovery my cousin’s child enjoyed going through your blog. She discovered a wide variety of details, most notably how it is like to have a very effective coaching heart to get many more effortlessly fully understand certain extremely tough matters. You actually did more than my desires. I appreciate you for displaying such great, healthy, educational and also cool guidance on that topic to Evelyn.

  3. My spouse and i got quite glad that John could deal with his investigations through the ideas he grabbed out of your web page. It’s not at all simplistic to just continually be giving out methods that many other people have been selling. And we also fully understand we have got the website owner to be grateful to for that. The main explanations you have made, the simple site menu, the friendships you can help foster – it’s got many great, and it’s really helping our son and the family imagine that the matter is entertaining, and that is pretty indispensable. Thank you for everything!

  4. A lot of thanks for all your valuable efforts on this site. Kate loves getting into investigation and it’s really easy to understand why. I know all concerning the dynamic medium you give insightful solutions on this web blog and as well as foster participation from others on that content plus our daughter is certainly discovering a lot. Have fun with the remaining portion of the new year. You are conducting a powerful job.

  5. My husband and i ended up being comfortable Albert could carry out his basic research by way of the ideas he acquired from your blog. It’s not at all simplistic just to always be freely giving guidance many others could have been making money from. And now we already know we’ve got the blog owner to appreciate because of that. All of the explanations you’ve made, the easy blog menu, the friendships you aid to create – it’s all astonishing, and it’s aiding our son and our family believe that that article is awesome, and that’s exceedingly essential. Many thanks for all!

  6. I simply wished to thank you very much yet again. I do not know what I would have undertaken in the absence of the actual creative ideas discussed by you concerning such concern. It absolutely was a real depressing concern for me personally, but considering a new professional fashion you handled it forced me to cry for delight. I’m grateful for the assistance and as well , expect you comprehend what a powerful job that you are carrying out educating men and women by way of a web site. I am certain you’ve never come across all of us.

  7. Thanks for the useful information on credit repair on your web-site. The thing I would offer as advice to people will be to give up a mentality they will buy at this moment and pay back later. As being a society most people tend to repeat this for many factors. This includes family vacations, furniture, plus items we really want to have. However, you must separate a person’s wants from the needs. When you’re working to fix your credit score you really have to make some trade-offs. For example you possibly can shop online to economize or you can click on second hand suppliers instead of highly-priced department stores to get clothing.

  8. Thanks for the useful information on credit repair on your web-site. The thing I would offer as advice to people will be to give up a mentality they will buy at this moment and pay back later. As being a society most people tend to repeat this for many factors. This includes family vacations, furniture, plus items we really want to have. However, you must separate a person’s wants from the needs. When you’re working to fix your credit score you really have to make some trade-offs. For example you possibly can shop online to economize or you can click on second hand suppliers instead of highly-priced department stores to get clothing.

Leave a Reply

Your email address will not be published.