Vous êtes sur la page 1sur 14

CLASSES & OBJECTS

by: maria cristina b. nazareno

created by: ma. cristina b. nazareno

2/12/16

INTRODUCTION
Java is an Object-Oriented Language. As a language that has the
Object Oriented feature, Java supports the following fundamental
concepts:
Polymorphism
Inheritance
Encapsulation
Abstraction
Classes
Objects
created by: ma. cristina b. nazareno

Instance

2/12/16

WHAT ARE OBJECTS?


What are objects and what are not objects?
Objects: bicycle, book, lamp, song, meeting
Non-objects: green, 30% of all pencils, large

If you can touch it, name it, or talk about it, it is likely
to be an object.
Objects can be physical things or conceptual.
Humans seem to want to think in terms of objects and
their relationships with each other.

In Java, an object is a specific, concrete


instance of a class.
created by: ma. cristina b. nazareno

2/12/16

WHAT IS A CLASS?
A class can be defined as a template/blue
print that describes the behaviors/states that
object of its type support.

created by: ma. cristina b. nazareno

2/12/16

CLASS DEFINITION
A Java class definition contains
Fields - what properties an object has
The values assigned to the fields define the state of
the object. (e.g., the car is painted silver, has a half
a tank of gas, and is stopped.)

Methods - what behaviors (actions) an object


can perform
Typically these actions supply or modify its state.
created by: ma. cristina b. nazareno

2/12/16

CLASS DEFINITION
You define a class by using the class
keyword along with the class name, like
this:
class MyClass
{
}
created by: ma. cristina b. nazareno

2/12/16

CLASS DEFINITION
To create an object from a class, you type the class's
name followed by the name of the object. For
example, the line below creates an object from the
MyClass class:
Name of an
object

Automatically calls
the constructor

MyClass myObject = new MyClass();


Class
Name

created by: ma. cristina b. nazareno

Dynamically create
object using new

2/12/16

DECLARING MEMBER VARIABLES


A class can contain any of the following variable types.
LOCAL VARIABLES: Variables defined inside methods,
constructors or blocks are called local variables. The variable will
be declared and initialized within the method and the variable
will be destroyed when the method has completed.
INSTANCE VARIABLES: Instance variables are variables within
a class but outside any method. These variables are initialized
when the class is instantiated. Instance variables can be
accessed from inside any method, constructor or blocks of that
particular class.
CLASS VARIABLES: Class variables are variables declared with
in a class, outside any method, with the static keyword.
PARAMETER VARIABLES: A parameter variable is used to store
information that is being passed from the location of the method
2/12/16
created by: ma. cristina b. nazareno
call into the method that is called.

ASSIGNMENT

What is/are the differences between static and non-static variables?

created by: ma. cristina b. nazareno

2/12/16

INSTANCE METHODS VS STATIC


10
STATIC METHODS
NON-STATIC (INSTANCE) METHODS
METHODS
declared with the keyword static
- are declared without the keyword static
When you use a static field or method, you do not use an object
Static methods in a class are called class methods. When you create a class with a static field and instantiate 100 objects, only one copy of that field
exists in memory
When you create a static method in a class and instantiate 100 objects, only one copy of the
method exists in memory and the method does
not receive a this reference.
Static class variables are not instance variables. The system allocates memory to hold class
variables once per class, no matter how many
instances of the class you instantiate. The system
allocates memory for class variables the first time
it encounters a class, and every instance of a class
created by: ma. cristina b. nazareno
shares the same copy of any static class variables.

When you use a nonstatic field or method, you


must use an object
Nonstatic methods in a class are called instance
methods.
When you create a class with a nonstatic field and
instantiate 100 objects, then 100 copies of that
field exist in memory.
When you create a nonstatic method in a class
and instantiate 100 objects, only one copy of the
method exists in memory, but the method
receives a this reference that contains the address
of the object currently using it.
Instance fields and methods are nonstatic. The
system allocates a separate memory location for
each nonstatic field in each instance.

2/12/16

11

ACCESSORS & CONSTRUCTORS


A.ACCESSORS
To achieve encapsulation in Java
Declare the variables of a class as private.
Provide public setter and getter methods (this is also known as
accessors or mutators) to modify and view the variables
values.

created by: ma. cristina b. nazareno

2/12/16

12

Example: Instance Class


public void setAge( int newAge){

/* File name : EncapTest.java */


public class EncapTest{

age = newAge;

private String name;

private String idNum;


private int age;

public void setName(String newName){


name = newName;
}

public int getAge(){


return age;
}

public void setIdNum( String newId){


idNum = newId;

public String getName(){


return name;
}

}
}

public String getIdNum(){


return idNum;
}

created by: ma. cristina b. nazareno

2/12/16

13

Example: Application Class


/* File name : RunEncap.java */
public class RunEncap{
public static void main(String args[]){
EncapTest encap = new EncapTest();
encap.setName("James");
encap.setAge(20);
encap.setIdNum("12343ms");
System.out.print("Name : " + encap.getName() + " Age : " + encap.getAge());
}
}
Name : James Age : 20

created by: ma. cristina b. nazareno

2/12/16

14

Benefits of Encapsulation
The fields of a class can be made read-only or write-only.
A class can have total control over what is stored in its fields.
The users of a class do not know how the class stores its data. A
class can change the data type of a field and users of the class
do not need to change any of their code.

created by: ma. cristina b. nazareno

2/12/16

Vous aimerez peut-être aussi