OOPs means Object-Oriented Programming which refers to languages that use objects in programming. Its aim is to implement real-world entities like inheritance, polymorphism, abstraction, etc in programming. The main popular object-oriented programming languages are Java, PHP, Python, C#, C++, etc. The function of OOPs is to bind the data and the functions together that operate on them so that no other part of the code can access this data except that function.
Concepts In OOPs
A class is a blueprint or template used to create objects and define the data types and methods. The class represents a set of properties or methods that are common to all objects of one type. The functions written in class are called methods and the fields written in class are called variables.
The class declaration can include the following components
Modifiers - A class can be public, private, protected, or have a default access.
Class name - The class name should begin with the initial letter capitalized by convention means.
Super class - The name of the class parent, if any present is preceded by the keywords extends. One class can only extend one parent.
Interface - A class can implement more than one interface. If any Interface is present it should be preceded by the keyword implements.
Body - The class body is written inside by flower braces
The object is an entity that has a state and behavior. Examples of objects are pens, tables, cycles, cars, etc. Objects can be physical or logical. An object can be defined as an instance of a class. They can communicate without knowing the details of the other code or data. A typical Java program creates as many objects, which can interact by invoking methods.
Object consists of the following
State - State is represented by attributes of an object
Behavior - It is represented by methods of objects. It also reflects the response of one object to the other objects.
Identity - it is a unique name provided to an object that enables it to interact with other objects
Inheritance is the mechanism in a java programming language by which one class is allowed to inherit the features of another class. When one object acquires all the properties and behaviors of its parent object then it is known as inheritance. It is used to achieve a runtime environment and it also provides code reusability. It is an important concept in object-oriented programming.
Terms to Know
- Superclass - A class whose features are inherited is known as a superclass or also called a base class or parent class.
- Subclass - The class that inherits the other class is known as the subclass and is also called a child class or extended class. A subclass can add its own fields (variables) and methods in addition to superclass fields and methods.
- Reusability - Inheritance supports the reusability concept, i.e when we want to create a new class and there is already a class that includes some of the code that we require, we can derive or achieve our new class from the existing class. By doing so we can reuse the field and methods of an existing class.
Data abstraction is the property of object-oriented programing by virtue of which only essential details are displayed to the user and hide the internal data. The important or the trivial units of data are not displayed to the user. For example, in a phone call, we don’t know the internal processing. The properties and behaviors of an object differentiate it from other objects of the same type and also help in classifying or grouping the object. Consider a real-life example of a man driving a car. the man only knows that pressing the accelerators will increase the speed of the car and applying breaks will stop the car but he doesn’t know about the internal mechanism taking place inside the car. This is called an abstraction. In Java abstraction is achieved by abstract classes and interfaces. We can achieve 100% abstraction by using interfaces.
Polymorphism refers to the ability of an object-oriented programming language to differentiate between entities with the same name effectively. Polymorphism can be done in java with the help of the signature and declaration of entities. If one task is performed in many different ways it can be called as polymorphism. For example- to convince the customer differently, to draw something, to explain one thing in many different ways, etc. In java, we use method overloading and method overriding to achieve polymorphism.
Encapsulation can be defined as the wrapping up of data in a single unit. It is the mechanism that binds the code and data together which it manipulates. Another way to think of encapsulation is that it is a protective covering or shield that prevents the data from being accessed by the code outside this shield. A java class is an example of encapsulation. Java bean is a fully encapsulated class because all the data members are private here. In encapsulation, the data in a class is hidden from other classes, which is similar to what data-hiding does. So, the terms encapsulation and data-hiding terms are used interchangeably. Encapsulation can be achieved by declaring all the variables in a class as private and writing public methods in the class to set or get the values of the variables.