"Object Oriented Programming"
One of the most important features of Java is that it is a "purely" Object Oriented Language.So before we learn to write programs in Java we must understand what exactly is OOP's.
In Object Oriented programming the emphasis is on developing a program around the data rather then the procedure.Programs are divided into small segments known as "Objects".One of the biggest advantages of OOP's is that data is hidden and cannot be accessed by any other external function.Another plus point is that new data members and methods can be easily added whenever necessary.We shall discuss the advantages and applications of OOP later on.
Since OOP is a fairly recent concept different people have different understanding and different working definition of OOP.So we shall summarize the concept as follows:
Whenever an object is created,a memory block is created in the computer memory.It is this block that stores data and the operations that can modify the data.Also different objects are independent of each other so we can run a variety of programs without any modification.
The Main Principles of OOP's
Data Abstraction:
Humans are always interested only in the final product and not in the complexity of the parts that form the product.
We all wear a watch,but how many of us exactly know the functioning of the gear inside it!We all drive a car but how many of us have ever popped its hood and checked of the engine works!!!
THIS IS WHAT DATA ABSTRACTION IS.
In technical terms it means that the complexity of the program is hidden from the user and only the end product is shown as one huge application.
Encapsulation:
Automatic Cars are a fine example of data encapsulation.It encapsulates a number of data like your acceleration,the road type on which you are driving,the position of the shift lever etc.All you have to do is to change the gear to affect all this recorded data.Only this action will affect your data and not any other action such as turning on your wiper or the headlight!!!It is true even vice verse.
This is a universal basic working principle for automatic car manufactures.But the end product is the same for all the companies,A AUTOMATIC TRANSMISSION CAR.
If we think on the same lines,we can combine all the methods,all the data into a single unit(called class).This data is not available to the methods of other classes(just like the technology of BMW is not known to Merc).This increases the security aspect of the application that has been developed.
Inheritance:
Let us study an example of Inheritance first:
BMW is the name of a company that manufactures cars.Cars can be more generally classified as automobiles. Hence because of inheritance we specify only the special attributes of that object otherwise for a more general or a broader view we refer to the general category into which that object falls.
Programming example:Let us create a class employee which accepts the name,salary,post of the employee.
Now we can use this class for any company and we do not have to rewrite this function.We shall simply inherit this function when and where we require to accept the name,salary and post.
Polymorphism:
Polymorphism is a Greek word which means "many forms".It is basically the ability of an operator or operation to behave in different forms in different situations.This behavior depends on the type of data used in operation.
eg: The Plus Sign:(+):When two numbers are added using the + sign then the sum of the two numbers is displayed.But if + is used for two string then a third string will be produced which will be the concatenation of the other two stings.
int x=5;
int y=5;
int z=x+y; //The value of z will be 10
String a="Yash";
String b="agrawal";
String c=a+b; //c will store Yashagrawal
Hence we conclude that OOP is an Integral part of modern day programming which needs to be understood in depth to become a successful programmer.
Now we can use this class for any company and we do not have to rewrite this function.We shall simply inherit this function when and where we require to accept the name,salary and post.
Polymorphism:
Polymorphism is a Greek word which means "many forms".It is basically the ability of an operator or operation to behave in different forms in different situations.This behavior depends on the type of data used in operation.
eg: The Plus Sign:(+):When two numbers are added using the + sign then the sum of the two numbers is displayed.But if + is used for two string then a third string will be produced which will be the concatenation of the other two stings.
int x=5;
int y=5;
int z=x+y; //The value of z will be 10
String a="Yash";
String b="agrawal";
String c=a+b; //c will store Yashagrawal
Hence we conclude that OOP is an Integral part of modern day programming which needs to be understood in depth to become a successful programmer.
No comments:
Post a Comment