|
|
|
|
|
|
|
|
|
|
||||||||||
|
Object
Oriented Programming is a kind of modeling problems in the way we think.
For example, we have integers, floats, bytes, booleans in programming languages
but we also have courses, universities, cars, dogs, computers etc. in real
life. If we think every thing as an object, we can model everything. For
example, a car is an object. However, there are various types of cars in
the world. Thus, we can model any car by using a superset of their shared
properties. For example; every car has
a price production date license palate color etc.
double topSpeed; double price; int productionYear; String licensePalate; String color; } An object is a specific instant of a class with specific values. For example;
myCar.topSpeed = 180; myCar.price=2500000000 myCar.productionYear = 1993; myCar.licensePalate = "06ABC01"; myCar.color = "WHITE"; We didn't finish by defining the Classes and instantiating their objects. Objects are nothing without methods which tell information about Classes. I should mention about a special object: "this". "this" is a special object which refers to the object who invokes the method or class. We do not need to use "this" object explicitly as the compiler can understand which object we mean. Let's improve Car class. We may want to know about the field values. Thus, we can write the following method to get the color of the car.
return (this.color); }
int i; int past; double lastPrice; past = year - this.productionYear; lastPrice
= this.price;
return lastPrice;
public Static String
getVersion() {
We may also prevent others to use some parts of our classes. That is, we may want to privatize those parts. Then, we use private keyword:
We need to know which parts should be public, private, etc. As a rule of thumb:
Fields are private. Constructors are public. Getter and setter methods (which update the properties of objects) are public. Other methods must be decided on a case-by-case basis. |
|
|
|
|
|
|