You are on page 1of 32

1

CS 241: OBJECT
ORIENTED
PROGRAMMING
Dr. Ibrahim Eldesoky
Ibrahim_desoky@hotmail.com
2

Defining Classes

• We have already seen some built-in types like String,


StringBuffer, Scanner,...

• This time we are going to introduce the basics of


designing and implementing our own object types.
3

Motivation

• A common motivation for creating a new Object


type, is to take several related pieces of data
along with operations to be done on these data,
encapsulate them into an object that can be
manipulated (passed as an argument, operated
on) as a single unit.

• So we will introduce the following concepts:


Encapsulation, Information hiding.
4

Classes - Quick Revision (from


lecture1)
• A class is a blueprint or template of its objects.
• We create a new type every time we define a new class

• We can create many objects from a single class.

• Creating an object from a class is called instantiation,


and an object is an instance of a particular class.

• Normally, an object is created from a class using the


new operator.
5

Class Declaration

//This declaration for the scope of our course

import statement;

public class ClassName


{
Class Members Declarations
}
6

Class Accessibility Modifiers

There are three class accessibility modifiers:


(public , private, package)

 public
A public class is accessible by any class.

Note: In .java file you can define only one public


class and should has the name of the .java file.
7

Class Members

• Inside of a class, we may declare the following class


members:
• Fields – data variables.

• Constructors – special methods to create objects of the


class, and to initialize fields.
• Methods.

• Inner Classes -- classes nested in the class.


8

Class Members (fields and methods) Accessibility Modifiers

• There are four accessibility modifiers for class members:


public
A public member is accessible by any class.

private
A private member is accessible only by the class itself.

( no modifier is presented), the member is accessible by all


the classes within the same package. This accessibility is
known as package accessibility modifier (out of the scope
of our course).

protected
We will talk about it later
9

Class Member Declarations

• The order of the declarations is not important, but following this


convention will make your program easier to read.

public class ClassName {


Fields
Constructors
Methods
}
10

Example – Time Class

• we will implement a class Time, which is used to record the


time.
• The pieces of information that form a time are: hour, minute and
second.

• Because every Time object will contain these data, we need to


create instance variables to hold them.

• The first step is to declare the instance variables.


11

Example – Time Class

• Instance variables are declared at the beginning of the


class definition, outside of any method definition, like this:

class Time {

private int hour, minute, second;

• This code segment is a legal class definition.

• After declaring the instance variables, the next step is


usually to define a constructor for the new class.
12

Constructor methods

• The usual role of a constructor is to initialize the instance


variables.
• When a new operator is executed the system allocates a
memory for a new object and the constructor method of the
class is invoked to initialize the instance variables of that object.

• The syntax for constructors is similar to that of other


methods, with three exceptions:
• The constructor method has same name as the class
• It does not have any return type.
• The keyword static isn't used.
13

Constructor methods
• Here is a constructor for the Time class:

public Time () {

hour = 0;
minute = 0;
second = 0;
}
• This constructor does not take any arguments, as indicated by
the empty parentheses ().

• Each line of the constructor initializes an instance variable to


an arbitrary default value (in this case, 0).
14

Constructor methods
• Here is another form to the constructor. It has parameter list that is
identical to the list of the instance variables.

public Time (int hr, int mn, int sc) {


hour = hr;
minute = mn;
second = sc;
}

• The constructor copies the information from the parameters to


the instance variables.
15

Constructor methods
• Can the Constructor be overloaded like other
methods? which means that you can provide multiple
constructors with different parameters.
• Yes, it can.

• It is very common to have one constructor that takes no


arguments, and one constructor that has a parameter list that is
identical to the list of instance variables.

• Java knows which constructor to invoke by matching the


arguments of the new command with the parameters of the
constructors.
• Ex: new Time (); new Time (2,3,4);
16

Constructor methods

• Remember:
• Although constructors look like methods, you never invoke them
directly, you have to use new operator.

• If you didn’t define any constructor methods the default one is


invoked.
• The default constructor is a constructor that doesn’t take any
parameters.
• If you defined a constructor that has parameters list, you will not
be able to invoke the default one without defining it by your own.
17

Time class - Example


• Here is our Time class up to now:

class Time {

private int hour, minute, second;


public Time () {
hour = 0;
minute = 0;
second = 0.0;
}
public Time (int hr, int mn, int sc) {
hour = hr;
minute = mn;
second = sc;
}
}

• How and where can we instantiate objects from this class?


18

Instantiating objects

• How and where can we instantiate objects from this class?

• How : using the new operator


• Where : We can instantiate an object either
• inside the class itself, or
• inside another class.

Let’s consider the first possibility, so we are going to declare a


method main inside our Time class.
19
// how to instantiate Time objects
class Time {

private int hour, minute, second;


public Time () {
hour = 0;
minute = 0;
second = 0;
}
public Time (int hr, int mn, int sc) {
hour = hr;
minute = mn;
second = sc;
}
public static void main (String[] args) {

Time t1 = new Time ();


Time t2 = new Time (11, 8, 3);
}}
20

// Let’s focus on the main method

public static void main (String[] args) {

// one way to create and initialize a Time object


Time t1 = new Time ();

// another way to do the same thing


Time t2 = new Time (11, 8, 3);
}
21

Let’s take a look on the memory


Time t1 = new Time ();

hour 0
t1
minute 0

second 0

Time t2 = new Time (11,8,3);

hour 11
t2
minute 8

second 3
22
// how to instantiate Time objects
class Time {

private int hour, minute, second;


public Time () {
hour = 0;
minute = 0;
second = 0;
}
public Time (int hr, int mn, int sc) {
hour = hr;
minute = mn;
second = sc;
}
public static void main (String[] args) {

Time t1 = new Time ();

t1.hour=11; t1.minute=8;t1.second=3;

Time t2 = new Time (11, 8, 3);


}}
23

Let’s take a look on the memory


Time t1 = new Time ();

hour 0
t1
minute 0

second 0

t1.hour=11; t1.minute=8; t1.second=3;

hour 11
t1
minute 8

second 3
24

• Creating an object from class Time inside another


class

class TimeCall {

public static void main (String[] args) {

Time t1 = new Time ();

Time t2 = new Time (11, 8, 3);

}
}
25

class TimeCall {

public static void main (String[] args) {

Time t1 = new Time ();


// if you tried the following statement you would get
a message “undefined parameter”
t1.hour =5
}}
26

We define mutators and accessors methods inside the


class to be used in accessing its fields from outside

class Time {

private int hour, minute, second;


public Time () {
hour = 0; minute = 0; second = 0; }
public Time (int hr, int mn, int sc) {
hour = hr; minute = mn; second = sc; }

public void setHour (int h){ //mutator


hour=h};

public int getHour () { //accessor


return hour; } }
27

class TimeCall {

public static void main (String[] args) {

Time t1 = new Time ();

t1.setHour(10);

System.out.println (t1.getHour());
}}
28

Printing objects

public static void main (String[] args) {

Time t1 = new Time ();


System.out.println (t1);

Time t2 = new Time (11, 8, 3);


System.out.println (t2);
}}

• What are the Results of the println() methods ?


29

• The Results of the two println() methods are :


Time@80cc7c0
Time@80cc807
• The name of the class appended by @ and a hexadecimal hash code
refers to the object.

• When you print an object using print or println, Java


checks to see whether you have provided an object
method named toString(),
• if so it invokes it.
• If not, it invokes a default version of toString() that produces the output
described
30
Let’s define our toString() method:

• It’s instance public method, its return type is String.

public String toString () {

return “Time :”+ hour+” ”+ minute + “ ” + second;}

• you can invoke this method from the main by using print or
println() method as follows:
System.out.print (t1);
 Time: hour minute second

• Or you can invoke it directly on an object as follows:


String s = t1.toString();
31

// Class time up to now


public class Time {
private int hour, minute, second;
public Time () {
hour = 0; minute = 0; second = 0; }
public Time (int hr, int mn, int sc) {
hour = hr; minute = mn; second = sc; }

public void setHour (int h){ //mutator for hour


hour=h};
public int getHour () { //accessor for hour
return hour};
// add accessors & mutators for minute & second too.

public String toString () {


return “Time :”+ hour+” ”+ minute + “ ” + second;}}
32

public Class TimeCall


{
public static void main (String[] args) {

Time t1 = new Time ();


System.out.println (t1);
t1. setHour(5);
System.out.println (t1);
Time t2 = new Time (11, 8, 3);
System.out.println (t2);
}
}

• What are the Results of the println() methods ?

You might also like