Object oriented programming : Classes

What is a class?

In object oriented programming parlance, a class is a blue print used to create different instances, called objects, of a domain entity. Classes encapsulate state and behavior of domain entity.  State is maintained using member variables and behavior using member functions.

For example, Bicycle is a class, it has three members : model name, top speed and number of gears. Based on this blueprint, we can instantiate objects like bike with 2 gears and 15 as top speed, another bike with 6 gears and 20 as top speed etc. Behaviors like change the top speed, get the number of gears are all encapsulate with in class and available in all instances of class Bicycle.

/**
 * Created by sangar on 22.10.17.
 *
 */
package Examples;
public class Bicycle {
    
    private int gears;
    private int topSpeed;
    
    public Bicycle(int gears, int topSpeed){
        this.gears = gears;
        this.topSpeed = topSpeed;
    }
    
    public int getGears(){
        return this.gears;
    }
    
    public void service(){
        System.out.println("Servicing for bike with grear"
           + this.gears + "costs $10");
    }
}

Are all classes used to create objects? Answer is no, as there are abstract classes and static classes, which cannot be instantiated as usual classes.

An abstract class contains one or more abstract methods. Abstract method does not have implementation, it only has declaration. An abstract class requires subclass to provide implementation of methods and instantiate it.

Continuing above example of bicycle, it would be nice if we can define different types of bicycles like city bikes, mountain bikes and racing bikes. All these bikes will have some common behavior like service, but the implementation of that behavior would be different for each one it. In this scenario, Bicycle becomes an abstract class and declares member function without implementation. City bike,  Mountain bike and Racing bike are sub-classes of bicycle abstract class and implement the service method.

package Examples;

/**
 * Created by sangar on 22.10.17.
 */
public abstract class AbstractBicycle {
    //member fields
    protected int gears;
    protected int topSpeed;
    protected int serviceCost;

    public int getGears(){
        return this.gears;
    }

    public abstract void service();
}
package Examples;

/**
 * Created by sangar on 22.10.17.
 */
public class MountainBike extends AbstractBicycle {

    public MountainBike(int gear,
                        int topSpeed, int serviceCost){
        this.gears = gears;
        this.topSpeed = topSpeed;
        this.serviceCost = serviceCost;
    }

    public void service() {
        System.out.println("Servicing Mountain Bike :");
        System.out.println("Oil the chain");
        System.out.println("Check tyre pressure");
        System.out.println("Oil brake");
    }
}
package Examples;

/**
 * Created by sangar on 22.10.17.
 */
public class CityBike extends AbstractBicycle {
    public CityBike(int gear,
                    int topSpeed, int serviceCost){
        this.gears = gears;
        this.topSpeed = topSpeed;
        this.serviceCost = serviceCost;
    }

    public void service() {
        System.out.println("Servicing City Bike :");
        System.out.println("Oil the chain");
        System.out.println("Fix the lock");
    }
}
package Examples;

/**
 * Created by sangar on 22.10.17.
 */
public class BicycleStore {

    public static void main(String[] args){
        MountainBike mountainBike = new MountainBike(3,15,7);
        mountainBike.service();

        CityBike cityBike = new CityBike(6,10,10);
        cityBike.service();
    }
}

On the other hand, static class is a class, which is never required to be instantiated. It contains member values which will not differ across instance and methods, which do not depend on external context.  In Java specifically, nested classes are static in nature.

What does a class contain?

As mentioned above, a class will contain member fields and functions. A class can also have one or more constructors.

Member fields can be public, private or protected. In short, public members can be access from outside of the class, using objects. Private members are not visible outside of class and can only be accessed by member functions of class itself. Protected ones have different rules when it comes to inheritance, but usually, accessible from within package, subclass and not accessible to outside world.

Access control specifier in Java

One can have setters and getters on fields of class. Setters set the value of fields and getters retrieve value of fields.

We will be discussing more advance things in following posts, so please stay tuned. Also, if you find anything wrong or missing, please let us know. We would be more than happy to correct things.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s