Professional Documents
Culture Documents
Delving Into Classes: CS F213: Object Oriented Programming
Delving Into Classes: CS F213: Object Oriented Programming
Dipanjan Chakraborty
Department of CS&IS
1
What are Classes and Objects?
●
Recap:
– Classes are templates for objects
– Objects are instances of a class
2
Generic form of a Class
class classname {
type instance-variable1;
type instance-variable2;
// ...
type instance-variableN;
type methodname1(parameter-list) {
// body of method
// ...
type methodnameN(parameter-list) {
// body of method
}
3
}
What are Classes and Objects?
●
Recap:
– Classes contain instance variables (can be
objects of other classes)
– Methods within the classes contain the code
– General rule: methods determine how a
class’ data can be used
4
Example Class
●
Let’s develop a class for boxes
●
What would be the data members?
5
The new keyword
●
The new operator dynamically allocates memory at runtime
– class-var = new classname();
●
classname() is called the constructor for the class.
– A constructor defines what happens when an object of a
class is created
– Classes can have explicitly defined constructors, in addition
Java supplies a default constructor
●
The new keyword is not required for primitive data types as
they are not implemented as objects
6
Object Reference Variables
● Box b1 = new Box();
Box b2 = b1;
●
No new space is allocated for b2. b2 and b1 refer to the
same object and any change in one will reflect in the
other. If b2 is set to null, b1 continues to refer to the
original object
7
Methods
● type name(parameter-list) {
// body of method
}
●
type: return type (must be specified) cant be var
●
name: any legal identifier not used in the current
scope
●
Parameters: variables received by the method 8
Methods
●
What methods would you add to the Box class?
●
How does the method invocation work?
●
Are methods replicated for each instance of a
class? methods are not replicated for each instance of a class; they are shared among all instances.
Each object (instance) of a class holds its own state (i.e., instance variables),
9
Constructors
●
A constructor contains the set of tasks to run when an object
is created
– e.g. initialisation
●
Has the same names as the declaring class
●
Automatically called before the new operator completes
●
Syntactically similar to a method but have some differences
– No return type, are not implicitly inherited (more on inheritance
later)
– Implicit return type is the class type itself
10
Constructors
●
Modify the Box class to use a constructor to set
the dimensions
●
Use parameterised constructors
If you define any constructor explicitly in a Java class, the default constructor provided by Java is no longer automatically generated.
box()
{
11
The this keyword
● this can be used inside any method to refer
to the current object
●
Modify the Box constructor to use the this
keyword
● How can the this keyword be used to
overcome instance variable hiding?
12
Garbage Collection
●
Java does deallocation of memory after objects are
destroyed, automatically
●
The technique is called Garbage Collection
●
When no references to an object exist then it is
assumed to be no longer needed and the memory
can be reclaimed
●
Occurs sporadically, defined by the Java run-time
implementation 13
Example Class: Stack
●
What are the properties of a stack?
●
Design a Stack class and implement the
different properties
14