7 Most Important Concepts of Object Oriented Programming (OOP)
Object-Oriented Programming is a programming style that has concepts of classes, objects, inheritance, polymorphism, etc.
Basically, it’s an operation of taking input,
processing the data, and returning the output. OOP completely depends on
objects, how they relate and how they communicate with each other in order to
manipulate data.
An object can be described as an instance of
a class that can contain both variables (attributes) and functions (methods).
So, the most important step is to identify the objects and to find the
relationship between them.
In Object-Oriented Programming, data can be
accessed by the functions that are associated with those objects. This approach
depends on
- Identifying objects and assigning responsibilities to these objects.
- Objects communicate with each other by sending messages.
Messages are received by the methods of an
object that in return process the data.
Java, C++, Python, Ruby, Visual basic. Net, etc
are languages that support the concept of object-oriented programming.
The main concepts of OOP are
1. Class:
A class is a template that contains attributes
and methods. Basically, a class is a set of similar entities. For Example, a
class Rectangle can have attributes length, width, height, etc, and can have
methods toCalculateArea(), etc.
2. Object:
An object is an instance of a class. A class can
have more than one object. For Example, by creating the instance of the above
class rectangle, we can access the attributes of the rectangle class such as
length, width, and height. Putting these values into toCalculateArea() method,
we can get the area of the rectangle. An object of the above class can be
created as
Rectangle rectangle = new Rectangle();
3. Inheritance:
Inheritance is basically a parent-child
relationship between the two classes. A parent and child class hold an IS-A
relationship. A parent class holds the general properties which are inherited
by the child classes whereas a subclass also has its own additional specific
properties as well. When we create an object of child class it gains all
properties of the parent class.
Examples:
- A dog is an animal.
- A rectangle is a shape.
In the above example, an animal is a base class
that has the general properties while the dog is the subclass which has his own
additional properties such as barking, etc. The keyword extends is used to
inherit the properties from the parent class. Just like
class Dog extends Animal{
//body
}
4. Polymorphism:
Polymorphism is the ability to appear in many
forms. Polymorphism gives us the benefit to process objects differently
depending on their data type and class.
There are two types of Polymorphism
- Static polymorphism
- dynamic polymorphism
Static polymorphism:
It is known as compile-time polymorphism. Static
polymorphism is basically method overloading. It gives permission to have more
than one method having the same name but with different parameters or a
different number of parameters. The compiler knows at the compilation time
which method to run.
Dynamic polymorphism:
It is known as runtime polymorphism. It is
basically known as Method Overriding. The compiler decides at the runtime which
method to run. All subclasses of the base class have the method with the same
name and parameters. JVM decides the method of which class to run at the
runtime.
5. Abstraction:
Abstraction means mention important features
without including background details. For example, while driving a bus you do
not have concerned with its internal functionality, you only concerned with the
gayer, accelerator, wheels, steering, etc. This type of data is put into the
abstract class and the concrete class is build by following the abstract class.
6. Encapsulation:
Encapsulation means wrapping the data.
Encapsulated data of a certain class will be hidden from other classes.
To achieve encapsulation in java, declare the
variable of the class as a private.
Provide the public setter and getter method to
modify or view the values.
Example:
- private int age;
- private String name;
We can use the setter-getter method to access
age and name.
7. Interface:
An interface is like a contract that is agreed
by a class in which a class implements the methods of an interface. An
interface is a sort of abstract class in which all the methods are abstract.
None of the methods defined in the interface has the body. The syntax of
writing an interface is very much similar to that of the class. You write it in
a similar way except you specify it as the interface, not as a class. An
interface contains method definitions, not method bodies just like an abstract
class. An abstract class can also have some non-abstract method body
definitions but an interface contains only method definitions.
A class does not extend the interface as it
extends its superclass instead it implements the interface. A class can
implement as many interfaces as it wants. If a class does not provide the body
to every method of the interfaces it implements then it becomes an abstract
class. You cannot create an object of that class.
Note: If you want to become a good programmer then it is necessary for you to have a good command of OOP. Object-oriented programming has many applications such as client-server systems, object-oriented databases, Real-Time system design, etc. So, work hard to get good skills in OOP and do not give up.
No comments