BILKENT UNIVERSITY
CS533 Information Retrieval Systems
Technical Presentation Web Site
Barýþ UZ
 
Index of
this page
Introduction
Push Technologies
Channels
CGI
ASP
Java
Streaming Media
 
Overview
 Classes
Main Data Structures in Java
Object Oriented Programming
More about Object Oriented Programming
Applets
GUI
Images and audio
Multithreaded Programming
Links
 
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 
    the highest speed 
    a price 
    production date 
    license palate 
    color etc. 
We can think this as a class. That is; 
    class Car { 
       double topSpeed; 
       double price; 
       int productionYear; 
       String licensePalate; 
       String color; 
    }
The variables topSpeed, ..., color are called instance variables or fields or member variables of a class. 

An object is a specific instant of a class with specific values. For example; 

    Car myCar = new Car(); 
    myCar.topSpeed = 180; 
    myCar.price=2500000000 
    myCar.productionYear = 1993; 
    myCar.licensePalate = "06ABC01"; 
    myCar.color = "WHITE";
defines an instant of a Car class. 

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. 

    String getCarColor() { 
       return (this.color); 
    }
We may also want to know the price of our car according to current year with a constant amount of increase. Thus, we may need to add: 
    final double final ratio=0.95;
and the following method: 
    double calculatePrice(int year) { 
       int i; 
       int past; 
       double lastPrice; 

       past = year - this.productionYear; 

       lastPrice = this.price; 
       for (i = 0; i < past; i++) 
            lastPrice += lastPrice*0.95; 

      return lastPrice; 
    }

We have a special type of objects which are called static. Static fields or methods are effective in all of the objects in the same class. Whenever a value of a static variable (field) is changed in any object of that class, that value is changed in all of the other objects. A common use of this property is to handle versions of classes. Accessing these variables are not by name of the instance variable, but the name of the calss instead. For example, if we add 
    static String version = "1.0"; 

    public Static String getVersion() { 
       return version; 
    }

to our Car class, we can get this information anywhere in the code by the following line: 
    Car.getVersion();
We cannot call non-static objects and methods from a static one. They must rather explicitly specify which instance of the class they are referring to. 

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: 

    private final double ratio = 0.95;
We may also want to protect some parts of our classes to enforce constratints on the force, to provide a simpler interface, and to be able to alter the implementation without touching interfaces. For example, we can define a licensePalateC class to use for licensePalates, instead of String class. 

We need to know which parts should be public, private, etc. 

As a rule of thumb: 

    Classes are public.  
    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.
All of these rules may be freely violated if you have a reason for doing so. These are simply the defaults that handle 90% of the cases.  
 
 
 
 
PREVIOUS TOPIC
UP
(First page of this topic)
NEXT TOPIC
PREVIOUS PAGE 
(of this topic)
NEXT PAGE 
(of this topic)