What are the principle concepts of OOPS?
The principle concepts of OOPS are,
- Abstraction means using simple things to represent complexity.
- In Java, abstraction is defined as simple things like objects, classes, and variables represent more complex underlying code and data.
- It is important because it avoids repeating the same work multiple times.
- This is the practice of keeping fields within a class private, then providing access to them via public methods.
- It’s a protective barrier that keeps the data and code safe within the class itself.
- This way, we can re-use objects like code components or variables without allowing open access to the data system-wide.
- This is a special feature of Object Oriented Programming in Java.
- It lets programmers create new classes that share some of the attributes of existing classes.
- This lets us build on previous work without reinventing the wheel.
- This Java OOP concept lets programmers use the same word to mean different things in different contexts.
- One form of polymorphism in Java is method overloading
- It is for the different meanings are implied by the code itself.
- The other form is method overriding.
- It is for the different meanings are implied by the values of the supplied variables.
Java OOPs Concepts
In this page, we will learn about the basics of OOPs. Object-Oriented Programming is a paradigm that provides many concepts such as inheritance, data binding, polymorphism, etc.
OOPs (Object-Oriented Programming System)
Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts:
Any entity that has state and behavior is known as an object. For example a chair, pen, table, keyboard, bike, etc. It can be physical or logical.
An Object can be defined as an instance of a class. An object contains an address and takes up some space in memory. Objects can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.
Collection of objects is called class. It is a logical entity.
A class can also be defined as a blueprint from which you can create an individual object. Class doesn’t consume any space.
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism.
If one task is performed by different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism.
Another example can be to speak something; for example, a cat speaks meow, dog barks woof, etc.
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don’t know the internal processing.
In Java, we use abstract class and interface to achieve abstraction.
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example capsule, it is wrapped with different medicines.
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
OOPS Concepts in Java with Examples
Object Oriented Programming is a programming concept that works on the principle that objects are the most important part of your program. It allows users create the objects that they want and then create methods to handle those objects. Manipulating these objects to get results is the goal of Object Oriented Programming.
Object Oriented Programming popularly known as OOP, is used in a modern programming language like Java
Core OOPS concepts are
The class is a group of similar entities. It is only an logical component and not the physical entity. For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc. Its properties(data) can be price or speed of these cars. While the methods may be performed with these cars are driving, reverse, braking etc.
An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An Object contains both the data and the function, which operates on the data. For example – chair, bike, marker, pen, table, car, etc.
Inheritance is an OOPS concept in which one object acquires the properties and behaviors of the parent object. It’s creating a parent-child relationship between two classes. It offers robust and natural mechanism for organizing and structure of any software.
Polymorphism refers to the ability of a variable, object or function to take on multiple forms. For example, in English, the verb “run” has a different meaning if you use it with “a laptop,” “a foot race, and ”business.&rdquo Here, we understand the meaning of “run” based on the other words used along with it.The same also applied to Polymorphism.
An abstraction is an act of representing essential features without including background details. It is a technique of creating a new data type that is suited for a specific application. For example, while driving a car, you do not have to be concerned with its internal working. Here you just need to concern about parts like steering wheel, Gears, accelerator, etc.
Encapsulation is an OOP technique of wrapping the data and code. In this OOPS concept, the variables of a class are always hidden from other classes. It can only be accessed using the methods of their current class. For example – in school, a student cannot exist without a class.
Association is a relationship between two objects. It defines the diversity between objects. In this OOP concept, all object have their separate lifecycle, and there is no owner. For example, many students can associate with one teacher while one student can also associate with multiple teachers.
In this technique, all objects have their separate lifecycle. However, there is ownership such that child object can’t belong to another parent object. For example consider class/objects department and teacher. Here, a single teacher can’t belong to multiple departments, but even if we delete the department, the teacher object will never be destroyed.