Chapter Two - Class and Object
Chapter Two - Class and Object
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 −
3
do...while loop
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.
6
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
• 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.
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.
• 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.
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.
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.
• 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.
• 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
28
Static Method
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