Oops
As the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-globe entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the lawmaking can access this information except that function.
Let united states exercise discuss pre-requisites by polishing concepts of methods announcement and passing. Starting off with the method declaration, it consists of six components:
- Access Modifier: Defines access type of the method i.due east. from where it tin can exist accessed in your application. In Coffee, in that location 4 type of the access specifiers.
- public: accessible in all class in your awarding.
- protected: attainable within the package in which it is divers and in its subclass(es)(including subclasses declared outside the package)
- individual: accessible only within the class in which it is defined.
- default (declared/divers without using any modifier): accessible within same grade and package within which its grade is defined.
- The render blazon: The data type of the value returned by the method or void if does not return a value.
- Method Proper noun: the rules for field names utilize to method names as well, only the convention is a picayune different.
- Parameter list: Comma separated list of the input parameters are divers, preceded with their information type, inside the enclosed parenthesis. If there are no parameters, yous must use empty parentheses ().
- Exception list: The exceptions you expect by the method tin throw, you can specify these exception(s).
- Method body: information technology is enclosed between braces. The code you need to be executed to perform your intended operations.
Bulletin Passing : Objects communicate with one another past sending and receiving information to each other. A message for an object is a request for execution of a procedure and therefore volition invoke a function in the receiving object that generates the desired results. Bulletin passing involves specifying the proper name of the object, the proper noun of the function and the information to be sent.
Now with bones prerequisite to footstep learning 4 pillar of OOPS is equally follows. Allow the states start with learning about the different characteristics of an Object-Oriented Programming language
OOPs Concepts are as follows:
- Class
- Object
- Method and method passing
- Pillars of OOPS
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- Compile-fourth dimension polymorphism
- Run-time polymorphism
A class is a user defined blueprint or prototype from which objects are created. It represents the set up of properties or methods that are common to all objects of one type. In general, class declarations tin can include these components, in order:
- Modifiers: A class can be public or has default admission (Refer this for details).
- Class name: The name should begin with a initial letter (capitalized past convention).
- Superclass(if whatsoever): The name of the class's parent (superclass), if whatever, preceded by the keyword extends. A class can only extend (subclass) one parent.
- Interfaces(if any): A comma-separated list of interfaces implemented by the grade, if any, preceded by the keyword implements. A class can implement more than 1 interface.
- Torso: The grade trunk surrounded by braces, { }.
Object is a basic unit of measurement of Object Oriented Programming and represents the real life entities. A typical Coffee program creates many objects, which every bit you lot know, interact past invoking methods. An object consists of:
- State : It is represented by attributes of an object. Information technology too reflects the backdrop of an object.
- Behavior : Information technology is represented by methods of an object. It also reflects the response of an object with other objects.
- Identity : Information technology gives a unique name to an object and enables ane object to interact with other objects.
- Method : A method is a collection of statements that perform some specific task and return result to the caller. A method tin perform some specific task without returning anything. Methods allow the states to reuse the code without retyping the code. In Coffee, every method must be function of some class which is different from languages like C, C++ and Python.
Methods are time savers and help u.s.a. to reuse the code without retyping the lawmaking.
Let us now discuss four pillars of OOPS:
Pillar ane: Abstraction
Data Abstraction is the holding by virtue of which only the essential details are displayed to the user.The picayune or the non-essentials units are not displayed to the user. Ex: A car is viewed equally a car rather than its individual components.
Information Brainchild may also be defined as the process of identifying only the required characteristics of an object ignoring the irrelevant details. The properties and behaviours of an object differentiate it from other objects of similar type and likewise aid in classifying/grouping the objects.
Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car just he does not know most how on pressing the accelerator the speed is actually increasing, he does non know about the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what brainchild is.
In java, brainchild is accomplished by interfaces and abstract classes. We can achieve 100% abstraction using interfaces.
Pillar 2: Encapsulation
It is defined as the wrapping upward of data under a single unit. Information technology is the mechanism that binds together lawmaking and the data it manipulates. Another fashion to remember near encapsulation is, it is a protective shield that prevents the data from existence accessed by the lawmaking outside this shield.
- Technically in encapsulation, the variables or information of a class is hidden from any other class and tin can be accessed merely through any member office of own form in which they are declared.
- As in encapsulation, the data in a class is hidden from other classes, so information technology is also known as data-hiding.
- Encapsulation can be achieved past Declaring all the variables in the class as private and writing public methods in the class to set and go the values of variables.
Pillar iii: Inheritence
Inheritance is an important pillar of OOP(Object Oriented Programming). It is the machinery in java by which i class is allow to inherit the features(fields and methods) of another class.
Let united states hash out some of frequent used important terminologies:
- Super Class: The class whose features are inherited is known every bit superclass(or a base class or a parent class).
- Sub Class: The class that inherits the other form is known as subclass(or a derived class, extended class, or child form). The subclass can add its ain fields and methods in add-on to the superclass fields and methods.
- Reusability: Inheritance supports the concept of "reusability", i.east. when we want to create a new class and there is already a course that includes some of the lawmaking that nosotros desire, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Colonnade 4: Polymorphism
It refers to the ability of OOPs programming languages to differentiate betwixt entities with the same proper noun efficiently. This is done by Java with the help of the signature and announcement of these entities.
Note: Polymorphism in Java are mainly of ii types:
- Overloading
- Overriding
Example
Coffee
public
class
Sum {
public
int
sum(
int
x,
int
y)
{
return
(x + y);
}
public
int
sum(
int
x,
int
y,
int
z)
{
return
(10 + y + z);
}
public
double
sum(
double
x,
double
y)
{
render
(x + y);
}
public
static
void
master(String args[])
{
Sum s =
new
Sum();
Organisation.out.println(s.sum(
ten
,
20
));
Arrangement.out.println(s.sum(
ten
,
20
,
30
));
System.out.println(s.sum(
10.v
,
xx.v
));
}
}
Source: https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/
Belum ada Komentar untuk "Oops"
Posting Komentar