Unit 3 Web Computing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Introduction of Object Oriented Programming

As the name suggests, Object-Oriented Programming or OOPs refers to


languages that use 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 data and the functions that
operate on them so that no other part of the code can access this data
except that function.

The word object-oriented is the combination of two words


i.e. object and oriented. The dictionary meaning of the object is an article
or entity that exists in the real world.

The meaning of oriented is interested in a particular kind of thing or entity.


In layman's terms, it is a programming pattern that rounds around an
object or entity are called object-oriented programming.

The object-oriented programming is basically a computer programming


design philosophy or methodology that organizes/ models software design
around data, or objects rather than functions and logic.

An object is referred to as a data field that has unique attributes and


behavior. Everything in OOP is grouped as self-sustainable objects.

It is the most popular programming model among developers. It is well


suited for programs that are large, complex, and actively updated or
maintained. It simplifies software development and maintenance by
providing major concepts such as abstraction, inheritance, polymorphism,
and encapsulation. These core concepts support OOP.

Points to Remember
o Everything is an object

o Developer manipulates objects that uses message passing.

o Every object is an instance of a class.

o The class contains the attribute and behavior associated with an object.
Pillars of OOPs
The major concepts that we have discussed above are known as pillars of
OOPs. There are four pillars on which OOP rests.

o Abstraction

o Encapsulation

o Inheritance

o Polymorphism

Abstraction
The concept allows us to hide the implementation from the user but shows
only essential information to the user. Using the concept developer can
easily make changes and added over time.

There are the following advantages of abstraction:

o It reduces complexity.

o It avoids delicacy.

o Eases the burden of maintenance

o Increase security and confidentially.

Encapsulation
Encapsulation is a mechanism that allows us to bind data and functions of
a class into an entity. It protects data and functions from outside
interference and misuse. Therefore, it also provides security. A class is the
best example of encapsulation.

Inheritance
The concept allows us to inherit or acquire the properties of an existing
class (parent class) into a newly created class (child class). It is known
as inheritance. It provides code reusability.

Polymorphism
The word polymorphism is derived from the two words
i.e. ploy and morphs. Poly means many and morphs means forms. It
allows us to create methods with the same name but different method
signatures. It allows the developer to create clean, sensible, readable, and
resilient code.

Class:

A class is a user-defined data type. It consists of data members and member


functions, which can be accessed and used by creating an instance of that
class. It represents the set of properties or methods that are common to all
objects of one type. A class is like a blueprint for an object.

For Example: Consider the Class of Cars. There may be many cars with
different names and brands but all of them will share some common properties
like all of them will have 4 wheels, Speed Limit, Mileage range, etc. So here,
Car is the class, and wheels, speed limits, mileage are their properties.

2. Object:

It is a basic unit of Object-Oriented Programming and represents the real-life


entities. An Object is an instance of a Class. When a class is defined, no
memory is allocated but when it is instantiated (i.e. an object is created) memory
is allocated. An object has an identity, state, and behavior. Each object contains
data and code to manipulate the data. Objects can interact without having to
know details of each other’s data or code, it is sufficient to know the type of
message accepted and type of response returned by the objects.

For example “Dog” is a real-life Object, which has some characteristics like
color, Breed, Bark, Sleep, and Eats.

Methods are functions attached to specific classes (or instances) in object-oriented


programming.
Properties are an object-oriented idiom. The term describes a one or two functions
(depending on the desired program behavior) - a 'getter' that retrieves a value and a 'setter'
that sets a value. By convention, properties usually don't have many side-effects. (And the
side-effects they do have usually have limited scope: they may validate the item being set,
notify listeners of a change, or convert an object's private data to or from a publicly-
declared type.)
Visibility

Visibility is a big part of OOP. It allows you to control where your class members can be
accessed from, for instance to prevent a certain variable to be modified from outside the
class. The default visibility is public, which means that the class members can be
accessed from anywhere. This means that declaring the visibility is optional, since it will
just fall back to public if there is no access modifier. For backwards compatibility, the old
way of declaring a class variable, where you would prefix the variable name with the
"var" keyword (this is from PHP 4 and should not be used anymore) will also default to
public visibility.

PHP is pretty simple in this area, because it comes with only 3 different access modifiers:
private, protected and public.

Private members can only be accessed from inside the class itself.

Protected members can only be accessed from inside the class it self and its child
classes.

Public members can be accessed from anywhere - outside the class, inside the class it
self and from child classes.

Introspection: Introspection is the process of analyzing a Bean to determine its capabilities.


This is an essential feature of the Java Beans API, because it allows an application builder tool
to present information about a component to a software designer. Without introspection, the
Java Beans technology could not operate.
There are two ways in which the developer of a Bean can indicate which of its properties,
events, and methods should be exposed by an application builder tool. With the first method,
simple naming conventions are used. These allow the introspection mechanisms to infer
information about a Bean. In the second way, an additional class is provided that explicitly
supplies this information. The first approach is examined here. The second method is described
later.

What is a Constructor?

A constructor is a member function of a class that initializes the object and


allocates the memory. A constructor has the same name as that of its class, thus
it can be easily identified. It is always declared and defined in the public section
of a class. A constructor does not have any return type. Therefore, it does not
return anything, but it is not even void.

class_name (arguments if any){


...
...
};

A single class may have multiple constructors that are differentiated based on the
number and type of arguments passed. There are three main types of
constructors, namely, default constructor, parameterized constructor, and copy
constructor.

What is a Destructor?

A destructor is a member function of a class that deallocates the memory


allocated to an object. A destructor is also declared and defined with the same
name as that of the class. A destructor is preceded by a tilde (~) symbol. A single
class has only a single destructor.

~ class_name (no arguments){


...
...
};

A destructor does not have any argument and is always called in the reverse
order of the constructor. Destructor are required for destroying the objects to
release the memory allocated to them.

Define inheritance in oops.

A. Inheritance is the concept in OOPs in which one class inherits the attributes
and methods of another class. The class whose properties and methods are
inherited is known as the Parent class. And the class that inherits the
properties from the parent class is the Child class.

Inheritance provides code reusability, abstraction, etc. Because of inheritance,


we can even inherit abstract classes, classes with constructors, etc. For
example – Beagle, Pitbull, etc., are different breeds of dogs, so they all have
inherited the properties of class dog.

In object-oriented programming, inheritance is the mechanism of basing


an object or class upon another object (prototype-based inheritance) or class (class-
based inheritance), retaining similar implementation.
Inheritance is contrasted with object composition, where one object contains another
object (or objects of one class contain objects of another class); see composition over
inheritance. Composition implements a has-a relationship, in contrast to the is-a
relationship of subtyping.

Method overriding, in object-oriented programming, is a language feature that allows


a subclass or child class to provide a specific implementation of a method that is already
provided by one of its superclasses or parent classes. In addition to providing data-driven
algorithm-determined parameters across virtual network interfaces,[1] it also allows for a
specific type of polymorphism (subtyping). The implementation in the subclass overrides
(replaces) the implementation in the superclass by providing a method that has same
name, same parameters or signature, and same return type as the method in the parent
class.[2] The version of a method that is executed will be determined by the object that is
used to invoke it. If an object of a parent class is used to invoke the method, then the
version in the parent class will be executed, but if an object of the subclass is used to
invoke the method, then the version in the child class will be executed.[3] This helps in
preventing problems associated with differential relay analytics which would otherwise
rely on a framework in which method overriding might be obviated.[4][5] Some languages
allow a programmer to prevent a method from being overridden.
Interface

An interface is a contract between itself and any class that implements it.
Interface can have methods, properties, or events. It contains only
declaration of its members and implementation of its members will be
given by the class who implements the interface. Interface makes it easy
to maintain the program. Following are the specified terms of interface.

• An interface can be defined by using interface keyword.

• Interface can’t have private members.

• By default all members of interface are public.

• Interface enforces to the class for implementing its members in


a class.

In object oriented programming, an interface generally defines the set of methods


(or messages) that an instance of a class that has that interface could respond to.

In object-oriented programming, an interface or protocol type is a data


type that acts as an abstraction of a class. It describes a set of method signatures,
the implementations of which may be provided by multiple classes that are
otherwise not necessarily related to each other A class which provides the
methods listed in a protocol is said to adopt the protocol, or to implement the
interface. What is encapsulation?

Encapsulation is a way to restrict the direct access to some components of an


object, so users cannot access state values for all of the variables of a particular
object. Encapsulation can be used to hide both data members and data functions
or methods associated with an instantiated class or object.
In object-oriented programming (OOP), encapsulation is the practice of bundling
related data into a structured unit, along with the methods used to work with that
data. Most OOP languages implement encapsulation primarily through classes and
the objects instantiated through those classes. A class defines a set of attributes for
the data and the methods used to carry out operations related to the data.
Key takeaways

• In object-oriented computer programming (OOP) languages, the notion of encapsulation (or OOP
Encapsulation) refers to the bundling of data, along with the methods that operate on that data, into
a single unit.
• Containers are just one example of encapsulation in coding where data and methods are bundled
together into a single package.
• A key benefits to hiding information about attributes and methods using encapsulation in
programming is that it prevents other developers from writing scripts or APIs that use your code.
• Sumo Logic complements your existing cyber security measure with cutting-edge threat detection
and security analytics powered by artificial intelligence.

Benefits of encapsulation programming

Encapsulation in programming has a few key benefits. These include:

• Hiding data: Users will have no idea how classes are being implemented or stored. All
that users will know is that values are being passed and initialized.
• More flexibility: Enables you to set variables as red or write-only. Examples include:
setName(), setAge() or to set variables as write-only then you only need to omit the get
methods like getName(), getAge() etc.
• Easy to reuse: With encapsulation, it's easy to change and adapt to new requirements.

Why encapsulation is important

Encapsulation is important because it provides a powerful way to store,


hide, and manipulate data while giving you increased control over it.
Encapsulation can used when dealing with secure data or methods because it
can restrict which functions or users have access to certain information.

Encapsulation is a key concept in object-oriented programming (OOP),


where everything revolves around objects. In OOP programming, a class is a
blueprint for creating objects. It defines the properties and behaviors that
objects of a certain class can have. Classes specify what data an object can
have (attributes) and what it can do (methods). A class bundles its attributes
and methods through encapsulation, protecting the data.
Encapsulation is also used to protect information from being
modified or having new errors introduced. When you store and lock the
information in a bundle, it is much more difficult for users to
accidentally modify the information. You achieve this by making the
data private, meaning you can only access and modify it through
methods within the same class. This principle ensures data integrity
and reduces the risk of accidental data corruption.

To implement encapsulation, you can use tools known as access


modifiers: public, private, and protected. These determine who can
access the data and methods in a class. “Public” means they're
accessible everywhere, “private” restricts access within the class, and
“protected” allows access within the same class and its subclasses. You
should carefully choose your access levels, which control how people
and algorithms access or use data and methods.

You might also like