Introduction OOP stands for Object-Oriented Programming.
Procedural programming is based on writing methods that perform operations on the data, while object-oriented programming is about creating objects that contain both data and methods. Below are the several advantages of OOP over procedural programming:
OOP is faster and easier to execute.
OOP provides a clear structure for the programs.
OOP helps to keep the Java code DRY "Don't Repeat Yourself", and promotes reusability of code.
Building blocks of OOP OOP can be divided into 6 different sections:
Class
Object
Abstraction
Encapsulation
Inheritance
Polymorphism
In this post we will see about the various blocks in OOP.
Object Any real world entity can be treated as an object. Anything which we can see and use. This object has a property and can perform certain tasks. For example: A dog is an object and has properties like breed, color, etc. It can do certain tasks like running, wagging its tail, etc.
Class Class is the blueprint that the object follows. A class can have many objects. Class defines the prototype that every object should follow. An object can be thought as an instance of a class. A class can contain any of the following variable types.
Local variables − Variables defined inside methods, constructors or blocks and have their existence within the block/method.
Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
Class variables − Class variables are variables declared within a class, outside any method, with the static keyword.
NOTE: Classes can't communicate with each other. In order to communicate(accessing the methods of other class), object has to be created.
Abstraction Abstraction is a concept in JAVA for showing only the essential parts to the end user and hiding the underlying implementation. For example, when we download a software, we get the .exe file and the internal implementation of the application is hidden.
Encapsulation Encapsulation is a mechanism of wrapping the data (variables) and methods together as a single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding. To achieve encapsulation in Java, we need to declare the variables of a class as private and provide public setter and getter methods to modify and view the variables values.
Inheritance Inheritance is the mechanism by which one class acquires the properties of another class. The class whose property is transferred to another class is known as the Parent Class/ Super Class/ Base Class and the class that acquires the property is known as the Child Class/ Sub Class/ Derived Class. Inheritance are of the following types:
Single-level
Multi-level
Hierarchical
Polymorphism Polymorphism is the concept of executing a method in different ways. Suppose there is a method to draw a polygon, we can use it to draw a square, rectangle, triangle, etc. This is polymorphism, same method different implementation. In JAVA, polymorphism can be achieved in 2 ways:
Method overloading -> Compile-time -> Static Polymorphism
Method overriding -> Run-time -> Dynamic Polymorphism
Summary In this post, we have discussed about the overview of OOP concepts in JAVA. In the later posts, we will discuss these concepts in depth.
تعليقات