CSC 302 (Introduction to OOP)
CSC 302 (Introduction to OOP)
(Introduction to OOP)
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.
Can we think of real world object by identifying their state and 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.
Benefits of using objects
1. Modularity: The source code for an object can be written and maintained independently of the source
code for other objects. Once created, an object can be easily passed around inside the system.
2. Information-hiding: By interacting only with an object's methods, the details of its internal
implementation remain hidden from the outside world.
3. Code re-use: If an object already exists (perhaps written by another software developer), you can use
that object in your program. This allows specialists to implement/test/debug complex, task-specific
objects, which you can then trust to run in your own code.
4. Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply
remove it from your application and plug in a different object as its replacement. This is analogous to
fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
Class
A class is the blueprint from which individual objects are created.
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.
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:
Analogy
A BMW is an object of class car, A Honda, Peugeot and Toyota are all objects of class car because they
are all types of cars.
Class car {
}
class UniversityMain {
class carMain {
public static void main(String args[]) {
public static void main(String args[]) {
}
}
constructor
Is used for creating objects of a class. A constructor is the channel by which
classes can communicate.