Vous êtes sur la page 1sur 11

OBJECT-ORIENTED

PROGRAMMING CONCEPTS
(Review)
What is an Object?
Objects have states and behaviors. Example: A dog
has states - color, name, breed as well as behaviors -
wagging, barking, eating. An object is an instance of
a class.

If we consider a dog, then its state is - name, breed,


color, and the behavior is - barking, wagging, running
What is a Class?
A class can be defined as a template/blue print that describes
the behaviors/states that object of its type. support.
A class can contain any of the following variable types:
Local variables: Variables defined inside methods, constructors
or blocks are called local variables. The variable will be declared and
initialized within the method and the variable will be destroyed
when the method has completed.
Instance variables: Instance variables are variables within a
class but outside any method. These variables are instantiated
when the class is loaded. Instance variables can be accessed from
inside any method, constructor or blocks of that particular class.
Class variables: Class variables are variables declared with in a
class, outside any method, with the static keyword.
What is Inheritance?
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
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
When we talk about inheritance, the most commonly
used keyword would be extends and implements

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:

class MountainBike extends Bicycle {

// new fields and methods defining

// a mountain bike would go here

}
The implements keyword is used by classes by inherit
from interfaces. Interfaces can never be extended by
the classes.

public interface Animal {

public class Mammal implements Animal{

}
What Is an Interface?
An interface is a collection of abstract methods. A class
implements an interface, thereby inheriting the abstract
methods of the interface.
An interface is not a class. Writing an interface is similar to writing a
class, but they are two different concepts. A class describes the
attributes and behaviors of an object. An interface contains behaviors
that a class implements.

interface Bicycle {
// wheel revolutions per minute
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
What Is a Package?
A package is a namespace that organizes a set of
related classes and interfaces

Conceptually you can think of packages as being


similar to different folders on your computer

You might keep HTML pages in one folder, images in


another, and scripts or applications in yet another

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 your own
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.
package insurance;

public class Policy{

//Code goes here

}
RESOURCES
http://docs.oracle.com/javase/tutorial/java/concepts/i
ndex.html