Object-Oriented Programming
(OOP) uses a different set of programming languages than old procedural
programming languages (C, Pascal, etc.). Everything in OOP is
grouped as self-sustainable "objects".
In order to clearly
understand the object orientation, let’s take your “hand” as an example.
The “hand” is a class. Your body has two objects of type hand, named
left hand and right hand. Their main functions are controlled/ managed by a set
of electrical signals sent through your shoulders (through an interface). So
the shoulder is an interface which your body uses to interact with your hands.
The hand is a well architected class. The hand is being re-used to create the
left hand and the right hand by slightly changing the properties of it.
Object:
An object can be considered a "thing"
that can perform a set of related activities.
The set of activities that the object performs defines the object's behavior.
For example, the hand can be used to write, to hit and to grip something, etc.
Class:
A class is
simply a representation of a type of object. It is the
blueprint/ plan/ template that describe the details of an object. A class is
the blueprint from which the individual objects are created. Class is
composed of three things: a name, attributes, and operations.
In simpler words, it is a type of user defined data type.
The object oriented
programming is based on objects and classes and it gains strength through four
main object-oriented programming concepts.
Abstraction:
An abstraction is a model of a complex system that includes only
the essential details. Different viewers use different
abstractions of a particular system.
Thus, while we see a car as
a means of transportation, the automotive engineer may see it as a large mass
with a small contact area between it and the road.
Properties
of the object are exposed only to an extent to which the user of the object
need to know to use it.
Encapsulation:
The
encapsulation is the inclusion within a program object of all the resources
need for the object to function - basically, the methods and the data. In OOP the encapsulation is mainly achieved by
creating classes, the classes expose public methods and properties. The class
is a kind of a container or capsule or a cell, which encapsulate the set of
methods, attribute and properties to provide its indented functionalities to
other classes. Encapsulation is needed to hide how a class does the
functionality without letting it know to the external environment how it is
happening.
In the above example, the properties is protected by the class and
restricts them for unauthorized access.
Inheritance:
Ability of a new class to be created, from an existing class by
extending it properties, is called inheritance. In
OOPs, the keyword extends specifies
that the given class is inheriting another class by extending its properties
and methods.
Here, Car is a Super class
with properties color, horsepower and number of seats. The child class Racecar
is extending Car by adding nitro property in it and the child class Truck
extending Car by adding trailer property in it.
Polymorphism:
Polymorphisms is a generic term that means 'many shapes'. More precisely
Polymorphism means
the ability to request that the same operations be performed by a wide range of
different types of things.
For Example,
For Example,
Here, speak () is a method which is in the Animal Class. When you call
speak() method, depending on the object(in this case, it can be a cat, dog,
duck) it is pointing to, the method in that particular object will gets
executed.
For example,
abstract class Bike
{
int
limit=30;
Bike()
{
System.out.println("constructor is
invoked");
}
void
getDetails()
{
System.out.println("it
has two wheels");
}
abstract
void run();
}
class Honda extends Bike
{
void
run()
{
System.out.println("running
safely..");
}
public
static void main(String args[])
{
Bike obj = new Honda();
obj.run();
obj.getDetails();
System.out.println(obj.limit);
}
}
Interface:
An interface
is not altogether a new one for us; just it is a special flavor of
abstract class where all methods are abstract. That is, an interface
contains only abstract methods (methods with empty bodies). The Interface separates the implementation
and defines the structure, and this concept is very useful in cases where you
need the implementation to be interchangeable. Apart from that an interface is
very useful when the implementation changes frequently. Interface can inherit
other interface.
Function Overloading:
Overloading is where
there is more than one method with the same name, but the methods have
different signatures (return type or parameter lists or both).
class Calculation
{
void sum(int a,int b)
{
System.out.println(a+b);
}
void sum(int a,int b,int c)
{
System.out.println(a+b+c);
}
public static void main(String args[])
{
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}
Function Overriding:
Overriding is where the
superclass's implementation of a method is altered by the subclass's
implementation of the method, but the signatures of both methods are the same.
//
Superclass: Attribute
public
class Attribute
{
int
objectVariable;
}
void
methodAtt()
{
//Some
statements
}
//
Subclass: ColorAttribute
public
class ColorAttribute extends Attribute
{
int
addedObjectVariable;
}
void
methodAtt()
{
//Some
statements
}
X++ supports overriding,
but it does not support overloading.
Access specifier:
The access modifiers
specifies accessibility (scope) of a data member, method, constructor or class.
They are:
1) private:
The private access
modifier is accessible only within class.
2) protected:
The protected access
modifier is accessible within package and outside the package but through
inheritance only.
3) public:
The public access
modifier is accessible everywhere. It has the widest scope among all other modifiers.













Comments
Post a Comment