In this post, we are going to learn about one of the most important topics of Java, which is OOPS concepts in Java. If you have not checked our previous posts about complete core java tutorial, then you can check that by click on the mentioned link.
What is object-oriented programming?
OOPS stands for Object-Oriented Programming System. Its a vital topic because without any idea about OOPS concepts, you can not design systems in the object-oriented programming model. OOPS, it is mainly based on “objects” which contains both data members and methods. The primary purpose of OOPS is to increase the maintainability, readability, reusability, and flexibility of Java programs.
Because of Java Object, it makes the language more understandable because it brings data members and methods in a single location so that it is easy to understand how the program works.
OOPS Concepts
Let’s look into the object-oriented programming systems one by one so that you will not face any difficulties in understanding the OOPS concepts.
- Class
- Object
- Constructor
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
What is a Class?
Class is nothing but a prototype or blueprint from which we can create objects. A Class contains properties or methods which are common for all objects of one type. A Class includes also have different components like:
- Modifiers
- Class Name
- Super-class
- Interfaces
- Body
In Classes, for initialising the new objects, we are using constructors. In Real-time application, we are using different classes like nested classes and anonymous classes. Below is a syntax of how a class looks like
class Class_Name { member variables; methods(); }
What is an Object?
Its a base unit of Java programming language which represents a real-time entity. Objects hold both data members and methods in a single location. Objects consist of:
- State: It represents the attributes or properties of an object.
- Behaviour: It represents the methods of a class
Example:
class Computer { String Maker; int Model; String Color; void turnOn { //statement(s) } void turnoff { //statement(s) } }
If you explain by taking a simple example, then that’s better
What is Constructor?
Constructors are looking like the methods, but constructors are not methods. There are some pre-defining rules for constructors, like the name of constructors should be same as the class name, and it does not return any value. The constructor is mainly used to initialise the objects. There are different types of constructors present, like default constructor, no-args constructor, and parameterised constructor. For more details about Java constructor use the link.
Abstraction
The main aim of abstraction is to hide the complexity from the user and show them whats the relevant information for the user. Let’s take a single example of Phone, where you dial the number and talk to someone, but we are not worried about the inner implantation. Therefore abstraction helps you to reduce the complexity.
Encapsulation
Encapsulation is nothing but a mechanism where you bind the data member (Variables) and member function (Methods) together in a single unit. The main purpose of this is to hide your data to make it safe from any other modification. In simple words, when we are creating a class, we are doing encapsulation.
Inheritance
The process of acquiring the data member and member function of another class called inheritance. Because of inheritance, we can achieve the re-usability of the code of an existing class. If the subclass has some unique feature which needs to be implemented then in the child class, we have defined the new features only and rest of the things you can inherit from the parent class.
Polymorphism
Polymorphism means “many forms”, It’s another feature of Object-oriented programming feature by which we can do a single action in different ways. There are two various forms of Polymorphism is there
Ref: article
Leave a Reply