0% found this document useful (0 votes)
4 views

CreatingClass Part-1

The document discusses creating classes in Java. It explains that classes allow creating new data types and describes the procedure for defining a class which includes writing a class definition in a .java file. It also explains the components of a class definition including attributes, behaviors, data members, methods, and constructors.

Uploaded by

AM Series
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

CreatingClass Part-1

The document discusses creating classes in Java. It explains that classes allow creating new data types and describes the procedure for defining a class which includes writing a class definition in a .java file. It also explains the components of a class definition including attributes, behaviors, data members, methods, and constructors.

Uploaded by

AM Series
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Creating Classes

Instructor: Abdul Hameed


Objectives
Introduction

Students should:
• Recall the meaning of classes and objects in Java
• Know the components in the definition of a Java
class
• Understand how constructors work
• Be able to create class and object methods
• Be able to create new Java classes and use them
correctly
Define Your
Categories Own Data Type
of Data
• There are two categories of data in Java
– primitive data type
– class
• We cannot create a new primitive data
type.
• they can create new data types
– by creating new classes containing attributes
and behaviors of the desired data types.
Define Your
Procedure OwnaData
to create newType
class
• Creating a new class
– write a class definition associated with that
class in specific Java syntax.
– save the definition in a separated .java file
named after the name of the class.
• Once the definition is created, other
programs can utilize the newly created
data type or class in a similar fashion to
the primitive data types or other existing
classes.
Exampleclass
MyPoint
• we would like to create a new data type
for representing points in a Cartesian co-
ordinate
– create a new class called MyPoint.
public class MyPoint
{
// a blank class definition
// there’re no details yet
}
Example to create a new class
Procedure
• This definition has to be saved using the
name MyPoint.java.
• we can write another program that makes
use of this class.
Example
Main Function

public class TestMyPoint1 1


{ 2
public static void main(String[] args) 3
{ 4
MyPoint p, q; 5
p = new MyPoint(); 6
q = new MyPoint(); 7
} 8
} 9
Example of the code
Explanation
• variables p and q are declared as variables
of the type MyPoint on line 5.
MyPoint p, q;

• On line 6 and line 7, p and q are assigned


with, or in other words, are made to refer
to, new instances, or objects, of the class
MyPoint using the keyword new.
p = new MyPoint();
q = new MyPoint();
Define Your
Explanation ofOwn Data Type
the code
• Notice that source codes of Java programs
that we have written so far, they take the
same structure as class definitions.
• They are in fact class definitions.
• Java programs are classes that contain the
methods named main() which make the
class executable.
Attributes
ComponentsandofBehaviors
Class Definitions

• The main functionality of a class definition


is to define attributes and behaviors of
that class.
• Attributes are entities defining properties
of an object.
• Behaviors are actions (or reactions) of an
object.
example attributes and behaviors of some
Example:
objects. Attributes and Behaviors

Object Attributes Behaviors


type
Point in a •The x •Moving the point a specified
2D space coordinate location
•The y •Calculating distance from the
coordinate point to a specified location
•etc. •etc.
example attributes and behaviors of some
Example:
objects. Attributes and Behaviors
Object Attributes Behaviors
type
Graphical •Location of •Calculating the length of the
line in a the starting line
3D space point •Moving the starting point to a
•Location of specified location
the ending •Moving the ending point to a
point specified location
•Color •Changing the color of the line
•etc. •etc.
example attributes and behaviors of some
Example:
objects. Attributes and Behaviors
Object type Attributes Behaviors
Complex •Value of the •Adding the object with another
number real part complex object,
•Value of the •Multiplying the object with
imaginary another complex object
part •Finding the conjugate of the
•etc. object
•Setting the real part to a specific
number
•Showing String representation
of the object
•etc.
example attributes and behaviors of some
Example:
objects. Attributes and Behaviors
Object Attributes Behaviors
type
Matrix •Members •Adding elements to the object
•etc. •Finding determinant
•Adding the object with
another matrix object
•Finding the inverse of the
object
•Raising to the power of n
•etc.
example attributes and behaviors of some
Example:
objects. Attributes and Behaviors
Object Attributes Behaviors
type
Car •Body color •Starting the engine
•Dimensions •Shutting down the engine
•Weight •Showing the name of its
•Number of manufacturer
doors •Accelerating
•Manufacturer •Decelerating
•Engine status •etc.
•etc.
example attributes and behaviors of some
Example:
objects. Attributes and Behaviors
Object Attributes Behaviors
type
Bank •Account •Showing the current balance
account name •Showing all info associated
•Owner with the account
•Account •Withdrawing money from the
type account
•Balance •Depositing to the account
•etc. •Closing the account
•etc.
example attributes and behaviors of some
Example:
objects. Attributes and Behaviors
Object Attributes Behaviors
type
Customer •Customer ID •Showing all info of the
•First name customer
•Family name •Changing the credit line
•Credit line •Checking whether the
•Gender customer’s favorite product
consists of a
•Favorite
products •specified product
•etc. •etc.
Attributes
ComponentsandofBehaviors
Class Definitions

• To describe attributes and behaviors of


objects of the class, a class definition can
consist of
– data member or fields
– methods
– constructors
Attributes
ComponentsandofBehaviors
Class Definitions

• An object’s attribute is represented using a


data member.
• Variables used for storing data members
are called instance variables.
• The behaviors of an object are described
using methods.
• Constructors are special methods invoked
whenever objects of the class are created.
Example:
Components of Class Definitions

You might also like