Week 2 Slides
Week 2 Slides
Basic objects
1
OOP
• OOP stands for Object Oriented Programming
2
OOP
• OOP stands for Object Oriented Programming
3
OOP
• OOP stands for Object Oriented Programming
4
OOP
• In this course, we’re going to see the basic concepts
of objects and how they work in Java
5
OOP
• In this course, we’re going to see the basic concepts
of objects and how they work in Java
6
OOP
• In this course, we’re going to see the basic concepts
of objects and how they work in Java
System.out.println(“Hello!”);
}
}
8
Class means type
• When you create a class, you’re actually defining a new
type of data (i.e. an object)
9
Class means type
• When you create a class, you’re actually defining a new
type of data (i.e. an object)
• Like any other type, a class has:
• A name (the class name, which should begin with
an uppercase letter by convention)
10
Class means type
• When you create a class, you’re actually defining a new
type of data (i.e. an object)
• Like any other type, a class has:
• A name (the class name, which should begin with
an uppercase letter by convention)
• Some kind of data that is stored: a collection of
variables called instance variables (& class variables)
11
Class means type
• When you create a class, you’re actually defining a new
type of data (i.e. an object)
• Like any other type, a class has:
• A name (the class name, which should begin with
an uppercase letter by convention)
• Some kind of data that is stored: a collection of
variables called instance variables (& class variables)
• Some kind of actions that can be performed on the
data: a collection of methods called instance
methods (& class methods)
12
Use of objects
• Objects in programs are often used to represent real
life objects…
13
Class definition
• A class is defined in a .java file, with the same name
as the class
14
Class definition
• A class is defined in a .java file, with the same name
as the class
• Example:
15
Class definition
• A class is defined in a .java file, with the same name
as the class
• Example:
This is just the class definition,
i.e a specification / model /
public class Person{
template for your new type
public String name; it does not create anything in
public int age; memory.
} When you actually build an
instance of this class, you get
what we call an object.
16
Class definition
• A class is defined in a .java file, with the same name
as the class
17
Class definition
• A class is defined in a .java file, with the same name
as the class
• Example:
18
Class definition
• A class is defined in a .java file, with the same name
as the class
• Example:
19
Creating objects
• Once your class has been compiled, you use this new
type, in a main for example:
Person john;
Person jane;
20
Creating objects
• Once your class has been compiled, you use this new
type, in a main for example:
Person john;
Person jane;
21
Creating objects
• Once your class has been compiled, you use this new
type, in a main for example:
Person john;
Person jane;
Person john;
Person jane;
memory
john
null
null
jane
23
Creating objects
• To create an instance (an actual object), the basic
syntax is:
this calls the constructor!
Person john = new Person();
Person jane = new Person();
name null
jane age 0
24
Using the instance variables
• Given a reference to an object (e.g. john or jane
below), you can access the instance variables using
the syntax below, assuming that they are “public”
(but normally they shouldn’t be… more on that soon)
john.name = “John”;
jane.name = “Jane”;
john.age = 55;
jane.age = 42;
if (john.age > jane.age)
System.out.println(john.name + “ is older!”);
25
Instance methods
• Instance methods are methods that can only be used
on instances (instantiated objects)
26
Instance methods
• Instance methods are methods that can only be used
on instances (instantiated objects)
jane.haveBirthday();
john.haveBirthday();
int numLetters = jane.getNbLettersInName();
.
objectInstance instanceMethod();
29
Using an instance method
• You use an instance method by applying it as an
operation to one particular object instance:
jane.haveBirthday();
john.haveBirthday();
int numLetters = jane.getNbLettersInName();
.
objectInstance instanceMethod();
jane.haveBirthday();
31
Under the hood
• E.g. send the haveBirthday() message to the jane
object:
jane.haveBirthday();
32
Under the hood
• E.g. send the haveBirthday() message to the jane
object:
jane.haveBirthday();
33
Scope and the this keyword
• In an instance method, if you have a local variable
(e.g. a parameter) with the same name as an
instance variable the local one will be used
34
Scope and the this keyword
• In an instance method, if you have a local variable
(e.g. a parameter) with the same name as an
instance variable the local one will be used
35
Scope and the this keyword
• In an instance method, if you have a local variable
(e.g. a parameter) with the same name as an
instance variable the local one will be used
36
Scope and the this keyword
• Example:
37
Scope and the this keyword
• Example:
38
Scope and the this keyword
• Example 2:
39
Scope and the this keyword
• Example 2:
40
The toString() method
• toString() is a very useful instance method, which
you should always try to supply using this signature:
41
The toString() method
• toString() is a very useful instance method, which
you should always try to supply using this signature:
42
The toString() method
• When you define your toString() method for your
object, you control how your object will be displayed
as a String
43
The toString() method
• Once toString() is defined, you can now get readable
results from, for example:
44
The toString() method
• Note that there is always a toString() method if
you don’t supply one, Java uses a default one
45
Constructors
• A constructor is a special method that is used to
instantiate (create an instance of) an object
46
Constructors
• A constructor is a special case of an instance
method:
• no return type at all (not even void)
• it must have the exact same name as the class
47
Defining a constructor
• Syntax:
48
Defining a constructor
• Syntax:
public makes it accessible
outside of this class
49
Defining a constructor
• Syntax:
name of the constructor:
same as class name
50
Defining a constructor
• Syntax:
list of parameters, just
like a normal method
51
Defining a constructor
• Syntax:
52
Defining a constructor
• You can define multiple constructors, as long as they
have different signatures (lists of parameters)
public class Person{
public String name;
public int age;
public Person(){
name = “Newborn”;
age = 0; 2 different
} constructors
public Person(String name, int age){ with different
this.name = name; parameters
this.age = age;
}
}
53
Defining a constructor
• You can define multiple constructors, as long as they
have different signatures (lists of parameters)
public class Person{
public String name;
public int age;
public Person(){
name = “Newborn”;
age = 0;
}
Note: You could also
public Person(String n, int a){ use different
name = n; parameter names to
age = a; just avoid the
} conflicts, so that ‘this.’
} isn’t required
54
Constructing new objects
• If any constructors are supplied, the correct
parameters for one of them must be used when
creating an instance:
55
Constructing new objects
• When a constructor does not initialize the instance
variables, they just keep their default value
56
Default constructors
• A default constructor is provided by Java in case no
constructor is defined in a class
public NameOfTheClass() { }
57
Default constructors
• A default constructor is provided by Java in case no
constructor is defined in a class
public NameOfTheClass() { }
58
Default constructors
• Example:
//in Person.java file: //in Test.java file
public class Person { public class Test{
public String name; public static void main (String[] args) {
public int age; Person p = new Person();
} }
}
59
Default constructors
• Example:
//in Person.java file: //in Test.java file
public class Person { public class Test{
public String name; public static void main (String[] args) {
public int age; Person p = new Person();
} }
}
60
Default constructors
• Example 2:
//in Person.java file: //in Test.java file
public class Person { public class Test{
public String name; public static void main (String[] args) {
public int age; Person p = new Person();
}
public Person(String n, int i){
}
//statements here
}
}
61
Default constructors
• Example 2:
//in Person.java file: //in Test.java file
public class Person { public class Test{
public String name; public static void main (String[] args) {
public int age; Person p = new Person();
}
public Person(String n, int i){
}
//statements here
}
}
• public
• private
• protected
• package-private
63
Access modifiers
• Each variable or method in a class can have 4 different
access modifiers, which affect their
visibility/accessibility:
64
Access modifiers
• public: means any code, anywhere, can access or use it
65
Access modifiers
Rule of thumb:
66
Principle of encapsulation
• Encapsulation is one of the main features of object-
oriented programming
67
Principle of encapsulation
• Goal: protecting the internals, preventing other
classes from misusing the object
68
Principle of encapsulation
• Goal: protecting the internals, preventing other
classes from misusing the object
69
Principle of encapsulation
• If the instance variables are private, then we provide
“accessor” and “mutator” methods (get/set
methods) if needed:
70
Why not just public?
• Suppose you use: public String name;
71
Why not just public?
• That means: you need to find
and change 6,328 other
places in the system, in 219
other classes…
72
Why not just public?
• That means: you need to find
and change 6,328 other
places in the system, in 219
other classes…
73
Why not just public?
• That means: you need to find
and change 6,328 other
places in the system, in 219
other classes…
74
Why not just public?
• That means: you need to find
and change 6,328 other
places in the system, in 219
other classes…
75
Why not just public?
• That means: you need to find
and change 6,328 other
places in the system, in 219
other classes…
76
Why not just public?
• If it was private, you could just modify getName()
and setName() and that’s it
77
Class variables and methods
• You can create variables and methods which do not
refer to any one specific instance (e.g. one actual
Person object), but belong to the class as a whole
• We call those:
• class variables
• class methods
78
Class variables and methods
• Example:
public class Person{
//instance variables - one per object created
private String name;
private int age;
//class variable - only one for the whole class class variables are
private static int population = 0; initialized at the same
time they are declared
//class method - cannot be applied to an object
public static int census() { return population; }
81
Class variables and methods
Person class
• Example:
population 3
//Constructor could look like this:
public Person(String n, int a)
Person
{
name Fred
name = n;
age 21
age = a;
population++; Person
} name John
p1 age 40
In a main, somewhere else,
p2 Person
you create 3 objects of type
p3 name Mary
Person, and this is what you age 29
get in the memory:
82
Class variables and methods
• You can also have class constants just add final:
//class constant
public static final boolean NEEDS_TO_EAT = true;
}
83
Calling methods
• Instance (non-static) methods:
someObject.method(…)
84
Calling methods
• Example, trying to call instance and class methods of
Person from a main in another class:
john.haveBirthday();
85
Methods you have used
• System.out.println(...)
• System is a class (google "Java System class")
• out is a public static variable in that class
• Its type is PrintStream
• println is a public instance method in the
PrintStream class.
• You're sending a println message to the object
referred to by the static out variable in the
System class.
86
Methods you have used
• Math.sqrt(…) is a public static method in Math
87
Comparing objects
• Comparing Object variables using == or != is not
usually what you want to do
88
Comparing objects
• Standard methods for comparing the actual data
inside Objects:
89
Comparing objects
• Similarly to the toString() instance method, you
should write these methods for your own objects.
Other methods can use them.
90
Places to use objects
• You can use an object type anywhere you can use
any other type
• as a parameter to a method
• as the return type of a method
• as the elements of an array
• as an instance variable in another object
• etc. etc.
91
Places to use objects
• Just remember that it's always a reference to an
object that is passed/returned/stored/etc.
92