You are on page 1of 22

GIN222

Applied programming for engineers


Classes and Objects
• Defining a class is to build a structured data
type.
• To define a type, just write a class, which, by
definition, consists of data and methods.
Classes and Objects
• The construction of a class is performed according to the following two
principles:
– Definition of data with variable declaration statements and / or objects. These
variables are primitive, as we used to (int, char, etc.) or objects predefined or
not (String, etc.). These data describe the characteristic information of the
object. They are also commonly called field or attributes.
– Construction methods defined by the programmer. These are the methods to
manipulate the data. They are built as simple functions, composed a header
and instructions. These methods are all treatments and behaviors of the
object to be described.

• By defining new types, we determine the specific characteristics of objects


that are desired to be programmed. An object type corresponds to all data
processed by the program, grouped by theme.
Classes and Objects
• After defining a new structured type, the next
step is to write an application that actually uses
an "object" of this type.
• For this, the programmer must declare useful
objects to the application and to ensure that the
necessary memory is reserved.
• To declare an object you need a statement with
the difference that the type of the variable is no
longer simply a predefined type but a structured
type, as built previously in the class.
Classes and Objects
• Example:
Classes and Objects
• The instruction
• Cercle A = new Cercle() ;
• This statement creates a memory box, to contain a reference
to the address where the information about the circle A is.
• The new operator is a Java program that manages itself
memory space reservation. When applying this operator to an
object, it determines how many bytes are required to store
the information in the class.
• A is now called an instance of the object.
• The now defined object is thus entirely determined by its data
and methods. It is therefore possible to change the values
that characterize and exploit methods.
Classes and Objects
• To access a particular class in order to change it, just write :
• A.x = a suitable value for the variable.
• Same operation applies to the methods as long as they are
correctly called with suitable parameters and return values.
• The memory spaces of the instance variables and methods
are accessible using the (.) dot notation.
• Pay attention that such access from the instance of the object
calling from another class is feasible since the class variables
and the methods are declared public.
• Had they been declared private the only permissible access is
from with the class only.
Classes and Objects
• Consider the example where the variable is private :
Classes and Objects
• A class’s private fields can be manipulated only by methods of
that class. So a client of an object—that is, any class that calls
the object’s methods—calls the class’s public methods to
manipulate the private fields of an object of the class.
• This is why the statements in method main call the
setCourseName, getCourseName and displayMessage
methods on a GradeBook object.
• Classes often provide public methods to allow clients of the
class to set (i.e., assign values to) or get (i.e., obtain the values
of) private instance variables.
• The names of these methods need not begin with set or get,
but this naming convention is highly recommended in Java.
Classes and Objects
• Calling from a main method of a client class :
Classes and Objects
• Declaring instance variables with access modifier
private is known as data hiding.
• When a program creates (instantiates) an object
of class GradeBook, variable courseName is
encapsulated (hidden) in the object and can be
accessed only by methods of the object’s class.
• In class GradeBook, methods setCourseName and
getCourseName manipulate the instance variable
courseName.
Classes and Objects
• Encapsulation is also known as “data hiding”.
• Objects encapsulate data and implementation details.
To the outside world, an object is a black box that
exhibits a certain behavior.
• The behavior of this object is what which is useful for
the external world or other objects.
• An object exposes its behavior by means of public
methods or functions.
• The set of functions an object exposes to other objects
or external world acts as the interface of the object.
Classes and Objects
• A class usually contains at least a method carrying its name.
• Such a method is called constructor.
• By default, the compiler provides a default constructor
with no parameters in any class that does not explicitly
include a constructor.
• When a class has only the default constructor, its instance
variables are initialized to their default values.
• Variables of types char, byte, short, int, long, float and
double are initialized to 0, variables of type boolean are
initialized to false, and reference-type variables are
initialized to null.
Classes and Objects
• Example:
Classes and Objects
• Calling from the main method of a client class:
Classes and Objects
• Every object can access a reference to itself with
keyword this (sometimes called the this
reference).
• When a non-static method is called for a
particular object, the method’s body implicitly
uses keyword this to refer to the object’s instance
variables and other methods.
• You can also use keyword this explicitly in a non-
static method’s body.
• Keyword this cannot be used in a static method.
Classes and Objects
• Example using this:
Classes and Objects
Classes and Objects
• All instance variables (object data) and methods
(object behavior) are created without static keyword
• Note: There is no “dynamic” keyword in Java.
• Dynamic by default. In general, dynamic refers to
things created at “run time” i.e. when the program is
running.
• Every object gets its own (dynamic) instance variables.
• Every object effectively gets its own copy of each
dynamic method (i.e. the instructions in the method).
Classes and Objects
• Static means “pertaining to the class in
general”, not to an individual object.
• Variable is declared with the static keyword
outside all methods.
• A static variable is shared by all instances (if
any).
• All instances may be able read/write it.
Classes and Objects
• A method may be declared with the static
keyword.
• Static methods live at class level, not at object
level.
• Static methods can access static variables and
other methods, but not dynamic ones.
Classes and Objects
• static variables and methods belong to the class
in general, not to individual objects.
• The absence of the keyword static before non-
local variables and methods means dynamic (one
per object/instance).
• A dynamic method can access all dynamic and
static variables and methods in the same class.
• A static method can not access a dynamic
variable.
• A static method can not call a dynamic method.

You might also like