What is Object Oriented Programming explain with an example?
In Java everything is an object or a class (or a piece of one or a collection of various).
Objects send messages to each other through calling methods.
Instance techniques belong to a particular object.
Static techniques belong to a particular class.
Example 1: The Car Class
Suppose you required writing a traffic simulation program in which watches cars going past an intersection. Each car has a speed, a maximum speed, and a license plate in which uniquely identifies it. In traditional programming languages you'd have two floating point and one string variable for each car. With a class you combine these within one thing like this.
class Car {
String licensePlate; // e.g. "New York 543 A23"
double speed; // in kilometers per hour
double maxSpeed; // in kilometers per hour
}
These variables (licensePlate, speed and maxSpeed) are called the member variables, instance variables, or fields of the class.
Fields tell you what a class is and what its properties are.
An object is a specific instance of a class along with particular values (possibly mutable) for the fields. Although a class is a general blueprint for objects, an instance is a particular object.
Remember the use of comments to specify the units. That's significant. A unit confusion between pounds and newtons led to the loss of NASA's $94 million Mars Climate Orbiter. (Believe it or not that's a cheap mission by NASA standards. If you're rich enough in which you don't have to worry about losing $94 million worth of work, you don't have to put comments in your source code. Everybody else has to use comments.)
How would you write an Angle class?