|
|
|
|
|
|
|
|
Before writing code in Java, some basic OOP (Object Oriented Programming) concepts are needed to be learnt which are:
|
|
|
| Object |
|
An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that are found in everyday life. Details...
|
| What is an Object? |
|
An object is any 'thing' which comprises of state and behavior. To give a real world analogy, look around right now and many examples of real-world objects will be found: dog, desk, television set and bicycle.
|
Now, pick up the dog example, it has two characteristics:
- state (name, gender, color, hungry) and
- behavior (barking, eating, sleeping, 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.
|
|
Some objects, in turn, also contain other objects. These real-world observations all translate into the world of object-oriented programming.
|
| Now... |
| What is a Software Object? |
|
Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. Like in real world, dogs and bicycles are the objects. In programming world, they can be implemented as Software objects. In Java programming world, an object stores its state in fields (variables in other some programming languages) and exposes its behavior through methods (functions in some other programming languages).
|
A software object.
|
|
Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.
|
| Data Encapsulation |
|
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.
|
| A bicycle implemented as 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.
|
A bicycle modeled as a software object.
|
|
|
| Class |
|
A class is a blueprint or prototype from which individual objects are created.
|
|
In the real world, many same 'kind individual' objects can be found.
|
|
Extending bicycle example.
|
|
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, it is being said that bicycle is an instance of the class of objects known as bicycles.
|
|
The following Bicycle class is one possible implementation of a bicycle:
|
class Bicycle {
int currentPedalCadence = 0;
int currentSpeed = 0;
int currentGear = 0; // 0 => Neutral
void changeCadence(int newValue) {
currentPedalCadence = newValue;
}
void speedUp(int increment) {
currentSpeed = currentSpeed + increment;
}
void applyBrakes(int decrement) {
currentSpeed = currentSpeed - decrement;
}
void changeGear(int newValue) {
currentGear = newValue;
}
void printStates() {
System.out.println("cadence:"+currentPedalCadence+" speed:"+speed+" currentSpeed:"+currentGear);
}
}
|
|
You may have noticed that the Bicycle class does not contain a main method. That's because it's not a complete application; it's just the blueprint for bicycles that might be used in an application. The responsibility of creating and using new Bicycle objects belongs to some other class in your application.
|
|
Here's a BicycleDemo class that creates two separate Bicycle objects and invokes their methods:
|
class BicycleDemo {
public static void main(String[] args) {
// Create two different Bicycle objects
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Invoke methods on those objects
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();
}
}
|
|
The output of this test prints the ending pedal cadence, speed, and gear for the two bicycles:
|
cadence:50 speed:10 gear:2
cadence:40 speed:20 gear:3
|
|
| Inheritance |
|
Inheritance provides a powerful and natural mechanism for organizing and structuring the software. Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.
|
|
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 example, Bicycle now becomes the superclass of MountainBike, RoadBike, and TandemBike. In the Java programming language, each class is allowed to have one direct superclass, and each superclass has the potential for an unlimited number of subclasses:
|
A hierarchy of bicycle classes.
|
|
Multiple Inheritance is not allowed in Java. Multiple Inheritance is the phenomenon when a subclass inherits state and behavior from many different superclasses.
|
|
The syntax for creating a subclass is simple. At the beginning of class declaration, use the extends keyword, followed by the name of the class to inherit from:
|
class MountainBike extends Bicycle {
// new fields and methods defining a mountain bike would go here
}
|
|
This gives 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 subclasses easy to read. However, it must be taken 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.
|
|
| Interface |
|
An interface is a contract between a class and the outside world. It is a group of related methods with empty bodies. When a class 'implements' an interface, it promises to provide the behavior published by that interface.
|
|
A bicycle's behavior, if specified as an interface, might appear as follows:
|
interface Bicycle {
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
|
|
To implement this interface, the name of class would change (to ACMEBicycle, for example), and the 'implements' keyword is used in the class declaration:
|
class ACMEBicycle implements Bicycle {
// remainder of this class implemented as before
}
|
|
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
|
|
| Package |
|
A package is a namespace for organizing classes and interfaces in a logical manner. Placing code into packages makes large software projects easier to manage.
|
|
It organizes a set of related classes and interfaces. Conceptually it can be thought of packages as being similar to different folders on a computer. HTML pages might be kept in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language 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.
|
|
The Java platform provides an enormous class library (a set of packages) suitable for use in applications. This library is known as the "Application Programming Interface" or "API" for short. Its packages represent the tasks most commonly associated with general-purpose programming. For example, a String object contains state and behavior for character strings; a File object allows a programmer to easily create, delete, inspect, compare, or modify a file on the filesystem; a Socket object allows for the creation and use of network sockets; various GUI objects control buttons and checkboxes and anything else related to graphical user interfaces. There are literally thousands of classes to choose from. This allows the programmers to focus on the design of their particular application, rather than the infrastructure required to make it work.
|
|
|
|
Comments/Feedback:
Please login to participate |
OneInfoPlace server is currently busy. Please try later. Sorry for the inconvenience | | |