This is an article on the main features and aspects of object-oriented programming. Mastering OOP is essential for any developer who wants to build a high quality software. So, let’s get started!
In object-oriented programming, your program will be split into several small, manageable, reusable programs. Each small program has it’s own identity, data, logic and how it’s going to communicate with the rest of the other small programs.
We can define oops programming as:
OOPs refers to languages that use objects in programming, they use objects as a primary source to implement what is to happen in the code. Objects are seen by the viewer or user, performing tasks assigned by you. 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. Object Oriented Programming (OOP) is a programming concept used in several modern programming languages, like C++, Java and Python.
Features of OOPS:
Some features of object-oriented programming in java are:
Importance is given to data than procedures
Programs are divided into objects
Data Structures are designed to characterize objects.
Methods operating on the data of an object are tied together in the data structure.
Data is hidden, and external functions cannot access it.
Objects communicate with each other through methods
New methods and data can be easily added whenever necessary
Follows the bottom-up approach in program design
Benefits of Object Oriented Programming
Improved productivity during software development
Improved software maintainability
Faster development sprints
Lower cost of development
Higher quality software
However, there are a few challenges associated with OOP, namely:
Steep learning curve
Larger program size
Slower program execution
Its not a one-size fits all solution
Object Oriented Programming Concepts:
OOP revolves around objects. You create the objects that you need and then create methods to handle those objects. These methods don’t influence the state of the objects, but it’s the other way around.
In OOP, the data is grouped together with the methods that operate upon it, which makes it easy to maintain the integrity of the data and provide a structure to the program. To understand OOP better, you have to understand its concepts, or the foundation, it operates upon.
So, what is an object oriented program exactly? It is a program that contains objects, of course, which have certain properties and have methods linked to them. You use these methods to obtain a programming result. Here are the basic object oriented concepts, with examples for each concept.
OOPS concepts are as follows:
Class
Object
Method and method passing
Pillars of OOP
1.Abstraction
2.Encapsulation
3.Inheritance
4.Polymorphism
Compile-time polymorphism
Runtime polymorphism
Class:
When you want to create an object, you create its class first. A class will define the attributes as well as the methods of an object. A class can be used to create similar objects. You can think of a class as a blueprint of an object. If you had a class called "Flowers" for example, it could have object like rose. The properties defined by the class could be its color ,fragrance , while the method could be the pollination.
We can define a class in Java. Like this, Take a look at the example below:
public class Flower {
String name;
String color;
void pollination() {
}
}
Here, we created a class called Flower, with attributes name and color and a method called pollination.
A Programming Object:
When you’re creating an object oriented program, your perspective changes so that you view the world as a collection of objects. A flower is a good example of an object. The length of its stem, the color of its petals, its fragrance and its design are the properties or the attributes of the flower object. These attributes form the data in our program. The values that these attributes take (for example, the blue color of the petals) form the state of the object. What is the task of a flower? You could say it pollenates to produce new flowers. This is a method of the flower object.
An object is both the data and the function(s), or method, which operates on the data. An object can also be defined as an instance of a class, and there can be more than one instance of an object in a program.
We should create an object in Java. Like this, using the new keyword:
Flower rose = new Flower;
This will create an object called rose for class Flower in your program.
Method:
A method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to reuse the code without retyping it, which is why they are considered time savers. In Java, every method must be part of some class, which is different from languages like C, C++, and Python.
Take a look at the example below:
4 Pillars of Object Oriented Programming:
Abstraction
Encapsulation
Inheritance
Polymorphism
1.Compile-time polymorphism
2. Runtime polymorphism
Abstraction:
Abstraction is the process of showing only necessary features of an object to the outside world and hide the other irrelevant information. If a class is declared as abstract, we cannot create an object to that class. Abstract class in Java contains the ‘abstract’ keyword. To use an abstract class, you have to inherit it from another class where you have to provide implementations for the abstract methods there itself.
Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the car speed or applying brakes will stop the car, but he does not know how on pressing the accelerator, the speed is actually increasing. He does not know about the inner mechanism of the car or the implementation of the accelerators, brakes etc. in the car. This is what abstraction is.
In Java, abstraction is achieved by interfaces and abstract classes. We can achieve 100% abstraction using interfaces.
The abstract method contains only method declaration but not implementation.
Interface in Java is a blueprint of a class or you can say it is a collection of abstract methods and static constants. In an interface, each method is public and abstract but it does not contain any constructor. Along with abstraction, interface also helps to achieve multiple inheritance in Java. So an interface basically is a group of related methods with empty bodies.
Below Images are the Examples for Abstraction :
Encapsulation:
Encapsulation is a mechanism where you bind your data and code together as a single unit. It also means to hide your data in order to make it safe from any modification.
Technically, in encapsulation, the variables or the data in a class is hidden from any other class and can be accessed only through any member function of the class in which they are declared.
In encapsulation, the data in a class is hidden from other classes, which is similar to what data-hiding does. So, the terms “encapsulation” and “data-hiding” are used interchangeably.
The best way to understand encapsulation is to think about the example of a medical capsule, where the drug is always safe inside the capsule. Similarly, through encapsulation the methods and variables of a class are well hidden and safe.
We can achieve encapsulation in Java by:
Declaring the variables of a class as private.
Providing public setter and getter methods to modify and view the variables
Below are the Examples of Encapsulation:
Inheritance:
Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features (Variables and methods) of another class. We are achieving inheritance by using extends keyword. A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.
Let us discuss some frequently used important terminologies:
Superclass: The class whose features are inherited is known as superclass (also known as base or parent class).
Subclass: The class that inherits the other class is known as subclass (also known as derived or extended or child class). The subclass can add 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 code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Inheritance is further classified into 4 types:
Single
Multilevel
Hierarchical
Hybrid
So let’s begin with the first type of inheritance i.e. Single Inheritance:
Single Inheritance
In single inheritance, one class inherits the properties of another. It enables a derived class to inherit the properties and behavior from a single parent class. This will in turn enable code reusability as well as add new features to the existing code.
Here, Class A is your parent class and Class B is your child class which inherits the properties and behavior of the parent class.
Let’s see the syntax for single inheritance:
Class A
{
------
}
Class B extends A {
------
}
Multilevel Inheritance:
When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.
If we talk about the flowchart, class B inherits the properties and behavior of class A and class C inherits the properties of class B. Here A is the parent class for B and class B is the parent class for C. So in this case class C implicitly inherits the properties and methods of class A along with Class B.
Let’s see the syntax for Multilevel inheritance:
Class A{
-----
}
Class B extends Class A{
-----
}
Class C extends Class B{
-----
}
Hierarchical Inheritance:
When a class has more than one child classes (sub classes) or in other words, more than one child classes have the same parent class, then such kind of inheritance is known as Hierarchical.
If we talk about the flowchart, Class B and C are the child classes which are inheriting from the parent Class A.
Let’s see the syntax for Multilevel inheritance:
Class A{
-----
}
Class B extends Class A{
-----
}
Class C extends Class A{
-----
}
Hybrid Inheritance
Hybrid inheritance is a combination of multiple inheritance and multilevel inheritance. Since multiple inheritance is not supported in Java as it leads to ambiguity, so this type of inheritance can only be achieved through the use of the interfaces.
If we talk about the flowchart, class A is a parent class for class B and C, whereas Class B and C are the parent class of D which is the only child class.
Let’s see the syntax for Multilevel inheritance:
Class A{
-----
}
Class B extends Class A{
-----
}
Class C extends Class A{
------
}
Class D extends Class B and C{
------
}
Below Images are the Examples for Inheritance :
Polymorphism:
Polymorphism is derived from 2 Greek words: poly and morphs. The word "poly" means many and "morphs" means forms. So polymorphism means "many forms". A subclass can define its own unique behavior and still share the same functionalities or behavior of its parent/base class. A subclass can have their own behavior and share some of its behavior from its parent class not the other way around. A parent class cannot have the behavior of its subclass.
Polymorphism can be achieved in two ways:
1.Run-time polymorphism(Over Riding or Dynamic)
2.Compile-time polymorphism(Over Loading or Static)
Run-Time Polymorphism:
A method defined in the superclass is inherited by its subclass and is used by the objects created by the subclass. However, there may be occasions when an object should respond to the same method but behave differently when that method is called, which means a method defined in the superclass is overridden. Overriding is achieved by defining a method in the subclass that has the same name, the same arguments, and the same return type as a method in the superclass. So, when the method is called, the method defined in the subclass invoked and executed instead of the one in the superclass.
Below is the Example for Run Time Polymorphism:
Compile-Time Polymorphism:
It is possible to create methods with the same name but different parameter lists and different definitions. This is called method overloading. Method overloading is required when objects are required to perform similar tasks but using different input parameters. When we call a method in an object, Java matches up the method name first and then the number and type of parameters to decide which definition to execute.
Method overloading is achieved in three ways:
Below are the Examples of Compile-Time Polymorphism:
I hope you guys are clear with all the Java OOPs Concepts with Examples that we have discussed above.
Thanks for reading My Blog.
Comments