Oops

Equally the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the information and the functions that operate on them then that no other part of the code can access this data except that function.

Allow us do hash out pre-requisites by polishing concepts of methods declaration and passing. Starting off with the method announcement, information technology consists of six components:

  • Access Modifier: Defines access type of the method i.due east. from where it can be accessed in your application. In Coffee, there iv type of the admission specifiers.
    • public: accessible in all class in your application.
    • protected: accessible within the package in which information technology is defined and in its subclass(es)(including subclasses declared outside the packet)
    • private: accessible only within the class in which information technology is defined.
    • default (declared/defined without using whatsoever modifier): accessible within aforementioned form and package within which its class is defined.
  • The return type: The data type of the value returned by the method or void if does non return a value.
  • Method Name: the rules for field names employ to method names every bit well, simply the convention is a little different.
  • Parameter listing: Comma separated list of the input parameters are defined, preceded with their data type, within the enclosed parenthesis. If in that location are no parameters, y'all must use empty parentheses ().
  • Exception listing: The exceptions y'all expect by the method can throw, you tin specify these exception(s).
  • Method trunk: it is enclosed between braces. The code yous need to be executed to perform your intended operations.

Message Passing : Objects communicate with i another by sending and receiving information to each other. A bulletin for an object is a request for execution of a procedure and therefore volition invoke a office in the receiving object that generates the desired results. Bulletin passing involves specifying the name of the object, the name of the function and the information to exist sent.

Now with basic prerequisite to step learning 4 pillar of OOPS is as follows. Let us showtime with learning nearly the different characteristics of an Object-Oriented Programming language

OOPs Concepts are as follows:

  1. Class
  2. Object
  3. Method and method passing
  4. Pillars of OOPS
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
      • Compile-fourth dimension polymorphism
      • Run-time polymorphism

A class is a user divers blueprint or image from which objects are created. It represents the set of backdrop or methods that are common to all objects of one type. In general, class declarations can include these components, in order:

  1. Modifiers: A class tin can be public or has default admission (Refer this for details).
  2. Class name: The name should begin with a initial letter (capitalized past convention).
  3. Superclass(if any): The proper noun of the class's parent (superclass), if any, preceded by the keyword extends. A class can but extend (subclass) ane parent.
  4. Interfaces(if any): A comma-separated list of interfaces implemented by the form, if whatever, preceded by the keyword implements. A class can implement more than one interface.
  5. Torso: The class body surrounded by braces, { }.

Object is a bones unit of Object Oriented Programming and represents the real life entities. A typical Java program creates many objects, which as you know, collaborate by invoking methods. An object consists of:

  1. Land : It is represented past attributes of an object. Information technology also reflects the properties of an object.
  2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
  3. Identity : It gives a unique proper noun to an object and enables one object to interact with other objects.
  4. Method : A method is a collection of statements that perform some specific task and render consequence to the caller. A method can perform some specific chore without returning annihilation. Methods allow usa to reuse the code without retyping the code. In Java, every method must be part of some form which is dissimilar from languages like C, C++ and Python.
    Methods are fourth dimension savers and assistance us to reuse the code without retyping the code.

Let us at present discuss 4 pillars of OOPS:

Pillar 1: Brainchild

Data Brainchild is the property past virtue of which only the essential details are displayed to the user.The niggling or the non-essentials units are non displayed to the user. Ex: A automobile is viewed as a auto rather than its individual components.
Data Abstraction may likewise be defined as the process of identifying just the required characteristics of an object ignoring the irrelevant details. The properties and behaviours of an object differentiate it from other objects of like blazon and likewise assistance in classifying/grouping the objects.
Consider a real-life case of a homo driving a machine. The man only knows that pressing the accelerators will increase the speed of car or applying brakes will terminate the car but he does not know about how on pressing the accelerator the speed is really increasing, he does not know nigh the inner mechanism of the car or the implementation of accelerator, brakes etc in the car. This is what abstraction is.
In java, brainchild is achieved by interfaces and abstract classes. We can accomplish 100% brainchild using interfaces.

Pillar 2: Encapsulation

Information technology is defined every bit the wrapping up of data under a single unit. It is the machinery that binds together code and the information information technology manipulates. Another way to think nearly encapsulation is, it is a protective shield that prevents the data from being accessed by the code outside this shield.

  • Technically in encapsulation, the variables or data of a course is hidden from whatsoever other class and can be accessed only through any member role of own class in which they are declared.
  • As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-hiding.
  • Encapsulation tin be achieved by Declaring all the variables in the class as private and writing public methods in the class to fix and get the values of variables.

Colonnade iii: Inheritence

Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is permit to inherit the features(fields and methods) of some other form.

Allow united states discuss some of frequent used important terminologies:

  • Super Class: The class whose features are inherited is known as superclass(or a base of operations class or a parent class).
  • Sub Course: The class that inherits the other class is known every bit subclass(or a derived class, extended class, or kid class). The subclass tin can add together its own fields and methods in addition to the superclass fields and methods.
  • Reusability: Inheritance supports the concept of "reusability", i.e. when we want to create a new class and there is already a class that includes some of the lawmaking that we want, we can derive our new class from the existing form. By doing this, we are reusing the fields and methods of the existing grade.

Pillar 4: Polymorphism

It refers to the ability of OOPs programming languages to differentiate betwixt entities with the aforementioned name efficiently. This is done by Java with the aid of the signature and declaration of these entities.

Note: Polymorphism in Java are mainly of 2 types:

  1. Overloading
  2. Overriding

Example

Java

public class Sum {

public int sum( int x, int y)

{

return (ten + y);

}

public int sum( int x, int y, int z)

{

return (x + y + z);

}

public double sum( double x, double y)

{

return (x + y);

}

public static void chief(String args[])

{

Sum south = new Sum();

System.out.println(southward.sum( 10 , xx ));

Arrangement.out.println(due south.sum( x , xx , thirty ));

System.out.println(s.sum( 10.v , 20.v ));

}

}


scottmanne1937.blogspot.com

Source: https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/

0 Response to "Oops"

Отправить комментарий

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel