Evo. G Tech Team Forum
Welcome to Evo. G Tech Team Forum. We have moved to a new website : www.evogtechteam.com

Thanks you.

by Evo. G Tech Team Management.


View previous topic View next topic Go down


Post by LifeStyle on September 7th 2014, 04:03


This chapter introduces some of the basic principles of object-oriented programming (OOP):
declaring classes, creating objects, manipulating objects. OOP places data and the operations
that pertain to them within a single entity called an object. Using objects improve software
reusability and makes programs easy to develop and easy to maintain.

Defining Classes for Objects
• An object represents an entity in the real world that can be distinctly identified.
• For example a student, a car, a tree and even a loan can all be viewed as objects. An object has a unique identity, state and behaviors.
• A class is a blueprint that defines what an object’s data and method’s will be.

State – consists of a set of data fields with their current values. (Also known as properties)
Behavior – is defined by a set of methods

A CAR (Source : Java How to program 6th Deitel & Deitel)
 We need the engineering drawings to build a car. The drawing is similar to the blueprints used to design a house.
 The drawings represent as a class
 The drawings include the design and the complex mechanism of:
o accelerator pedal to make the car goes faster
o brake pedal that slow the car
o steering wheel that turn the car.
 The complex mechanisms are representing as methods
 We cannot drive the engineering drawings of a car. Someone has to build an object from the drawings.
 The real car is the object
 We must press its gas pedal make the car go faster.
 Pressing the gas pedal is similar to send a message to an object to perform a task. Known as a method call.
 The car has many attributes, such as its color, number of doors, the current speed and etc. These attributes are always associated with car.
 Every car maintains its own attributes. For example each car knows how much gas is in its own gas tank but not how much is in the tanks of other cars.
Similarly, an object has attributes that are carried with the object as it is used in a program. The attributes are specified as part of the object’s class. This is known as class’s instance variables.

UML Class Diagram
• The class can be presented in a diagram called UML class diagram.
• UML stands for Unified Modeling Language – a set of standard diagrams for graphically depicting object-oriented systems.
• Figure 4.1 shows the layout of a UML diagram for the Car class.
• ASK your lecturer to explain the basic notations of UML diagram.

[You must be registered and logged in to see this image.]

A Rectangle
• Any rectangle object can be described with its attributes: e.g: color, width and length.
• We can do many things to the rectangle, such as:
o Calculate the area
o Calculate the perimeter
o Paint color for the rectangle

[You must be registered and logged in to see this image.]

How to define a class?

You already know that every java program begins with a class declaration in which the keyword
class is followed by the class name. Once a class has been defined, it serves as a template, or
blueprint for creating individual objects or instances of the class.
So, classes:
• are constructs that define objects of the same type
• comprises a collection of data and method definitions
• An object is an instance of a class. (You can create many instances of a class)
Example: Program 1 – refer Example 2 (Rectangle class)

[You must be registered and logged in to see this image.]

• Explanation:
o This class is different from all of the other classes you have seen so far. It is
not an application.
o The class does not have a main method and therefore cannot be run
o It is a definition used to declare and create Rectangle objects.
o The class that contains the main method will be referred to as the main
class in this chapter.


• Java enables you to define a special method in the class, known as the constructor, which can be used to initialize an object’s data.
• Like methods, constructors can be overloaded, to construct objects with different initial data values.
• Note:
It is a common mistake to put the void keyword in front of a constructor. 
For example,
public void Rectangle() {}
In this case, Rectangle () is method, not a constructor.

Declare and Create Objects
How to declare
• Syntax: className objectName ;
• Example: Rectangle rectOne;

How to create (using new operator)
• Syntax: objectName = new className( );
• Example: rectOne = new Rectangle( );

(Will Be Continue On Next Week In C4 Part 2)

Posts : 10
Points : 25990
Reputation : 0
Join date : 2014-01-06
Location : Behide you

View user profile

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum