Vous êtes sur la page 1sur 17

OBJECTS AND

CLASSES
Object Oriented Programming

Objects and Classes


2

Classification of Objects

Physical
objects

which can be seen or touched

Conceptual
objects

which cant be touched but they exists

Objects and Classes

Objects and Classes


3

Relationship between an object and


a class

Object is -- an an - instance instance - of


Class
Eg.

Juan

Dela Cruz is an instance of Person

Objects and Classes

Variables
4

Kinds of Variables

Fields - member variables of a class


Local Variables variables declared in a
method or block of code
Parameters variables used in method

Objects and Classes

Declaring Member Variables


5

Coding guidelines when declaring


member variables
1.
2.
3.
4.
5.

Declare all instance variables on the top of the


class declaration.
Declare one variable for each line.
Instance variables should start with small
letter.
Use an appropriate data type for each declared
variable.
Declare instance variables as private so that
only class methods can access them directly.
Objects and Classes

Declaring Member Variables


6

The static keyword is used to


indicate that a variable is a static
variable.

Objects and Classes

Access Modifiers
7

Public

specifies that class members are accessible to anyone,


both inside and outside the class

Private

specifies that the class members are only accessible


by the class they are defined in

Protected

specifies that the class members are accessible only to


methods in that class and the subclasses of the class

Default

specifies that only classes in the same package can


have access to the class variables and methods
Objects and Classes

Declaring Methods
8

A method is a self-contained block of


program code that is similar to a
procedure.
A method is a sequence of instructions
that a class or an object follows to
perform a task.
To execute a method, you invoke or call
it from another method.

Advanced Object-Oriented Concepts (Part 1)

Declaring Methods
9

Naming a Method

Method names should be a verb in


lowercase or a multi-word name that begins
with a verb in lowercase, followed by
adjectives, noun, etc.
In multi-word names, the first letter of
each of the second and following words
should be capitalized.

Advanced Object-Oriented Concepts (Part 1)

Methods that Return Values


10

A methods return type can be primitive


types like int, double, char, etc., as well
as class types.
A methods return type is declared in its
method declaration. Within the body of
the method, the return statement is
used to return the value.
A method that does not return a value is
declared void.
Advanced Object-Oriented Concepts (Part 1)

Methods
11

Accessor Methods

method that is used to read values from


member fields (instance/static) returns the
value of the field

Mutator Methods

method that is used to write or change the


value of member fields (instance/static)

Advanced Object-Oriented Concepts (Part 1)

Method Overloading
12

object-oriented technique that lets you


define several different versions of a
method, all with the same name but
each with a different argument or
parameter list

Advanced Object-Oriented Concepts (Part 1)

Constructors
13

special type of method that establishes


or creates a new object
have the same name as their class and
have no return value in their declaration
can only be called by using the new
operator during class instantiation

Advanced Object-Oriented Concepts (Part 1)

Constructors
14

Default Constructor

the constructor without any arguments or


parameters

created automatically by the Java


compiler for any class created whenever
you do not write your own constructor

Advanced Object-Oriented Concepts (Part 1)

Constructors
15

Constructors are used after you have


instantiated an object.
The new keyword is the key to
instantiating an object.

Advanced Object-Oriented Concepts (Part 1)

The this Keyword


16

keyword in Java that can be used in


instance methods (or a constructor) to
refer to the object on which the currently
executing method has been invoked
used to access the instance variables
shadowed or obscured by the
parameters
this.<nameOfField>

Advanced Object-Oriented Concepts (Part 1)

Packages
17

named collection of classes Javas means


of grouping related classes and
interfaces together in a single unit
by default, all Java programs import the :

java.lang.* package

Advanced Object-Oriented Concepts (Part 1)

Vous aimerez peut-être aussi