Chapter - 2 Class and Objects Updated
Chapter - 2 Class and Objects Updated
1
outline
o Introduction to Classes and Objects
o Defining a class
o Creating an Object
o Instantiating and using objects
2
Introduction to Classes and Objects
Defining Classes and Objects
A class is a blueprint used to create objects.
It defines the (data) and methods (functions) that are common to all
objects of a particular type.
Object-Oriented Programming (OOP) is a programming approach
based on using objects.
An object represents a real-world entity that can be uniquely identified.
The state of an object (also called properties or attributes) is
represented by data fields and their current values.
The behavior of an object (also called actions) is defined by its
methods.
To invoke (call) a method means to ask an object to perform a
3
specific action.
Methods and Classes
Performing a task in a program requires a method.
The method houses the program statements that actually perform
its tasks.
The method hides these statements from its user
In Java, we create a program unit called a class to house the set of
methods that perform the class’s tasks.
4
Method
Methods also known as functions or procedures.
Methods in Java are used to define the behavior of an object.
They can accept parameters and return values.
Hiding the internal workings of a method from a user but
providing the correct answer is known as abstraction
5
Declaring Methods
A method definition consists of 4 parts: the return type, the name, the
parameters, and the body:
The syntax for defining a method is as follows:
modifier returnValueType methodName(list of parameters)
{
// method body;
}
6
Cont..
The method header specifies the modifiers, return type, method
name, and parameters of the method.
A method may return a value.
The return type of a method may be any data type of the value
the method returns.
Some methods perform desired operations without returning a
value.
In this case, the return type is the keyword void.
If a method returns a value, it is called a value-returning
method; otherwise, it is called a void method.
7
Cont…
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 is what you write inside the parentheses () of a
method.
It includes type of parameters, order of parameters, number of
parameters
Note: Parameters are optional; that is, a method may contain no
parameters.
8
Cont…
Similarly, a method can require one or more parameters that represent
additional information it needs to perform its task.
The parameter list may contain any number of parameters separated
by commas, including none at all.
double mark(double test1, double test2)
Each parameter must specify a type and a variable name.
Empty parentheses following the method name indicate that a method
does not require any parameters ( additional information)
Example: double mark()
parameters may be of different types.
int mark(double test1, int test2)
9
The Method Body
The body of a method is a block specified by curly brackets. The body defines
the actions of the method.
The method arguments can be used anywhere inside of the body.
All methods must have curly brackets to specify the body even if the body
contains only one or no statement.
10
Invoking Methods
To call a method, specify the name of the method followed by a list of comma
separated arguments in parentheses as follow:
methodName(agument1,…,argumentN);
If the method has no arguments, you still need to follow the method name with
empty parentheses as follow:
methodName();
11
Static method and Instance method
Some methods and member variables may have the keyword static.
Static methods are the methods in Java that can be called without creating an
object of class. They are referenced by the class name itself or reference to the
Object of that class.
Static members belong to the class instead of a specific instance.
Instance method are methods which require an object of its class to be created before it
can be called.
To invoke a instance method, we have to create an Object of the class in which the method
is defined.
Example:
12
Constructing Objects Using
A Constructors
constructor is invoked to create an object using the new
keyword.
Constructors are a special kind of method.
They have three Special characteristics:
A constructor must have the same name as the class itself.
Constructors do not have a return type—not even void.
Constructors are invoked using the new keyword when an
object is created. Constructors play the role of initializing
objects.
13
Cont…
Creating an object in java involves using the new keyword
followed by a constructor of the class.
The process of doing this is called instantiation. [Creating an
instance is referred to as instantiation.]
An object is then referred to as an instance of its class.
Example:
Student student1=new Student();
Student student2=new Student();
[Link]=“John”;
student2.test1=88.9;
14
Java Access Modifiers
In java, access modifiers are used to set the accessibility of classes,
interfaces, variables, methods, constructors, data members and the
setter methods.
There are four access modifiers in java
Default: visible only within the package(access only to classes in
the same package)
Private: access only to the class itself.
Protected: access to classes in the same package and to all
subclasses.
Public: access to all classes everywhere.
A visibility modifier specifies how data fields and methods in a class
can be accessed from outside the class.
15
Cont..
16
Cont….
17
----------The End-----------
18