0% found this document useful (0 votes)
38 views12 pages

Chapter 5 Introducing Classes

The document discusses classes in Java, including how to declare a class, define methods and instance variables within a class, create objects of a class using the new operator, call methods on objects, add parameters to methods, and use constructors to initialize objects.

Uploaded by

J A Y T R O N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views12 pages

Chapter 5 Introducing Classes

The document discusses classes in Java, including how to declare a class, define methods and instance variables within a class, create objects of a class using the new operator, call methods on objects, add parameters to methods, and use constructors to initialize objects.

Uploaded by

J A Y T R O N
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

INTRODUCING CLASSES

OBJECTIVES
How to declare a class and use it to create an object.
How to implement a class’s behaviors as methods.
How to implement a class’s attributes as instance variables and
properties.
How to call an object’s methods to make them perform their tasks.
How to use a constructor to initialize an object’s data.
INTRODUCTION
Object-oriented programming in Java is based on classes. Any concept you wish to
implement in a Java program must be encapsulated within a class.
A class defines a new data type.
Once defined, this new type can be used to create objects of that type.
Thus, a class is a template for an object, and an object is an instance of a class.
CLASS FUNDAMENTALS
The data, or variables, defined within a class are called instance variables.
The code is contained within methods.
Collectively, the methods and variables defined within a class are called members of
the class.
Variables defined within a class are called instance variables because each instance
of the class (that is, each object of the class) contains its own copy of these variables.
DECLARING A CLASS WITH A METHOD
Every class declaration contains keyword class followed immediately by
the class’s name.
Every class’s body is enclosed in a pair of left and right braces

public class AreaOfCircle {


//display a message to the user
public void displayMesg(){
System.out.println("This application calculates the area
of a circle.");
}
}
DECLARING OBJECTS
As just explained, when you create a class, you are creating a new
data type.
You can use this type to declare objects of that type.
First, you must declare a variable of the class type.
Second, you must acquire an actual, physical copy of the object
and assign it to that variable.
You can do this using the new operator.
Then new operator dynamically allocates (that is, allocates at run
time) memory for an object and returns a reference to it.
DECLARING OBJECTS
AreaOfCircle areaOfCircle1 = new AreaOfCircle();

A class that contains method main begins the execution of a Java


application.
To access the members of the object created, you will use the dot
(.) operator.
CALLING A METHOD OF OBJECTS

public static void main(String[] args) {


AreaOfCircle areaOfCircle1 = new AreaOfCircle();
areaOfCircle1.displayMesg();
}
DECLARING A METHOD WITH A PARAMETER
A parameter represents additional information the method needs to
perform its task.
Parameters are defined in a comma-separated parameter list, which is
located inside the parentheses that follow the method name.
Each parameter must specify a type and a variable name.
The parameter list may contain any number of parameters, including
none at all.
Empty parentheses following the method name indicate that a method
does not require any parameters.
DECLARING A METHOD WITH A PARAMETER
Argument to a Method
A method call supplies values—called arguments—for each of the method’s
parameters.

public void displayName(String userName){


System.out.println("Your name is: " + userName);
}
INITIALIZING OBJECTS WITH CONSTRUCTORS
A constructor is used to initialize an object of a class when the
object is created.
In fact, Java requires a constructor call for every object that’s
created.
Keyword new requests memory from the system to store an object,
then calls the corresponding class’s constructor to initialize the
object.
A constructor must have the same name as the class.
INITIALIZING OBJECTS WITH CONSTRUCTORS
public class NameApp {
String userName;
public NameApp (String name){
userName = name;
}//end of constructor

public void displayName(String Name){


System.out.println("Your name is: " + Name);
}

You might also like