Tuesday, March 7, 2017

Java OOPs Concepts

Object and Classes
Since Java is an object-oriented language, complete java language is built on classes and object. Java is also known as a strong Object oriented programming language(oops).
OOPS is a programming approach which provides solution to problems with the help of algorithms based on real world. It uses real-world approach to solving a problem. So object oriented technique offers better and easy way to write program then procedural programming model such as C, ALGOL, PASCAL.
Object: Any entity that has state and behavior is known as an object. For example chair, pen, table, keyboard, bike etc. It can be physical and logical.
Class :Collection of objects is called class. It is a logical entity.

OOPs (Object Oriented Programming System): –

Abstraction: Abstraction means showing essential features and hiding non-essential features to the user.
In java, we use abstract class and interface to achieve abstraction.
For Example: Yahoo Mail…
When you provide the user name and password and click on submit button. It will show Compose, Inbox, Outbox, sent mails…so and so when you click on compose it will open…but user doesn’t
know what are the actions performed internally…. It just Opens….that is essential; User doesn’t know internal actions …that is non-essential things…

Encapsulation: Encapsulation means which binds the data and code (or) writing operations and methods in single unit (class).
For Example1:
A car is having multiple parts. Like, steering, wheels, engine… etc. Which binds together to form a single object that is car. So, here multiple parts of cars encapsulate itself together to form a single object that is Car.
For Example2:
A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.
In real time we are using Encapsulation for security purpose…
Encapsulation = Abstraction + Data Hiding.

Inheritance: Deriving a new class from the existing class, is called Inheritance.
Derived (sub class) class is getting all the features from Existing (super class\base class) class and also incorporating some new features to the sub class. It provides code reusability.
For Eg.,
class Address
{
String name;
String Street name;
}
class LatestAddress extends Address
{
String City;
String State;
String Country;
}
public class Vishal
{
{
LatestAddress la = new LatestAddress();
//Assign variable accordingly…
}
}
In the above Example class LatestAddress getting all features from the Address class.
In the LatestAddress class we have total 6 properties.3 are inherited from Address class and 3 properties are
incorporated. So in the class Vishal we are declaring the object of class LatestAddress and then assign new variables using the properties of the previous base classes… So this is a nice example of inheritance…

Polymorphism:
Polymorphism means ability to take more than one form that an operation can exhibit different behavior at different instance depend upon the data passed in the operation. In java, we use method overloading and method overriding to achieve polymorphism.
For Examples:
  1. We behave differently in front of elders, and friends. A single person is behaving differently at different time.
  2. A software engineer can perform different task at different instance of time depending on the task assigned to him. He can do coding, testing, analysis and designing depending on the task assign and the requirement.
  1. Consider the stadium of common wealth games. Single stadium but it performs multiple task like swimming, lawn tennis etc.
  1. If a girl is married and is the mother of 2 children, and is teaching, then she is a woman first, a teacher in a school when she is in school, wife of someone at home, mother of her children, and obviously daughter of someone. This means a woman plays different roles at different times and this is polymorphism (many forms).

Advantage of OOPs over Procedure-oriented programming language

1)OOPs makes development and maintenance easier where as in Procedure-oriented programming language it is not easy to manage if code grows as project size grows.
2)OOPs provides data hiding whereas in Procedure-oriented programming language a global data can be accessed from anywhere.
3)OOPs provides ability to simulate real-world event much more effectively. We can provide the solution of real word problem if we are using the Object-Oriented Programming language.
Q. What is the difference between object-oriented programming language and object-based programming language?
Object based programming language follows all the features of OOPs except Inheritance. JavaScript and VBScript are examples of object based programming languages.


Summary:

OOPs have following features:
  1.  Object – Instance of Class
  2.  Class – Blueprint of Object
  3. Encapsulation – Protecting our Data
  4. Polymorphism – Different behaviors at different instances
  5. Abstraction – Hiding our irrelevant Data
  6. Inheritance – One property of object is acquiring to another property of object

No comments:

Post a Comment