FANDOM


Object-oriented Programming is one of the fundamentals of the Java Language. Through this concept, we can explain how objects relate to the real world, while simultaneously providing an introduction to the syntax of the Java language.

ObjectsEdit

An object is any item of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life.

The State and Behaviour of an ObjectEdit

Look around you, there are many examples of real-world objects: your dog, your desk, your television set, and your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

Take a minute right now to observe the real-world objects that are in your immediate area. For each object that you see, ask yourself two questions: "What possible states can this object be in?" and "What possible behavior can this object perform?". Make sure to write down your observations. As you do, you'll notice that real-world objects vary in complexity; your desktop lamp may have only two possible states (on and off) and two possible behaviors (turn on, turn off), but your desktop radio might have additional states (on, off, current volume, current station) and behavior (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

Concepts-object

A typical software object.









Relationship between State and Behaviour in a Software ObjectEdit

  Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields (variables in some programming languages) and exposes its behavior through methods (functions in some programming languages). Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication. Hiding internal state and requiring all interaction to be performed through an object's methods is known as data encapsulation — a fundamental principle of object-oriented programming.

Consider a bicycle, for example:

Concepts-bicycleObject

A bicycle(real-world object) as a software object.










By attributing state (current speed, current pedal cadence, and current gear) and providing methods for changing that state, the object remains in control of how the outside world is allowed to use it. For example, if the bicycle only has 6 gears, a method to change gears could reject any value that is less than 1 or greater than 6. This explains part of the code:

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;  
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

General Explanation:

  • The method changeCadence produces a new value in the reading of the cadence.
  • The method changeGear produces a new value in the reading of the gear.
  • The method speedUp produces a new value in the reading of the speed which is obtained from the formula speed + increment.
  • The method applyBrakes produces a new value in the reading of the speed which is obtained from the formula speed - decrement.

ClassesEdit

A class defines an object's state and behaviour. Which is acts like a blueprint or prototype from which objects are created . In other words, it provides the basic structure for a software object.

Concept of a ClassEdit

In the real world, you'll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created.

From the previous code in the topic Objects, we can define a class as:

class Bicycle {

    int cadence = 0;
    int speed = 0;
    int gear = 1;

    void changeCadence(int newValue) {
         cadence = newValue;
    }

    void changeGear(int newValue) {
         gear = newValue;
    }

    void speedUp(int increment) {
         speed = speed + increment;   
    }

    void applyBrakes(int decrement) {
         speed = speed - decrement;
    }

    void printStates() {
         System.out.println("cadence:" +
             cadence + " speed:" + 
             speed + " gear:" + gear);
    }
}

The design of this class is based on the previous discussion of bicycle objects. The fields cadence, speed, and gear represent the object's state, and the methods (changeCadence, changeGear, speedUp etc.) define its interaction with the outside world.

Application of the Concept of ClassesEdit

A class does not contain a main method. That's because it's not a complete application; it's just a blueprint for an object that might be used in an application. The responsibility of creating and using new objects belongs to some other class in your application.

Below is a example of a complete class named BicycleTypes:

class BicycleTypes {
    public static void main(String[] args) {

        // Create two different types
        // of objects
        Bicycle bike1 = new Bicycle();
        Bicycle bike2 = new Bicycle();

        // Apply methods on 
        // the objects created
        bike1.changeCadence(50);
        bike1.speedUp(10);
        bike1.changeGear(2);
        bike1.printStates();

        bike2.changeCadence(50);
        bike2.speedUp(10);
        bike2.changeGear(2);
        bike2.changeCadence(40);
        bike2.speedUp(10);
        bike2.changeGear(3);
        bike2.printStates();
    }
}

General Explanation:

  • This is a class showing the types of a typical object which has state and behaviour of the same kind.
  • Creates two distinct types of the same object, which are Bicycle 1(labelled as bike 1) and Bicycle 2(labelled as bike 2)
  • Each type of object have undergone different methods to change the values cadence, speed and gear. Bike 1 have undergone a difference in cadence(+50), speed(+10) and gear(+2). Bike 2 have undergone a difference in cadence(+40), speed(+10) and gear(+3).
  • Note that the values in bike 2 are taken from the last method in each state.
  • The print state part just shows that the final values of cadence, speed and gear are stated out in the program.

InheritanceEdit

'How are two types of an object the same?', 'Why mountain bikes and the road bikes have the same state and behaviour, but different values in them?'

We can answer the above questions by considering Inheritance in object-oriented terms. Inheritance refers to the class of the same kind of object inheriting same characteristics from a main class called 'superclass'. Superclass in this instance is also called the kind of the object. Like your great great great...grandfather and grandmother, who gave birth to your grandfather, then your father, and finally to you. You inherit some of the characteristics from him. This may be passed to preceeding generations.

Concept of InheritanceEdit

Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current pedal cadence, current gear). Yet each also defines additional features that make them different: tandem bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain bikes have an additional chain ring, giving them a lower gear ratio.

In this case, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In Java, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses.

Concepts-bikeHierarchy

An example of a hierarchy for the superclass Bicycles.













The syntax for creating a subclass is simple. At the beginning of your class declaration, use the extends keyword, followed by the name of the class to inherit from(superclass). Like this:

class MountainBike extends Bicycle {

    // new fields and methods describing 
    // a mountain bike would go underneath
    // this line

}

This gives a MountainBike all the same fields and methods as Bicycle, yet allows its code to focus exclusively on the features that make it unique. This makes code for your subclasses easy to read. However, you must take care to properly document the state and behavior that each superclass defines, since that code will not appear in the source file of each subclass. From the previous examples, we document the state and behaviour of Bicycle, MountainBike should have all the following general features from a typical bicycle.

int cadence = 0;
int speed = 0;
int gear = 1;

A MountainBike has additional features, which are stated like this:

class MountainBike extends Bicycle {

    // new fields and methods describing
    // a mountain bike        
    MountainBike.changeCadence(50);
    MountainBike.speedUp(10);
    MountainBike.changeGear(2);
    MountainBike.printStates();
}

General Explanation:

  • This subclass provides additional features to the superclass to make it different from the other subclasses.
  • Yet this subclass inherits its state and behaviour from the superclass.

InterfacesEdit

An interface is a contract between a class and the outside world. When a class implements an interface, it promises to provide the behavior published by that interface. In other words, an interface is just something which provide a basic rule the operation of a class. For example, when you are in a game, you give commands to it. The interface is the thing that receives your command and tells which and what a class are going to do.

The Code-form of an InterfaceEdit

An interface is a group of related methods with empty bodies. A bicycle's behavior, if specified as an interface, might appear as follows:

interface Bicycle {

   //wheel revolutions per minute
   void changeCadence(int newValue);
   void changeGear(int newValue);
   void speedUp(int increment);
   void applyBrakes(int decrement);

}

To activate this interface(or command-giving class), the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'll use the implements keyword in the class declaration:

public class ACMEBicycle implements Bicycle {

    //remainder of this class before
    //implementation. That means
    //the methods before a command
    //was received to change it.

}

Note a keyword 'public' is added to the beginning of a interface-implementing class. You'll learn the reasons why in both the Classes & Objects and Interfaces & Inheritance trails.

PackagesEdit

A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into packages makes large software projects easier to manage. Conceptually you can think of packages as being similar to different folders on your desktop. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in Java can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

Some packages in source codes are understood by the way how it is written. But some applications make use of not only existing classes, they also make use of network sockets, GUI objects or anything else related to graphical user interfaces. This is where the provided API list comes in handy. You may learn more about this list here .

Summary of Object-oriented Programming ConceptsEdit

ObjectsEdit

  • Concept of Objects
  1. State and Behaviour
  2. Software Objects

ClassesEdit

  • Concept of Classes
  1. Composition of a Class
  • Application of Classes
  1. Creating Objects
  2. Applying Methods

InheritanceEdit

  • Concept of Inheritance
  1. Superclasses and Subclasses
  • Subclasses
  1. Creating Subclasses

InterfacesEdit

  • Concept of Interface
  1. Creating Interfaces

PackagesEdit

  • Concept and Usage of Packages
  1. Organizing and Providing Structure for Classes

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.