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

Chapter Two - Class and Object

1. Iteration statements like while, do-while, and for loops allow code to be repeatedly executed. 2. Classes define new data types that can be used to create objects with their own state and behavior. 3. Methods define reusable blocks of code that can be invoked on objects to perform actions.

Uploaded by

lencho03406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Chapter Two - Class and Object

1. Iteration statements like while, do-while, and for loops allow code to be repeatedly executed. 2. Classes define new data types that can be used to create objects with their own state and behavior. 3. Methods define reusable blocks of code that can be invoked on objects to perform actions.

Uploaded by

lencho03406
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Iteration Statements

• There may be a situation when you need to execute a block of


code several number of times. In general, statements are executed
sequentially:

• The first statement in a function is executed first, followed by the


second, and so on.

• A loop statement allows us to execute a statement or group of


statements multiple times

1
while loop
• A while loop statement in Java programming language repeatedly
executes a target statement as long as a given condition is true.
• The syntax of a while loop is −

• Statement(s) may be a single statement or a block of statements.


• The condition may be any expression, and true is any non zero value.
• When executing, if the boolean_expression result is true, then the
actions inside the loop will be executed. This will continue as long as
the expression result is true.
• When the condition becomes false, program control passes to the line 2
while loop…

3
do...while loop

• A do...while loop is similar to a while loop, except that a do...while


loop is guaranteed to execute at least one time.

• The Boolean expression appears at the end of the loop, so the


statements in the loop execute once before the Boolean is tested.
• If the Boolean expression is true, the control jumps back up to do
statement, and the statements in the loop execute again. This process
repeats until the Boolean expression is false.

4
do...while loop

5
For loop
• A for loop is a repetition control structure that allows you to
efficiently write a loop that needs to be executed a specific number of
times.

• A for loop is useful when you know how many times a task is to be
repeated.

• The syntax of a for loop is −

6
For loop…

• Here is the flow of control in a for loop −


• The initialization step is executed first, and only once. This step allows you to declare and
initialize any loop control variables and this step ends with a semi colon (;).
• Next, the Boolean expression is evaluated. If it is true, the body of the loop is executed. If
it is false, the body of the loop will not be executed and control jumps to the next statement
past the for loop.
• After the body of the for loop gets executed, the control jumps back up to the update
statement. This statement allows you to update any loop control variables. This statement
can be left blank with a semicolon at the end.
• The Boolean expression is now evaluated again. If it is true, the loop executes and the
process repeats (body of loop, then update step, then Boolean expression). After the
Boolean expression is false, the for loop terminates.
7
For loop…

8
Break and continue statement
• The break statement in Java programming language has the following
two usages −
⁻ When the break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next
statement following the loop.
⁻ It can be used to terminate a case in the switch statement (covered in
the next chapter).
• The continue keyword can be used in any of the loop control structures.
It causes the loop to immediately jump to the next iteration of the loop.
⁻ In a for loop, the continue keyword causes control to immediately
jump to the update statement.
⁻ In a while loop or do/while loop, control immediately jumps to the
Boolean expression.
9
Chapter Two: Defining Classes and Objects
• The most important thing to understand about a class is that it 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.

• Because an object is an instance of a class, you will often see the two
words object and instance used interchangeably.
10
Class Definition(cont’d…)

• When you define a class, you declare its exact form and nature.
• You do this by specifying the data that it contains and the code that
operates on that data.

• 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.
11
Simple Class

• As stated, a class defines a new type


of data. In this case, the new data
type is called Box.
• You will use this name to declare
objects of type Box.
• It is important to remember that a
class declaration only creates a
template; it does not create an actual
object.
• Thus, the code found at right side
does not cause any objects of type
Box to come into existence.
12
Simple Class…
• To actually create a Box object, you will use a statement like the following:
• Box mybox = new Box(); // create a Box object called mybox
• After this statement executes, mybox will be an instance of Box.
• Each time you create an instance of a class, you are creating an object that
contains its own copy of each instance variable defined by the class.
• Thus, every Box object will contain its own copies of the instance variables
width, height, and depth.
• To access these variables, you will use the dot (.) operator.
• The dot operator links the name of the object with the name of an instance
variable. For example, to assign the width variable of mybox the value 100,
you would use the following statement: mybox.width = 100;

13
Declaring Objects
• when you create a class, you are creating a new data type.
• you can use this type to declare objects of that type.

• obtaining objects of a class is a two-step process.

• First, you must declare a variable of the class type. This variable
does not define an object. Instead, it is simply a variable that can
refer to an object.

14
Declaring Objects…
• 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.

• The new operator dynamically allocates memory for an object


and returns a reference to it.

• This reference is the address in memory of the object allocated


by new.
15
Declaring Objects…

16
Declaring Object(cont’d…)
• An object has a unique identity, state, and behavior.
• The state of an object (also known as its properties or attributes) is
represented by data fields with their current values.
• For example, A circle object has a data field radius, which is the
property that characterizes a circle.
• The behavior of an object (also known as its actions) is defined by
methods.
• To invoke a method on an object is to ask the object to perform an
action.
• For example, you may define a method named getArea() for circle
objects. A circle object may invoke getArea() to return its area.
17
Access modifier

• Private members can be accessed only from the inside of the class.

• Public members can be accessed from any other classes.

• Often it is desirable to allow subclasses to access data fields or


methods defined in the superclass, but not allow non-subclasses to
access these data fields and methods. To do so, you can use the
protected keyword.

• A protected data field or method in a superclass can be accessed in


its subclasses
18
Introducing Methods
• A method is a block of code or collection of statements or a set
of code grouped together to perform a certain task or operation.
• It is used to achieve the reusability of code
• We write a method once and use it many times.
• We do not require to write code again and again.
• It also provides the easy modification and readability of code,
just by adding or removing a chunk of code.
• The method is executed only when we call or invoke it.

19
Method Declaration

• The method header specifies the modifiers, return value type, method
name, and parameters of the method.

• The return Type is the data type of the value the method returns.

• Some methods perform desired operations without returning a value.


• In this case, the returnValueType is the keyword void.

• If a method returns a value, it is called a value returning method,


otherwise it is a void method.
20
Method Declaration…
• The variables defined in the method header are known as formal
parameters or simply parameters.

• A parameter is like a placeholder. When a method is invoked, you pass


a value to the parameter.
• This value is referred to as an actual parameter or argument.

• The parameter list refers to the type, order, and number of the
parameters of a method.
21
Method Declaration…
• The method name and the parameter list together constitute the method
signature.

• Parameters are optional; that is, a method may contain no parameters.

• In creating a method, you define what the method is to do.

• To use a method, you have to call or invoke it.

• Calling method means using that method to perform the defined tasks
for that method.
22
Method Declaration…

23
Types of method

 predefined methods are the method that is already defined in the Java class
libraries.
• It is also known as the standard library method or built-in method.
• We can directly use these methods just by calling them in the program at any
point.
• Some pre-defined methods are length(), equals(), compareTo(), etc.
• When we call any of the predefined methods in our program, a series of codes
related to the corresponding method runs in the background that is already
stored in the library.
User-defined Method
• The method written by the user or programmer is known as a user-
24
defined method. These methods are modified according to the requirement.
Passing Parameters
• The power of a method is its ability to work with parameters.
• When calling a method, you need to provide arguments, which must
be given in the same order as their respective parameters in the method
signature. This is known as parameter order association.
• For example, the following method prints a message n times:

25
Constructors

• In Java a constructor is a block of codes similar to the method. It is called


when an instance of the class is created.
• At the time of calling constructor, memory for the object is allocated.
• It is a special type of method which is used to initialize the object.
• Every time an object is created using the new() keyword, at least one
constructor is called.
• Java compiler calls a default constructor if there is no constructor
available in the class.
Note: It is called constructor because it constructs the values at the
time of object creation.
26
Constructors…
• The following are rules used to define the constructor.
• Constructor name must be the same as its class name
• A Constructor must have no explicit return type
• A Java constructor cannot be abstract, static, final, and
synchronized

• Like regular methods, constructors can be overloaded (i.e., multiple


constructors can have the same name but different signatures),
• This make it easy to construct objects with different initial data values.
27
static variable

• If you declare any variable as static, it is known as a static


variable.
• The static variable can be used to refer to the common
property of all objects (which is not unique for each object), for
example, the company name of employees, college name of
students, etc.
• It gets memory only once in the class area at the time of class
loading.
• It makes your program memory efficient (i.e., it saves
memory).

28
Static Method

• Sometimes a method performs tasks that does not depend on an object


• Such methods applies to the class in which it is declared as a whole
and it is known as static methods
• To declare a method as static, place keyword static before the return
type in method declaration.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an
instance of a class.
• A static method can access static data member and can change the
value of it.

29
Final keyword
• The final keyword in java is used to restrict the user. The java
final keyword can be used in many context. Final can be:
Java final variable
• If you make any variable as final, you cannot change the value of
final variable(It will be constant).
Java final method
• If you make any method as final, you cannot override it.
Java final class
• If you make any class as final, you cannot extend it.

30
this Keyword
• As you know, it is illegal in Java
to declare two local variables
with the same name inside the
same or enclosing scopes.
• Interestingly, you can have local
variables, including formal
parameters to methods, which
overlap with the names of the
class’ instance variables.
• However, when a local variable
has the same name as an instance
variable, the local variable hides
the instance variable
31

You might also like