OOPs stands for Object Oriented Programming.
They are an abstraction, encapsulation, inheritance, and polymorphism moreover association, composition, and aggregation. Java OOPs concepts let us create working methods and variables, then re-use all or part of them without compromising security.
Object oriented programming (OOP) is a programming language model organized around objects and data rather than actions and logic.
Types of OOPs
Let’s look into these objects oriented programming concepts one by one.
Abstraction is the concept or idea of hiding the internal details and describing things in simple terms. It does not have a concrete existence.
For example, a method that adds two integers. The method internal processing is hidden from the outer world.
Abstraction captures only those details about an object that are relevant to the current perspective, so that the programmer can focus on a few concepts at a time.
Java provides interfaces and abstract classes for describing abstract types.
interfaceis a contract or specification without any implementation. An interface does not have behavior or state.
abstract classis a class that cannot be instantiated, but has all the properties of a class including constructors. Abstract classes can have state and can be used to provide a skeletal implementation.
Encapsulation is the technique used to implement abstraction in object oriented programming. Encapsulation is used for access restricted to a class members and methods.
Encapsulation is the process of wrapping up of data (properties) and behavior (methods) of an object into a single unit; and the unit here is a Class (or interface).
It is 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.
Access modifier keywords are used for encapsulation in object oriented programming. For example, encapsulation in Java is achieved using private, protected and public keywords.
Polymorphism is the concept where an object behaves differently in different situations.
There are two types of polymorphism – compile time polymorphism (method overloading) and runtime polymorphism (method overriding).
The same method name (method overloading) or operator symbol (operator overloading) can be used in different contexts.
Java doesn’t allow operator overloading except that “+” is overloaded for class String. The “+” operator can be used in addition as well as string concatenation.
We can override an instance method of the parent class in the child class.
When you refer to, a child class object using a Parent reference (e.g. Parent p = new Child()) and invokes a method, the override child class method will be invoked. Here, the actual method called will depend on the object at runtime, not the reference type.
Overriding is not applicable for static methods or variables (static and non-static). In case of variables (static and non-static) and static methods, when you invoke a method using a reference type variable, the method or variable that belong to the reference type are invoked.
Inheritance is the object oriented programming concept where an object is based on another object.
Inheritance is the mechanism of code reuse. The object that is getting inherited is called superclass and the object that inherits the superclass is called a subclass.
We use extends keyword in Java to implement inheritance.
Association is a relationship where all objects have their own lifecycle and there is no owner. Association defines the multiplicity between objects. For example Teacher and Student objects. There is one to many relationship between a teacher and students. Similarly a student can have one to many relationship with teacher objects. However, both student and teacher objects are independent of each other.
Aggregation is a special type of association. In aggregation, objects have their own life cycle, but there is an ownership. Whenever we have “HAS-A” relationship between objects and ownership, then it’s a case of aggregation.
Composition is a special case of aggregation. Composition is a more restrictive form of aggregation. When the contained object in “HAS-A” relationship cannot exist on its own, then it’s a case of composition. For example, House has-a Room. Here room can’t exist without house.