SlideShare une entreprise Scribd logo
1  sur  122
Télécharger pour lire hors ligne
Dr. Martin Chapman
programming@kcl.ac.uk
martinchapman.co.uk/teaching
Programming Practice and Applications (4CCS1PPA)
Topic 4: Why Object-Orientation?
Q: What do you think constitutes a real world object? 1
Thursday 13th October
To understand why object-orientation is an important
paradigm for code development.
WHY OBJECT-ORIENTATION: OBJECTIVES
2
Reason #1: Code Organisation (Review)
3
🙈
public class Driver {
public static void main(String[] args) {
MartinPrinter copyOfMartinPrinter
}
}
REMEMBER: MAKING A COPY OF A CLASS
4
new MartinPrinter();=
How do we do this?
We need to request a new copy of the class. We use the labelled name of
the class so that Java is able to match the class we want.
Then we need to put this copy inside the variable, using an assignment,
in the same way that we put values inside variables.
We will call a variable that contains a copy of a class an object.
😴
public class MartinPrinter {
public static void printMartin() {
System.out.println("+------+");
System.out.println("|Martin|");
System.out.println("+------+");
}
}
public class NumberPrinter {
public static void printNumber(int num) {
System.out.println("+------+");
System.out.println("|" + num + "|");
System.out.println("+------+");
Methods have to be static if they are called from a method
that is also static.
REMEMBER: STATIC METHODS AND OBJECTS
5
Because these
methods are now
being called
through an object
we can drop the
static keyword.
Remember ? The native output
from this statement isn’t particularly clear (it’s just a
number).
Make a class called with a method called
_________. In this method we should print "The Unix time is:
" followed by the (Unix) time.
You should then make a class, which makes an
object of the class, and uses this object to call
the method.
LECTURE EXERCISE: PRETTY TIME (1)
6
System.currentTimeMillis()
PrettyTime
printTime
Driver
PrettyTime
printTime
LECTURE EXERCISE: PRETTY TIME (2)
7
public class PrettyTime {
public void printTime() {
long currentTime = System.currentTimeMillis();
System.out.println("The Unix time is: " + currentTime);
}
}
public class Driver {
public static void main(String[] args) {
PrettyTime prettyTime = new PrettyTime();
prettyTime.printTime();
}
}
printTime here is not static, and
the methods in your coursework
should not be either.
We could leave this
methods static, and
reference it using the
class, but as we
haven’t yet fully
explored the
implications of doing
this, we will not.
LECTURE EXERCISE: PRETTY TIME (3)
8
Often, when first learning to program, people do things in a fairly
verbose way.
This is fine, as its important to start somewhere.
But often, when reviewing code, we can find small enhancements to
make.
For example, these two lines:
ASIDE: MAKING THINGS MORE EFFICIENT (1)
9
Could be reduced to this single line:
long currentTime = System.currentTimeMillis();
System.out.println("The Unix time is: " + currentTime);
System.out.println("The Unix time is: " + System.currentTimeMillis());
ASIDE: MAKING THINGS MORE EFFICIENT (2)
10
Investigating your code and trying to reduce the number of lines you
have used is an important skill to learn.
• Less lines are neater and easier to read
• Code is often more efficient. In this case, for example, we’re not
using memory to store the time unnecessarily (although
remember we won’t concern ourselves with memory too much).
In the laboratories, when you are copying the code in these slides in
order to try it out, check if anything can be done more efficiently.
• Have I done certain things inefficiently? Probably! Catch me out,
and let me know.
• Tackling verbosity does not always necessitate the introduce of
new syntax.
Classes and objects provide us with a way to organise our
code (Topic 3).
WHY OBJECT-ORIENTATION: SO FAR WE KNOW…
11
Object-oriented purists would be angry that I’m only selling
classes (and their associated objects) as a way to organise
code, because the idea is much more powerful.
• But I think this is a good initial way to understand
things.
• We will gradually see more important reasons for using
classes and objects going forward.
If any of the further information on objects and classes
confuses you, just come back to this idea that an object is
just a copy of the code in a class.
REMEMBER: CLASSES AND OBJECTS
12
Reason #2: Reusability
13
In the previous exercise, we were again able to use objects and
classes to organise our code nicely, such that methods like
_________ can be called along with methods from other classes,
while still retaining a separation of functionality (as we did in Topic
3).
REUSABILITY (1)
14
printTime
public class Driver {
public static void main(String[] args) {
MartinPrinter copyOfMartinPrinter = new MartinPrinter();
copyOfPrintMartin.printMartin();
NumberPrinter copyOfNumberPrinter = new NumberPrinter();
copyOfNumberPrinter.printNumber(191);
PrettyTime prettyTime = new PrettyTime();
prettyTime.printTime();
}
}
REUSABILITY (2)
15
public class Driver1 {
public static void main(String[] args) {
PrettyTime prettyTime = new PrettyTime();
prettyTime.printTime();
}
}
public class Driver2 {
public static void main(String[] args) {
PrettyTime prettyTime = new PrettyTime();
prettyTime.printTime();
}
}
public class NumberPrinter {
public void printNumber(int num) {
System.out.println("+------+");
System.out.println("|" + num + "|");
System.out.println("+------+");
PrettyTime prettyTime = new PrettyTime();
prettyTime.printTime();
}
}
The implicit benefit of this is reusability. We could call
in one program, and then use it in another program, simply by
making an object of the class again.
printTime
And of course, we can do the same thing inside any of our classes,
even those without a main method.
In increasing order of importance (to keep the purists
happy):
Classes and objects provide us with a way to organise our
code (Topic 3).
WHY OBJECT-ORIENTATION: SO FAR WE KNOW…
16
Classes and objects provide us with a way in which to reuse
our code.
😴
public class MartinPrinter {
public static void main(String[] args) {
long currentTime = System.currentTimeMillis();
System.out.println("+------+");
System.out.println("|Martin|");
System.out.println("+------+");
System.out.println(System.currentTimeMillis() - currentTime);
}
}
REMEMBER RUNNING TIME?
17
A way to calculate how long it takes for a piece of code to execute.
Can we wrap this code in a class in order to make this functionality
reusable across different programs? Is it as simple as before?
DESIRED DRIVER…
18
public class Driver {
public static void main(String[] args) {
RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator();
copyOfRunningTimeCalculator.recordCurrentTime();
System.out.println("+------+");
System.out.println("|Martin|");
System.out.println("+------+");
copyOfRunningTimeCalculator.printRunningTime();
}
}
Sometimes it’s helpful to look at what we want the end product to look
like when creating a class:
In this case, this is the functionality that should be provided to us by our
reusable running time calculator.
This is the first time
we’ve seen the calling
of multiple methods in
practice, but hopefully
it is intuitive that this
can be done.
So far we have only seen variable declarations inside methods.
So we might approach writing our reusable running time calculator as
follows:
THE LOCATION OF VARIABLES INSIDE CLASSES (1)
19
Because these variables are close to the method, so to speak, we call
them local variables.
public class RunningTimeCalculator {
public void recordCurrentTime() {
long currentTime = System.currentTimeMillis();
}
}
We record the current time. We then
want to reference this value at a later
point in time, via a different method
call.
public class RunningTimeCalculator {
public void recordCurrentTime() {
long currentTime = System.currentTimeMillis();
}
public void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
}
}
Local variables can only be referenced inside the method in which they
are declared.
LOCAL VARIABLES: VISIBILITY
20
But we cannot reference it
within this one.
So we can reference the current
time variable within this
method.
Therefore, local variables are typically used to store temporary values not
values that need to be used again, like in this example.
(This code will not compile.)
The formal way to refer to the visibility of a variable is that
variable’s scope. When a variable is in scope it is in our
sight and visible.
ASIDE: SCOPE
21
As a local variable can only be referenced within a method, it is only associated
with that method.
Therefore, more fundamentally, local variables only exist while the method in
which they are defined is being executed.
LOCAL VARIABLES: LIFETIME
22
public class RunningTimeCalculator {
public void recordCurrentTime() {
long currentTime = System.currentTimeMillis();
}
public void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
}
}
Values assigned to this variable will
disappear after the method finishes
executing.
What can we do?
We can, if we want to, declare this variable inside the class itself.
THE LOCATION OF VARIABLES INSIDE CLASSES (2)
23
We call these variables fields (they correspond, somewhat, to global
variables).
public class RunningTimeCalculator {
private long currentTime;
public void recordCurrentTime() {
currentTime = System.currentTimeMillis();
}
}
Why would we do this?
By default, we keep fields
private to the class. We will
return to this idea.
public class RunningTimeCalculator {
private long currentTime;
public void recordCurrentTime() {
currentTime = System.currentTimeMillis();
}
public void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
}
}
Fields can be referenced anywhere inside a class.
So, if we declare a current time field, we can still assign that field when
we set the current time, and then also reference it in order to print the
running time.
FIELDS: VISIBILITY
24
Indentation helps to confirm field
visibility.
As with method calls, where a field appears in
a class does not affect its visibility. The current
time field could appear below the print time
method, for example.
🤓
As fields can be referenced anywhere within a class, they are associated with the
whole class (rather than just a single method).
Therefore fields exist while the class is being `executed’ (rather than while a
single method is being executed).
What is the equivalent of a class being executed? When it is copied into an object.
Ergo, fields exist while an object exists.
FIELDS: LIFETIME (1)
25
public class RunningTimeCalculator {
private long currentTime;
public void recordCurrentTime() {
currentTime = System.currentTimeMillis();
}
public void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
Values assigned to this field won’t disappear
after the method that assigns the value has
finished executing.
This is tough, let’s see this in practice with our
current example…
26
FIELDS: LIFETIME (2)
27
public class Driver {
public static void main(String[] args) {
RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator();
copyOfRunningTimeCalculator.recordCurrentTime();
System.out.println("+------+");
System.out.println("|Martin|");
System.out.println("+------+");
copyOfRunningTimeCalculator.printRunningTime();
}
}
public class RunningTimeCalculator {
private long currentTime;
public void recordCurrentTime() {
currentTime = System.currentTimeMillis();
}
public void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
Let’s see how the lifetime of a field allows our run time calculator to operate
as intended.
FIELDS: LIFETIME (3)
28
public class Driver {
public static void main(String[] args) {
RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator();
copyOfRunningTimeCalculator.recordCurrentTime();
System.out.println("+------+");
System.out.println("|Martin|");
System.out.println("+------+");
copyOfRunningTimeCalculator.printRunningTime();
}
}
The life of the object, and thus the
fields it contains, and their stored
values.
Let’s see how the lifetime of a field allows our run time calculator to operate
as intended.
FIELDS: LIFETIME (4)
29
And we can use it in any program we like.
public class Driver {
public static void main(String[] args) {
RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator();
copyOfRunningTimeCalculator.recordCurrentTime();
System.out.println("+------+");
System.out.println("|Martin|");
System.out.println("+------+");
copyOfRunningTimeCalculator.printRunningTime();
}
}
ic class RunningTimeCalculator {
rivate long currentTime;
ublic void recordCurrentTime() {
currentTime = System.currentTimeMillis();
ublic void printRunningTime() {
1234567890
Let’s see how the lifetime of a field allows our run time calculator to operate
as intended.
🙈
Let’s see how the lifetime of a field allows our run time calculator to operate
as intended.
FIELDS: LIFETIME (2)
30
public class Driver {
public static void main(String[] args) {
RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator();
copyOfRunningTimeCalculator.recordCurrentTime();
System.out.println("+------+");
System.out.println("|Martin|");
System.out.println("+------+");
copyOfRunningTimeCalculator.printRunningTime();
}
}
public class RunningTimeCalculator {
private long currentTime;
public void recordCurrentTime() {
currentTime = System.currentTimeMillis();
}
public void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
}
}
We’ve moved to a new place
in the program, and the
field still retains the value
assigned previously.
1234567890
OUTPUT: REUSABLE RUNNING TIME CALCULATOR
31
Can’t we just store everything in fields to be sure?
We could, there would be nothing wrong with that from a
compilation perspective.
But stylistically this may be questionable.
• Does it make sense to have a variable visible in every method?
• Does it keep our class neat and readable?
• What about temporary results (e.g. from calculations) as
mentioned previously?
ASIDE: DO WE EVER NEED LOCAL VARIABLES, THEN?
32
😴
OPEN QUESTION 1: WHAT IF WE WE USE BOTH VARIABLES?
33
public class RunningTimeCalculator {
private long currentTime;
public void recordCurrentTime() {
long currentTime = System.currentTimeMillis();
}
void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
}
}
What if we — intentionally, accidentally or out of intrigue — decided to
use a local variable as well as a field, with the same name, what would
happen? Would this code compile?
This code does compile because Java has specific rules in this case to
avoid name conflict.
We will return to this idea.
It is probably intuitively clear that we can return variables (or the
result of manipulating variables) from a method, rather that just
literal values, but we have not yet seen this in practice.
We have seen this process for method calls (see Topic 2, Slide 43).
OPEN QUESTION 2: RETURNING VARIABLES FROM A METHOD (1)
34
int numberOne = 1;
printNumber(numberOne);
printNumber(1);
public long getRunningTime() {
return System.currentTimeMillis() - currentTime;
}
As such, we could modify the method we saw in the
previous example to return the running time rather than just printing it.
printRunningTime
😴
Whether you return a value (or a variable) from a method or simply
printed it is, again (you guessed it), a style choice.
Personally I always try and return values from methods rather than
printing them, and do as much printing in my Driver class as
possible.
• When a method gives you a value back, rather than just printing
it, you have the option to do something else with that value, so
the class becomes more reusable.
• So far I’ve been opting for printing within a method, because we
don’t yet have all the syntactic tools we need to always return
information from methods.
We will return to this idea.
OPEN QUESTION 2: RETURNING VARIABLES FROM A METHOD (2)
35
😴
Although we’re using our Driver class simply to house our main method, it is still
just a class.
Therefore, a valid question is, what do fields look like in this class, and how are they
referenced?
The answer is pretty much in the same way, only remember our rule: anything that
is referenced from main must also be static.
Thus, fields in the Driver class must be static.
OPEN QUESTION 3: WHAT IF WE ADD FIELDS TO OUR DRIVER CLASS?
36
public class Driver {
private static int fieldInDriver;
public static void main(String[] args) {
fieldInDriver = 1;
}
}
This might not be ideal, for (you’ve guessed it) reasons we will return to.
Reason #3: Storing Complex Data
37
🤓
Remember: fields are defined as part of a class (not a particular
method) and therefore exist while any objects of that class exists.
Because of this, in their simplest form, objects are simply multi-
slot variables.
We can use the fields inside an object to store multiple pieces of
data, rather than a single piece of data, which is what we would
do with a primitive variable.
OBJECTS AND VARIABLES
38
public class RunningTimeCalculator {
private long currentTime;
Moreover, recall that a data type describes the format of what a
variable will contain.
CLASSES AND TYPES
39
int myFirstIntegerVariable;
Because classes tell us the format of the fields inside an object it is
sensible to view our own classes as custom data types.
This idea is reinforced by the style of an object declaration, which is
very similar to that of a primitive variable declaration, as we have
seen…
😴
RELATIONSHIP: TYPES AND CLASSES; VARIABLES AND OBJECTS
40
Class Object Class Copy
Type Variable Value
int myFirstIntegerVariable = 1;
= new RunningTime
Calculator();
RunningTime
Calculator
copyOfRunningTime
Calculator
Describes the format and
structure of
holds
Describes the format and
structure of
holds
Let’s think about why we might want to define our own type,
and why it might be useful.
When programming, it is often useful to store pairs of
integers (e.g. a coordinate). But there’s no simple data type
that allows us to do this.
Let’s define our own data type (a class) that allows us to
store pairs of integer.
DEFINING OUR OWN DATA TYPE (1)
41
What will the fields of a Pair class look like?
Which methods will a Pair class need?
DEFINING OUR OWN DATA TYPE (2)
42
DEFINING OUR OWN DATA TYPE (3) - FIELDS
43
public class Pair {
private int valueA;
private int valueB;
}
DEFINING OUR OWN DATA TYPE (4) - METHODS
44
When we want to
store data in the
different fields of
an object, and
when we want to
get that data back
again, we need to
do so via
methods.
public class Pair {
private int valueA;
private int valueB;
public void setPair(int passedValueA, int passedValueB) {
valueA = passedValueA;
valueB = passedValueB;
}
public int getValueA() {
return valueA;
}
public int getValueB() {
return valueB;
}
}
ic class Driver {
ublic static void main(String[] args) {
Pair pair = new Pair();
pair.setPair(1, 2);
System.out.println(pair.getValueA());
DEFINING OUR OWN DATA TYPE (5) - DRIVER
45
Not that I’ve dropped
the `copyOf’ object
name prefix.
public class Pair {
private int valueA;
private int valueB;
public void setPair(int passedValueA, int passedVal
valueA = passedValueA;
valueB = passedValueB;
}
public int getValueA() {
return valueA;
}
public int getValueB() {
return valueB;
}
}
1 2
😴
In this example, our methods are predominantly what we call
accessors and mutators.
Accessors give us the value from a field back; mutators change
the value in that field to something else.
These are the simplest type of methods.
But it’s important to remember that methods can also perform
computation based upon data, as in our first example.
ASIDE: ACCESSORS AND MUTATORS
46
public void printRunningTime() {
System.out.println(System.currentTimeMillis() - currentTime);
}
ASIDE: A POTENTIAL OPTIMISATION? (1)
47
public class Pair {
private int valueA;
private int valueB;
public void setPair(int passedValueA, int passedValueB) {
valueA = passedValueA;
valueB = passedValueB;
}
public int getValueA() {
return valueA;
}
public int getValueB() {
return valueB;
}
}
These variable names
(parameters) are informative,
but they aren’t particularly
readable.
It would actually be quite nice
to be able to use consistent
names.
public class Pair {
private int valueA;
private int valueB;
public void setPair(int valueA, int valueB) {
valueA = passedValueA;
valueB = passedValueB;
}
public int getValueA() {
return valueA;
}
public int getValueB() {
return valueB;
}
}
Earlier, we discussed that this
will compile (parameters are
just another form of local
variable), but naming our
parameters in this way
necessitates the following
update.
public class Pair {
private int valueA;
private int valueB;
public void setPair(int valueA, int valueB) {
valueA = valueA;
valueB = valueB;
}
public int getValueA() {
return valueA;
}
public int getValueB() {
return valueB;
}
}
How do we then determine
which variable we want to
assign to which?
ASIDE: A POTENTIAL OPTIMISATION? (2)
48
If we want to reference fields,
but we have a (permitted)
naming conflict, we can use
the prefix this.
public class Pair {
private int valueA;
private int valueB;
public void setPair(int valueA, int valueB) {
this.valueA = valueA;
this.valueB = valueB;
}
public int getValueA() {
return valueA;
}
public int getValueB() {
return valueB;
}
}
When we write the keyword
this, we say to Java `get this
object’, and when we write a
dot after this, we are also
saying `and look for the
following:’.
So, here we are saying, `get
this object, and find the field
valueA’ (remember fields are
part of the object).
This syntax allows us to
differentiate between local
variables and fields with the
same name.
Because there’s no
conflict here, there is no
need for the this
notation.
But some people choose
to write this before all
field references, as a
style choice (I don’t).
🖥
In the laboratories, write your own data type. It doesn’t have
to be too complex, but make sure it has the following…
• The appropriate fields
• The appropriate methods
…also make sure you test your data type using a Driver
class.
DEFINE YOUR OWN DATA TYPE (1)
49Topic4-1
The idea of a class as a data type is exciting, because it
suggests that we can have multiple objects of the same
class, in the same program, in very much the same way that
we can have multiple variables of the same type, in the
same program.
In other words, our classes are not just reusable across
different programs, they’re reusable within the same
program.
This is probably intuitively clear, but let’s see what that
looks like anyway…
CLASSES AS TYPES (1): MULTIPLE OBJECTS
50
I can have as many Pairs as I like in my program.
MULTIPLE INSTANCES OF THE SAME CLASS
51
We call each object a different instance of the class.
public class Driver {
public static void main(String[] args) {
Pair pairA = new Pair();
Pair pairB = new Pair();
}
}
🤓
More exciting still, if variables of the same type can hold different
values, in the same program, then (the fields of) objects of the
same class can hold different values, in the same program.
In other words, not only can we reuse classes in the same
program, we can manipulate the data in the objects of those
classes without affecting other instances.
CLASSES AS TYPES (2): MULTIPLE OBJECTS WITH DIFFERENT VALUES
52
I can manipulate the values in one Pair instance, without
affecting the value in another.
MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (1)
53
public class Driver {
public static void main(String[] args) {
Pair pairA = new Pair();
Pair pairB = new Pair();
pairA.setPair(1, 2);
pairB.setPair(3, 4);
System.out.println(pairA.getValueA());
System.out.println(pairB.getValueA());
}
}
I can manipulate the values in one Pair instance, without
affecting the value in another.
MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (2)
54
MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (3)
55
This can often be hard to wrap your head around, because we
have only physically written one Pair class, and thus one set of
the and fields.
But because we make a copy of a class when we use the new
keyword, we also have virtual copies of the fields, which can be
manipulated separately.
valueA valueB
MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (4)
56
public class Driver {
public static void main(String[] args) {
Pair pairA = new Pair();
Pair pairB = new Pair();
pairA.setPair(1, 2);
pairB.setPair(3, 4);
System.out.println(pairA.getValueA());
System.out.println(pairB.getValueA());
}
}
public class Pair {
private int valueA;
private int valueB;
public void setPair(int passed
valueA = passedValueA;
valueB = passedValueB;
}
int getValueA() {
return valueA;
}
public int getValueB() {
return valueB;
}
}
pairA
public class Pair {
private int valueA;
private int valueB;
public void setPair(int passed
valueA = passedValueA;
valueB = passedValueB;
}
int getValueA() {
return valueA;
One pair class; two copies.
1 2
3 4
pairB
The ability for there to be multiple instances (objects) of the same class,
each with unique values, is like having a template or a blueprint that can
be configured in some way once the object it represents is created.
ANOTHER WAY TO THINK ABOUT CLASSES AND OBJECTS…
57
Pair
Pair pairA = new Pair(); Pair pairB = new Pair();
pairA.setPair(1, 2); pairB.setPair(3, 4);
🖥
DEFINE YOUR OWN TYPE (2)
58Topic4-2
In the laboratories, play with the new type you’ve just made:
• Make different instances of this type.
• Change the values in this type.
How would we go about defining a Pair if we didn’t have the
expressivity of objects and classes?
ASIDE: A LANGUAGE WITHOUT OBJECTS AND CLASSES
59
public class Driver {
public static void main(String[] args) {
int valueAInPair1 = 1;
int valueBInPair1 = 2;
int valueAInPair2 = 3;
int valueBInPair2 = 4;
}
}
With no neat way to collectively associate these values, things
become messy.
This necessitates complex data types like objects.
Because classes are types, we can replace any references to primitive
types, with references to classes.
Remember our printNumber method?
CLASSES AS TYPES (3): CUSTOM TYPES IN METHODS (1)
60
/**
* Prints the supplied number surrounded by a box.
*/
public static void printNumber(int num) {
System.out.println("+------+");
System.out.println("|" + num + "|");
System.out.println("+------+");
}
We can replace a simple integer parameter with a parameter of
Pair type, and extract an element from the pair in order to print
it out.
CLASSES AS TYPES (3): CUSTOM TYPES IN METHODS (2)
61
/**
* Prints the first value of the supplied pair
* surrounded by a box.
*/
public static void printNumber(Pair pair) {
System.out.println("+------+");
System.out.println("|" + pair.getValueA() + "|");
System.out.println("+------+");
}
CLASSES AS TYPES (3): CUSTOM TYPES IN METHODS (3)
62
We can also return objects of our class from a method, by
altering the return type to match our custom type.
public class Driver {
public static void main(String[] args) {
System.out.println(getMeAPair().getValueA());
}
public static Pair getMeAPair() {
Pair pairA = new Pair();
pairA.setPair(3, 4);
return pairA;
}
}
This is one way we can return
multiple values from a method
while still only returning one
thing.
🖥
In the laboratories, play even more with the new type you’ve
just made:
• Create methods that accept this type as a parameter.
• Create methods that return objects of this type.
DEFINE YOUR OWN TYPE (1)
63Topic4-3
CLASSES AS TYPES (4): CUSTOM TYPES AS FIELDS (1)
64
public class TwoPairs {
private Pair firstPair;
private Pair secondPair;
public void setFirstPair(Pair firstPair) {
this.firstPair = firstPair;
}
public void setSecondPair(Pair secondPair) {
this.secondPair = secondPair;
}
public Pair getFirstPair() {
return firstPair;
}
public Pair getSecondPair() {
return secondPair;
}
}
Now that we’ve opened
ourselves up to the
prospect of replacing
any primitive type with
one of our own class
types, it’s natural that
we can have methods
inside a class that
accept types of other
classes, and fields that
are able to store objects
of this type.
😴
Moreover, this now means that we can have methods inside
a class that accept types of that class itself.
CLASSES AS TYPES (4): CUSTOM TYPES AS FIELDS (2)
65
public class Pair {
private int valueA;
private int valueB;
public void setPair(Pair pair) {
valueA = pair.getValueA();
valueB = pair.getValueB();
}
We’ll return
to this idea
later in the
topic.
In increasing order of importance (to keep the purists
happy):
Classes and objects provide us with a way to organise our
code (Topic 3).
Classes and object provide us with a way in which to reuse
our code.
WHY OBJECT-ORIENTATION: SO FAR WE KNOW…
66
Objects provide us with an place in which to store complex
data; classes define what that data looks like.
Reason #4: Expressivity
67
So far we have used classes to model very technical things: e.g. a
running time calculator, a pair data type.
• We did this for practical purposes such as organisation,
reuse and data storage.
This is important, as it shows us very early on how object-oriented
programming might actually be used.
But it’s also important to learn that object-orientation is designed
to be a much larger and expressive tool.
In fact, we can write code that models almost anything in the
world with object-orientation. Why?
Because object-orientation comes from the real world.
THE EXPRESSIVITY OF OBJECT-ORIENTED PROGRAMMING (1)
68
We all have classes in our mind; shared concepts of things in
the world.
CLASSES AND OBJECTS IN THE REAL WORLD (1)
69
We see objects of these classes every day; real instances of
these concepts.
CLASSES AND OBJECTS IN THE REAL WORLD (2)
70
The idea of a
dog doesn’t just
exist in our
minds, they
exist all around
us.
The fields of these classes are clear; common features that we can all
identify as a part of the shared concept.
But fields often hold different data; things in the world are in different
states.
CLASSES AND OBJECTS IN THE REAL WORLD (3): STATE
71
The methods of these classes are clear; common actions
that we can all identify as being exhibited by the shared
concept.
Things in the world exhibit certain behaviours.
CLASSES AND OBJECTS IN THE REAL WORLD (4): BEHAVIOUR
72
It’s clear to see methods updating fields; actions when
performed have some effect on the object.
Behaviours affect state.
CLASSES AND OBJECTS IN THE REAL WORLD (4): STATE AND BEHAVIOUR
73
🤓
This understanding shows us that not only can we model
technical things (like pairs and run time counters), but we
can also model real world objects. Why would we do this?
• Simulation
• Games
• Learning. Certain concepts are easier to understand
when we aim to model real objects.
Let’s try it out…
THE EXPRESSIVITY OF OBJECT-ORIENTED PROGRAMMING (2)
74
In order to revisit the mind bending notion of a class
accepting parameters of itself (Slide 64), let’s aim to model
a bank account.
MODELLING THE REAL WORLD
75
This is a good example, because it’s a concept from the real
world, but we can also imagine our code supporting a real
banking system.
What’s the state (fields) of a bank account?
MODELLING A BANK ACCOUNT (1)
76
Let’s just think about a balance for now.
Let’s think about deposit, withdraw and transfer.
Which behaviours (methods) can a bank account exhibit?
MODELLING A BANK ACCOUNT (2): FIELDS
77
public class BankAccount {
private double balance;
}
We should definitely use a floating point
format here as we’ll be dealing with currency.
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance = balance + amount;
}
public void printBalance() {
System.out.println(balance);
}
}
MODELLING A BANK ACCOUNT (3): DEPOSIT
78
Some more basic arithmetic,
which we will return to, along
with the idea of reassignment.
It is not
enough to
simply add a
value to the
amount, we
must also
reassign the
new value, or
the result
will be lost.
😴
We’ve now seen the plus (+) symbol used for two different things:
• Concatenation (placing things side-by-side in output)
ASIDE: ADDING OR CONCATENATION?
79
System.out.println("|" + num + "|");
• Adding numbers together.
balance = balance + amount;
How does Java know which to do? When the operands either side of
the symbol are numeric (and the only things present), Java will
perform arithmetic such as addition.
(Another) Open question: How does Java know when to concatenate?
🖥
ASIDE: DEFAULT VARIABLE VALUES
80Topic4-4
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance = balance + amount;
}
void printBalance() {
System.out.println(balance);
}
}
You’ll notice here that, the first time we deposit an amount, it looks like
we’re adding this amount to nothing, as we haven’t explicitly specified
the initial amount in the balance field.
In reality this isn't strictly true, as primitive types when they are used as
fields are given default values.
In this case, the default value given to a double value is 0.0, so our code
works as intended. In your laboratory session, investigate the default
values prescribed to other primitive types when they are used as fields.
We first
discussed this
in Topic 2.
MODELLING A BANK ACCOUNT (4): TESTING DEPOSIT
81
public class Driver {
public static void main(String[] args) {
BankAccount accountA = new BankAccount();
accountA.deposit(10.0);
accountA.printBalance();
}
}
We’ll investigate further exactly what is going on here shortly.
MODELLING A BANK ACCOUNT (5): WITHDRAW
82
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance = balance + amount;
}
public void printBalance() {}
public void withdraw(double amount) {
balance = balance - amount;
}
}
I’m going to shorten
this method down,
because we know
what it does (I may
do this from time-to-
time to focus
attention on other
methods).
MODELLING A BANK ACCOUNT (6): TESTING WITHDRAW
83
public class Driver {
public static void main(String[] args) {
BankAccount accountA = new BankAccount();
accountA.deposit(10.0);
accountA.printBalance();
accountA.withdraw(10.0);
accountA.printBalance();
}
}
Again, we’ll examine what is going on here shortly.
MODELLING A BANK ACCOUNT (7): TRANSFER
84
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance = balance + amount;
}
public void printBalance() { … }
public void withdraw(double amount) {
balance = balance - amount;
}
public void transfer(BankAccount otherAccount, double amount) {
withdraw(amount);
otherAccount.deposit(amount);
}
}
We could just interact
with the balance field
directly, but why not call
the method we already
have?
🙈
MODELLING A BANK ACCOUNT (8): TESTING TRANSFER
85
s Driver {
ublic static void main(String[] args) {
BankAccount accountA = new BankAccount();
accountA.deposit(10.0);
accountA.printBalance();
accountA.withdraw(10.0);
accountA.printBalance();
accountA.deposit(1000.0);
BankAccount accountB = new BankAccount();
accountA.transfer(accountB, 100);
accountA.printBalance();
accountB.printBalance();
public class BankAccount {
private double balance;
public void deposit(double amount) {
balance = balance + amount;
}
public void printBalance() { … }
public void withdraw(double amount) {
balance = balance - amount;
}
public void transfer(BankAccount otherAccount, double
withdraw(amount);
otherAccount.deposit(amount);
}
}
0.0
0.0 0.0
0.0 0.0
10.0 10.0
10.0 10.0
10.0
accountA
10.0
0.0
0.0 0.0
0.0 0.0
1000.0 1000.0
1000.0 1000.01000.0
900.0 900.0
900.0 900.0
900.0
public class BankAccount {
private double balance;
public void deposit(double amount) {
0.0 0.0
0.0
0.00.0
100100
accountB
public class BankAccount {
private double balance;
public void deposit(double amount) {
accountB
100.00.0
accountB
100.0 100.0
100.0
100.0100.0
🖥
The previous slide had 72 animations in it (!).
I’ve placed a video of these animations on KEATS.
In the lab, go through the video animation by animation,
and see if you can write a simple sentence that describes
what is going on each time something moves.
Keep this description for revision.
You should also write out all the bank account code and
annotate it with comments (although you should be doing
that anyway).
FRAME BY FRAME
86Topic4-5
😴
In the previous topic, you should have discerned that Java is
always pass by value.
ASIDE: BACK TO PASS-BY-REFERENCE AND PASS-BY-VALUE (1)
87
public class NumberChanger {
public static void changeNumber(int changeMe) {
changeMe = 2;
}
public static void main(String[] args) {
int numberOne = 1;
changeNumber(numberOne);
System.out.println(numberOne);
}
}
When a variable is
passed to a
method, it is
effectively copied,
such that any
interactions with
that variable have
no effect on the
original variable.
This will print ‘1’.
😴
class Driver {
public static void main(String[] args) {
BankAccount accountA = new BankAccount();
accountA.deposit(10.0);
accountA.printBalance();
accountA.withdraw(10.0);
accountA.printBalance();
accountA.deposit(1000.0);
BankAccount accountB = new BankAccount();
accountA.transfer(accountB, 100);
accountA.printBalance();
accountB.printBalance();
}
}
So, then, why is it the case that retains the £100 transferred to
it, if only a copy of is passed to ‘s transfer method?
ASIDE: BACK TO PASS-BY-REFERENCE AND PASS-BY-VALUE (2)
88
accountB
accountB accountA
😴
That’s because, at our current level of abstraction, the rule
is slightly different for objects.
When an object is passed to a method, any interactions with
that object will alter the original object.
• Reassigning the variable holding the class copy,
however, will not alter the object.
Next semester, when we look at objects in memory (i.e. a
lower level of abstraction), the reasoning behind this should
become clearer.
ASIDE: OBJECTS IN MEMORY (2)
89
🖥
In a laboratory, go back and model the real world dog object
we showed earlier as a class, or even better, pick your own
object to model.
• This class should have appropriate fields
• This class should have appropriate methods.
• Methods should update the values in fields, where
appropriate.
• You should test this class with a Driver class.
MODEL YOUR OWN REAL WORLD OBJECT
90Topic4-6
In increasing order of importance (to keep the purists
happy):
Classes and objects provide us with a way to organise our
code (Topic 3).
Classes and object provide us with a way in which to reuse
our code.
Objects provide us with an place in which to store complex
data; classes define what that data looks like.
WHY OBJECT-ORIENTATION: SO FAR WE KNOW…
91
Objects and classes provide a natural way to conceptualise
the world.
Reason #5: Control
92
Once you start writing code on a more permanent basis, it’s
inevitable that your code will be used by other people.
This is particularly true if you write reusable code, like we
have been doing so far.
The unfortunate thing about people is that they often do
things wrong, break your code and then complain.
So, it’s important to control how your code is used.
The object-oriented paradigm gives us several ways to
control how our code is used.
SENDING YOUR CODE OUT INTO THE WORLD
93
😴
We use documentation to help us understand our own code, but
remember it’s also a useful tool in helping other people to
understand our code.
CONTROL 1: DOCUMENTING YOUR CODE (1)
94
Accessible code documentation is something that’s indirectly
supported by object-orientation (more later).
This doesn’t stop people using your code incorrectly, but
reduces the risk of it happening.
/**
* Prints the supplied number surrounded by a box.
*/
public static void printNumber(int num) {
System.out.println("+------+");
System.out.println("|" + num + "|");
System.out.println("+------+");
}
CONTROL 1: DOCUMENTING YOUR CODE (2): CLASS DIAGRAMS
95
What are these?
We can also use the
expressivity of our class
diagram to show fields.
In Topic 3, I promised I would discuss what the brackets, appearing
after the name of the class you wish to copy are for (we also saw
these in the previous diagram). Now is this time.
This format looks very much like a method call.
BRACKETS WHEN INITIALISING AN OBJECT
96
public class Driver {
public static void main(String[] args) {
MartinPrinter copyOfMartinPrinter
}
}
new MartinPrinter();=
That’s because it is! Every class has a hidden method called
a constructor which is called every time you write the new
command.
HIDDEN METHODS
97
public class Driver {
public static void main(String[] args) {
BankAccount accountA = new BankAccount();
}
}
public class BankAccount {
private double balance;
public BankAccount() {
}
void deposit(double amount) {
The method labels
match.
CONSTRUCTORS
98
public class BankAccount {
private double balance;
public BankAccount() {
}
void deposit(double amount) {
These hidden methods are known as implicit constructors because
they are called when you construct an object (make a copy of the
code in a class).
Behind the scenes, they help setup the class for use (more later).
CONSTRUCTORS: OUT OF OBSCURITY
99
public class BankAccount {
private double balance;
public BankAccount() {
System.out.println("Oooh I'm being constructed");
}
public void deposit(double amount) {
Constructors don’t have to be hidden, however, we can actually write
them into our classes, and place code into them like a normal method.
Any code inside a constructor will therefore be called when the class is
first copied.
Note how we differentiate a constructor from a normal method:
1. No
return
type
2. The same name
as the class
Apart from having no return type and having to have the same
name as the class — to ensure they are called when the class
is first constructed — constructors are just normal methods.
This means we can add parameters to a constructor, if we
wish to.
PARAMETERS IN CONSTRUCTORS (1)
100
public class BankAccount {
private double balance;
public BankAccount(int initialDeposit) {
System.out.println("Oooh I'm being constructed");
}
What is the effect of this?
Remember that in order to alter Java’s execution order, you typically
have to match a certain pattern (i.e. match the signature of a
method).
Thus, the current way in which we construct an object, in our bank
account example, will no longer work, because we do not use the
correct pattern to match the constructor and thus create an object
of the class.
As such, we cannot make copies of or use the class without giving an
initial deposit.
PARAMETERS IN CONSTRUCTORS (2)
101
PARAMETERS IN CONSTRUCTORS (3)
102
public class BankAccount {
private double balance;
public BankAccount(int initialDeposit) {
System.out.println("Oooh I'm being constructed");
}
public class Driver {
public static void main(String[] args) {
BankAccount accountA = new BankAccount();
}
}
ERROR: CONSTRUCTOR CANNOT BE APPLIED TO GIVEN TYPES
103
PARAMETERS IN CONSTRUCTORS (4)
104
public class BankAccount {
private double balance;
public BankAccount(int initialDeposit) {
System.out.println("Oooh I'm being constructed");
}
public class Driver {
public static void main(String[] args) {
BankAccount accountA = new BankAccount(100);
}
}
🤓
Therefore, when we place a constructor with parameters
into our code, we are able to control how our class is used
by forcing a user to provide values when an object is made
of that class.
Why is this beneficial?
Because the code in a constructor is the first thing to
execute when an object is made of a class, we can take the
data supplied to the constructor and store it in one or more
fields, to ensure that any methods that rely on data being in
these fields do not cause unexpected or erroneous
behaviour.
CONTROL 2: CONSTRUCTORS (1)
105
😴
In this scenario, of course, we assume that the user makes a sensible
deposit (i.e. a positive number).
Open question: How would we discern whether a suitable deposit
had been made?
CONTROL 2: CONSTRUCTORS (2)
106
public class BankAccount {
private double balance;
public BankAccount(int initialDeposit) {
balance = initialDeposit;
}
public void withdraw(double amount) {
balance = balance - amount;
}
Here, the assignment of the initial
deposit will always be the first thing
that happens in our class.
So when a
withdrawal is
made, we know
that it is being
taken after an
initial deposit is
made.
So, in theory, one
cannot take money
from an account
before putting
something in
As a more practical example, we could consider adding two
parameters to a constructor in our Pair class.
CONTROL 2: CONSTRUCTORS (3)
107
It also enables more efficient use of the class, as the setting
of the values is effectively combined with the construction of
the class.
This would avoid a user calling an accessor without first
specifying what the pair contains, and receiving a zero
value.
public Pair(int valueA, int valueB) {
Pair pair = new Pair();
pair.getValueA();
Pair pair = new Pair(1, 2);
pair.getValueA();
There’s also an element of style here.
The intelligibility of a class is increased if it’s clear what
data that class needs to operate by just looking at the
constructor.
The constructor is also a good place to initialise fields, and
stops us having to initialise on the field itself.
• Less chance of inefficient reassignment when
programming.
• Stylistically nicer.
CONTROL 2: CONSTRUCTORS (4) - STYLE (1)
108
So, if we wanted to give everyone with a bank account £100 (rather
than asking for an initial deposit), we would do it like this…
CONTROL 2: CONSTRUCTORS (4) - STYLE (2)
109
…instead of this…
private double balance = 100;
private double balance;
public BankAccount() {
balance = 100;
}
(This is a debated topic.)
In general, at this
stage, I would not
do anything
outside a method,
except declare
fields.
😴
There might be a case in which we want to offer the user the
ability to pass information to an object when it is
constructed, but not force them to.
For example, we might want to give the user the option to
specify an initial balance when they create an account or to
create an account without specifying this value.
ASIDE: RELINQUISHING CONTROL FOR FLEXIBILITY (1)
110
public static void main(String[] args) {
BankAccount account = new BankAccount();
BankAccount secondAccount = new BankAccount(100);
}
😴
If we want to do this we can specify multiple constructors,
each of which accepts different parameters, thus giving a user
the option to construct an object of our class in different ways.
ASIDE: RELINQUISHING CONTROL FOR FLEXIBILITY (2)
111
private double balance;
public BankAccount() {}
public BankAccount(double deposit) {
balance = deposit;
}
Each of our constructors has a
different pattern, and thus there are
different ways to match and create
objects of the class.
Open question: Why
is it ok for us to
break our `methods
with unique
names’ (Topic 3,
Slide 14) rule here?
Specifically, we use an empty
constructor. We can see this as replacing
the default constructor we had
previously, to remove any restrictions
from the creation of objects of our class.
You’ve been patient, and now it’s time to talk about public and
private.
The meaning of these access modifiers should be intuitively
clear, given that we’ve been calling code in other classes by
creating objects:
• Anything that is public in a class can be referenced from
any other class that is used as part of your program.
• When we look at packaging up classes next semester,
we will see how this rule changes.
• Anything that is private can only be referenced within the
class itself.
CONTROL 3: FINALLY, TO PUBLIC AND PRIVATE…
112
Given this definition, it makes sense for classes to be public as
we want to be able to use our classes in as many different
places as possible.
But it’s less clear why we want our fields to be private,
especially when I show you the following piece of bad but oh so
tempting syntax:
PUBLIC VS. PRIVATE (1)
113
public static void main(String[] args) {
Pair pair = new Pair();
pair.valueA = 1;
} public class Pair {
public int valueA;
We can avoid writing accessor and
mutator methods if we make our
fields public.
public static void main(String[] args) {
Pair pair = new Pair();
pair.
}
Remember our dot syntax? As this allows us to reference any public
identifiers, we can also access public variables.
PUBLIC VS. PRIVATE (2)
114
public class Pair {
public int valueA;
🤓
It’s not always the case that we want users to be able to set
the values in fields arbitrarily.
For example, let’s imagine that we implement a pin system
into our bank account, such that a user must supply the
right pin before being allowed to make a withdrawal:
WHAT’S WRONG WITH PUBLIC FIELDS? (1)
115
If our balance field is public, users can simply interact with it directly,
circumventing the pin, and do what they wish.
WHAT’S WRONG WITH PUBLIC FIELDS? (2)
116
public class BankAccount {
private double balance;
public void withdraw(double amount, int pin) {
balance = balance - amount;
}
public
BankAccount account = new BankAccount();
account.balance = -10000;
(This method is incomplete)
🤓
Instead, we want to force all requests to alter the values in
fields to come through the parameters of methods. We do
this by making fields private.
In this way, we can validate that a request is sensible (e.g.
the correct pin is supplied) and doesn’t adversely affect the
intended operation of our class, inside the method itself.
WHAT’S WRONG WITH PUBLIC FIELDS? (3)
117
But we’re currently missing the logic to do this.
VALIDATING PARAMETERS INSIDE A METHOD
118
public class BankAccount {
private double balance;
public void withdraw(double amount, int pin) {
}
Ensure pin is correct before:
If not, print:
balance = balance - amount;
System.out.println("Ah ah ah, you didn't say the magic word.");
🤓
When we ensure users can only change the data (state) of
an object (of a class) through methods, we say that our class
has a well defined interface.
THE CLASS INTERFACE AND ENCAPSULATION
119
The process of hiding data from our user, and carefully
selecting which methods are available to manipulate that
data, is known as encapsulation.
An interface, in this
context, means a
point of contact.
😴
What about private in unexpected places?
• A private class
• A private constructor
Are these the only access modifiers available to us?
What happens if we don’t write an access modifier?
We will return to these questions, mostly in the second
semester.
ASIDE: OPEN QUESTIONS ABOUT ACCESS MODIFIERS?
120
In increasing order of importance (to keep the purists happy):
Classes and objects provide us with a way to organise our
code (Topic 3).
Classes and object provide us with a way in which to reuse
our code.
Objects provide us with an place in which to store complex
data; classes define what that data looks like.
Objects and classes provide a natural way to conceptualise
the world.
WHY OBJECT-ORIENTATION?
121
Object-orientation allows us to control how our code is used.
Dr. Martin Chapman
programming@kcl.ac.uk
martinchapman.co.uk/teaching
Programming Practice and Applications (4CCS1PPA)
Topic 4: Why Object-Orientation?
These slides will be available on KEATS, but will be subject to
ongoing amendments. Therefore, please always download a new
version of these slides when approaching an assessed piece of work,
or when preparing for a written assessment. 122
Thursday 13th October

Contenu connexe

Tendances

Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked ListsAfaq Mansoor Khan
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
shell and merge sort
shell and merge sortshell and merge sort
shell and merge sorti i
 
GSP 125 Enhance teaching - snaptutorial.com
GSP 125   Enhance teaching - snaptutorial.comGSP 125   Enhance teaching - snaptutorial.com
GSP 125 Enhance teaching - snaptutorial.comDavisMurphyA81
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL fileRACHIT_GUPTA
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...Dharmendra Prasad
 
Gsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.comGsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.comamaranthbeg8
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMSTanya Makkar
 

Tendances (18)

All experiment of java
All experiment of javaAll experiment of java
All experiment of java
 
Java lab-manual
Java lab-manualJava lab-manual
Java lab-manual
 
Ch10 Recursion
Ch10 RecursionCh10 Recursion
Ch10 Recursion
 
Dynamic Memory & Linked Lists
Dynamic Memory & Linked ListsDynamic Memory & Linked Lists
Dynamic Memory & Linked Lists
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
shell and merge sort
shell and merge sortshell and merge sort
shell and merge sort
 
Isola 12 presentation
Isola 12 presentationIsola 12 presentation
Isola 12 presentation
 
GSP 125 Enhance teaching - snaptutorial.com
GSP 125   Enhance teaching - snaptutorial.comGSP 125   Enhance teaching - snaptutorial.com
GSP 125 Enhance teaching - snaptutorial.com
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
3 recursion
3 recursion3 recursion
3 recursion
 
Java PRACTICAL file
Java PRACTICAL fileJava PRACTICAL file
Java PRACTICAL file
 
introduction
introductionintroduction
introduction
 
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
Lecture 3  data structures & algorithms - sorting techniques - http://techiem...Lecture 3  data structures & algorithms - sorting techniques - http://techiem...
Lecture 3 data structures & algorithms - sorting techniques - http://techiem...
 
Gsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.comGsp 125 Future Our Mission/newtonhelp.com
Gsp 125 Future Our Mission/newtonhelp.com
 
Recursion
RecursionRecursion
Recursion
 
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
TIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMSTIME EXECUTION   OF  DIFFERENT SORTED ALGORITHMS
TIME EXECUTION OF DIFFERENT SORTED ALGORITHMS
 

Similaire à Programming in Java: Why Object-Orientation?

Programming in Java: Organising Your Code
Programming in Java: Organising Your CodeProgramming in Java: Organising Your Code
Programming in Java: Organising Your CodeMartin Chapman
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2Bianca Teşilă
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Umar Farooq
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1stConnex
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIEduardo Bergavera
 
An eternal question of timing
An eternal question of timingAn eternal question of timing
An eternal question of timingPVS-Studio
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxsaber tabatabaee
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)indiangarg
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7Vince Vo
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Akhil Mittal
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingHock Leng PUAH
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console ProgramHock Leng PUAH
 
Assg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docx
Assg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docxAssg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docx
Assg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docxcargillfilberto
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#SyedUmairAli9
 

Similaire à Programming in Java: Why Object-Orientation? (20)

Programming in Java: Organising Your Code
Programming in Java: Organising Your CodeProgramming in Java: Organising Your Code
Programming in Java: Organising Your Code
 
Data structures and algorithms lab2
Data structures and algorithms lab2Data structures and algorithms lab2
Data structures and algorithms lab2
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#Polymorphism, Abstarct Class and Interface in C#
Polymorphism, Abstarct Class and Interface in C#
 
Presentation 1st
Presentation 1stPresentation 1st
Presentation 1st
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Chapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part IIChapter 7 - Defining Your Own Classes - Part II
Chapter 7 - Defining Your Own Classes - Part II
 
Abstraction
Abstraction Abstraction
Abstraction
 
Abstraction
AbstractionAbstraction
Abstraction
 
An eternal question of timing
An eternal question of timingAn eternal question of timing
An eternal question of timing
 
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptxclean architecture uncle bob AnalysisAndDesign.el.en.pptx
clean architecture uncle bob AnalysisAndDesign.el.en.pptx
 
Oop
OopOop
Oop
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)Programming Fundamentals With OOPs Concepts (Java Examples Based)
Programming Fundamentals With OOPs Concepts (Java Examples Based)
 
Reflection
ReflectionReflection
Reflection
 
Java căn bản - Chapter7
Java căn bản - Chapter7Java căn bản - Chapter7
Java căn bản - Chapter7
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
Assg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docx
Assg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docxAssg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docx
Assg 11 Priority Queues and Queue ApplicationsCOSC 2336 S.docx
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 

Plus de Martin Chapman

Principles of Health Informatics: Artificial intelligence and machine learning
Principles of Health Informatics: Artificial intelligence and machine learningPrinciples of Health Informatics: Artificial intelligence and machine learning
Principles of Health Informatics: Artificial intelligence and machine learningMartin Chapman
 
Principles of Health Informatics: Clinical decision support systems
Principles of Health Informatics: Clinical decision support systemsPrinciples of Health Informatics: Clinical decision support systems
Principles of Health Informatics: Clinical decision support systemsMartin Chapman
 
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...Martin Chapman
 
Technical Validation through Automated Testing
Technical Validation through Automated TestingTechnical Validation through Automated Testing
Technical Validation through Automated TestingMartin Chapman
 
Scalable architectures for phenotype libraries
Scalable architectures for phenotype librariesScalable architectures for phenotype libraries
Scalable architectures for phenotype librariesMartin Chapman
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Martin Chapman
 
Using AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patientsUsing AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patientsMartin Chapman
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Martin Chapman
 
Principles of Health Informatics: Evaluating medical software
Principles of Health Informatics: Evaluating medical softwarePrinciples of Health Informatics: Evaluating medical software
Principles of Health Informatics: Evaluating medical softwareMartin Chapman
 
Principles of Health Informatics: Usability of medical software
Principles of Health Informatics: Usability of medical softwarePrinciples of Health Informatics: Usability of medical software
Principles of Health Informatics: Usability of medical softwareMartin Chapman
 
Principles of Health Informatics: Social networks, telehealth, and mobile health
Principles of Health Informatics: Social networks, telehealth, and mobile healthPrinciples of Health Informatics: Social networks, telehealth, and mobile health
Principles of Health Informatics: Social networks, telehealth, and mobile healthMartin Chapman
 
Principles of Health Informatics: Communication systems in healthcare
Principles of Health Informatics: Communication systems in healthcarePrinciples of Health Informatics: Communication systems in healthcare
Principles of Health Informatics: Communication systems in healthcareMartin Chapman
 
Principles of Health Informatics: Terminologies and classification systems
Principles of Health Informatics: Terminologies and classification systemsPrinciples of Health Informatics: Terminologies and classification systems
Principles of Health Informatics: Terminologies and classification systemsMartin Chapman
 
Principles of Health Informatics: Representing medical knowledge
Principles of Health Informatics: Representing medical knowledgePrinciples of Health Informatics: Representing medical knowledge
Principles of Health Informatics: Representing medical knowledgeMartin Chapman
 
Principles of Health Informatics: Informatics skills - searching and making d...
Principles of Health Informatics: Informatics skills - searching and making d...Principles of Health Informatics: Informatics skills - searching and making d...
Principles of Health Informatics: Informatics skills - searching and making d...Martin Chapman
 
Principles of Health Informatics: Informatics skills - communicating, structu...
Principles of Health Informatics: Informatics skills - communicating, structu...Principles of Health Informatics: Informatics skills - communicating, structu...
Principles of Health Informatics: Informatics skills - communicating, structu...Martin Chapman
 
Principles of Health Informatics: Models, information, and information systems
Principles of Health Informatics: Models, information, and information systemsPrinciples of Health Informatics: Models, information, and information systems
Principles of Health Informatics: Models, information, and information systemsMartin Chapman
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Martin Chapman
 
Using Microservices to Design Patient-facing Research Software
Using Microservices to Design Patient-facing Research SoftwareUsing Microservices to Design Patient-facing Research Software
Using Microservices to Design Patient-facing Research SoftwareMartin Chapman
 
Using CWL to support EHR-based phenotyping
Using CWL to support EHR-based phenotypingUsing CWL to support EHR-based phenotyping
Using CWL to support EHR-based phenotypingMartin Chapman
 

Plus de Martin Chapman (20)

Principles of Health Informatics: Artificial intelligence and machine learning
Principles of Health Informatics: Artificial intelligence and machine learningPrinciples of Health Informatics: Artificial intelligence and machine learning
Principles of Health Informatics: Artificial intelligence and machine learning
 
Principles of Health Informatics: Clinical decision support systems
Principles of Health Informatics: Clinical decision support systemsPrinciples of Health Informatics: Clinical decision support systems
Principles of Health Informatics: Clinical decision support systems
 
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
Mechanisms for Integrating Real Data into Search Game Simulations: An Applica...
 
Technical Validation through Automated Testing
Technical Validation through Automated TestingTechnical Validation through Automated Testing
Technical Validation through Automated Testing
 
Scalable architectures for phenotype libraries
Scalable architectures for phenotype librariesScalable architectures for phenotype libraries
Scalable architectures for phenotype libraries
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...
 
Using AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patientsUsing AI to autonomously identify diseases within groups of patients
Using AI to autonomously identify diseases within groups of patients
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...
 
Principles of Health Informatics: Evaluating medical software
Principles of Health Informatics: Evaluating medical softwarePrinciples of Health Informatics: Evaluating medical software
Principles of Health Informatics: Evaluating medical software
 
Principles of Health Informatics: Usability of medical software
Principles of Health Informatics: Usability of medical softwarePrinciples of Health Informatics: Usability of medical software
Principles of Health Informatics: Usability of medical software
 
Principles of Health Informatics: Social networks, telehealth, and mobile health
Principles of Health Informatics: Social networks, telehealth, and mobile healthPrinciples of Health Informatics: Social networks, telehealth, and mobile health
Principles of Health Informatics: Social networks, telehealth, and mobile health
 
Principles of Health Informatics: Communication systems in healthcare
Principles of Health Informatics: Communication systems in healthcarePrinciples of Health Informatics: Communication systems in healthcare
Principles of Health Informatics: Communication systems in healthcare
 
Principles of Health Informatics: Terminologies and classification systems
Principles of Health Informatics: Terminologies and classification systemsPrinciples of Health Informatics: Terminologies and classification systems
Principles of Health Informatics: Terminologies and classification systems
 
Principles of Health Informatics: Representing medical knowledge
Principles of Health Informatics: Representing medical knowledgePrinciples of Health Informatics: Representing medical knowledge
Principles of Health Informatics: Representing medical knowledge
 
Principles of Health Informatics: Informatics skills - searching and making d...
Principles of Health Informatics: Informatics skills - searching and making d...Principles of Health Informatics: Informatics skills - searching and making d...
Principles of Health Informatics: Informatics skills - searching and making d...
 
Principles of Health Informatics: Informatics skills - communicating, structu...
Principles of Health Informatics: Informatics skills - communicating, structu...Principles of Health Informatics: Informatics skills - communicating, structu...
Principles of Health Informatics: Informatics skills - communicating, structu...
 
Principles of Health Informatics: Models, information, and information systems
Principles of Health Informatics: Models, information, and information systemsPrinciples of Health Informatics: Models, information, and information systems
Principles of Health Informatics: Models, information, and information systems
 
Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...Using AI to understand how preventative interventions can improve the health ...
Using AI to understand how preventative interventions can improve the health ...
 
Using Microservices to Design Patient-facing Research Software
Using Microservices to Design Patient-facing Research SoftwareUsing Microservices to Design Patient-facing Research Software
Using Microservices to Design Patient-facing Research Software
 
Using CWL to support EHR-based phenotyping
Using CWL to support EHR-based phenotypingUsing CWL to support EHR-based phenotyping
Using CWL to support EHR-based phenotyping
 

Dernier

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 

Dernier (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 

Programming in Java: Why Object-Orientation?

  • 1. Dr. Martin Chapman programming@kcl.ac.uk martinchapman.co.uk/teaching Programming Practice and Applications (4CCS1PPA) Topic 4: Why Object-Orientation? Q: What do you think constitutes a real world object? 1 Thursday 13th October
  • 2. To understand why object-orientation is an important paradigm for code development. WHY OBJECT-ORIENTATION: OBJECTIVES 2
  • 3. Reason #1: Code Organisation (Review) 3
  • 4. 🙈 public class Driver { public static void main(String[] args) { MartinPrinter copyOfMartinPrinter } } REMEMBER: MAKING A COPY OF A CLASS 4 new MartinPrinter();= How do we do this? We need to request a new copy of the class. We use the labelled name of the class so that Java is able to match the class we want. Then we need to put this copy inside the variable, using an assignment, in the same way that we put values inside variables. We will call a variable that contains a copy of a class an object.
  • 5. 😴 public class MartinPrinter { public static void printMartin() { System.out.println("+------+"); System.out.println("|Martin|"); System.out.println("+------+"); } } public class NumberPrinter { public static void printNumber(int num) { System.out.println("+------+"); System.out.println("|" + num + "|"); System.out.println("+------+"); Methods have to be static if they are called from a method that is also static. REMEMBER: STATIC METHODS AND OBJECTS 5 Because these methods are now being called through an object we can drop the static keyword.
  • 6. Remember ? The native output from this statement isn’t particularly clear (it’s just a number). Make a class called with a method called _________. In this method we should print "The Unix time is: " followed by the (Unix) time. You should then make a class, which makes an object of the class, and uses this object to call the method. LECTURE EXERCISE: PRETTY TIME (1) 6 System.currentTimeMillis() PrettyTime printTime Driver PrettyTime printTime
  • 7. LECTURE EXERCISE: PRETTY TIME (2) 7 public class PrettyTime { public void printTime() { long currentTime = System.currentTimeMillis(); System.out.println("The Unix time is: " + currentTime); } } public class Driver { public static void main(String[] args) { PrettyTime prettyTime = new PrettyTime(); prettyTime.printTime(); } } printTime here is not static, and the methods in your coursework should not be either. We could leave this methods static, and reference it using the class, but as we haven’t yet fully explored the implications of doing this, we will not.
  • 9. Often, when first learning to program, people do things in a fairly verbose way. This is fine, as its important to start somewhere. But often, when reviewing code, we can find small enhancements to make. For example, these two lines: ASIDE: MAKING THINGS MORE EFFICIENT (1) 9 Could be reduced to this single line: long currentTime = System.currentTimeMillis(); System.out.println("The Unix time is: " + currentTime); System.out.println("The Unix time is: " + System.currentTimeMillis());
  • 10. ASIDE: MAKING THINGS MORE EFFICIENT (2) 10 Investigating your code and trying to reduce the number of lines you have used is an important skill to learn. • Less lines are neater and easier to read • Code is often more efficient. In this case, for example, we’re not using memory to store the time unnecessarily (although remember we won’t concern ourselves with memory too much). In the laboratories, when you are copying the code in these slides in order to try it out, check if anything can be done more efficiently. • Have I done certain things inefficiently? Probably! Catch me out, and let me know. • Tackling verbosity does not always necessitate the introduce of new syntax.
  • 11. Classes and objects provide us with a way to organise our code (Topic 3). WHY OBJECT-ORIENTATION: SO FAR WE KNOW… 11
  • 12. Object-oriented purists would be angry that I’m only selling classes (and their associated objects) as a way to organise code, because the idea is much more powerful. • But I think this is a good initial way to understand things. • We will gradually see more important reasons for using classes and objects going forward. If any of the further information on objects and classes confuses you, just come back to this idea that an object is just a copy of the code in a class. REMEMBER: CLASSES AND OBJECTS 12
  • 14. In the previous exercise, we were again able to use objects and classes to organise our code nicely, such that methods like _________ can be called along with methods from other classes, while still retaining a separation of functionality (as we did in Topic 3). REUSABILITY (1) 14 printTime public class Driver { public static void main(String[] args) { MartinPrinter copyOfMartinPrinter = new MartinPrinter(); copyOfPrintMartin.printMartin(); NumberPrinter copyOfNumberPrinter = new NumberPrinter(); copyOfNumberPrinter.printNumber(191); PrettyTime prettyTime = new PrettyTime(); prettyTime.printTime(); } }
  • 15. REUSABILITY (2) 15 public class Driver1 { public static void main(String[] args) { PrettyTime prettyTime = new PrettyTime(); prettyTime.printTime(); } } public class Driver2 { public static void main(String[] args) { PrettyTime prettyTime = new PrettyTime(); prettyTime.printTime(); } } public class NumberPrinter { public void printNumber(int num) { System.out.println("+------+"); System.out.println("|" + num + "|"); System.out.println("+------+"); PrettyTime prettyTime = new PrettyTime(); prettyTime.printTime(); } } The implicit benefit of this is reusability. We could call in one program, and then use it in another program, simply by making an object of the class again. printTime And of course, we can do the same thing inside any of our classes, even those without a main method.
  • 16. In increasing order of importance (to keep the purists happy): Classes and objects provide us with a way to organise our code (Topic 3). WHY OBJECT-ORIENTATION: SO FAR WE KNOW… 16 Classes and objects provide us with a way in which to reuse our code.
  • 17. 😴 public class MartinPrinter { public static void main(String[] args) { long currentTime = System.currentTimeMillis(); System.out.println("+------+"); System.out.println("|Martin|"); System.out.println("+------+"); System.out.println(System.currentTimeMillis() - currentTime); } } REMEMBER RUNNING TIME? 17 A way to calculate how long it takes for a piece of code to execute. Can we wrap this code in a class in order to make this functionality reusable across different programs? Is it as simple as before?
  • 18. DESIRED DRIVER… 18 public class Driver { public static void main(String[] args) { RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator(); copyOfRunningTimeCalculator.recordCurrentTime(); System.out.println("+------+"); System.out.println("|Martin|"); System.out.println("+------+"); copyOfRunningTimeCalculator.printRunningTime(); } } Sometimes it’s helpful to look at what we want the end product to look like when creating a class: In this case, this is the functionality that should be provided to us by our reusable running time calculator. This is the first time we’ve seen the calling of multiple methods in practice, but hopefully it is intuitive that this can be done.
  • 19. So far we have only seen variable declarations inside methods. So we might approach writing our reusable running time calculator as follows: THE LOCATION OF VARIABLES INSIDE CLASSES (1) 19 Because these variables are close to the method, so to speak, we call them local variables. public class RunningTimeCalculator { public void recordCurrentTime() { long currentTime = System.currentTimeMillis(); } } We record the current time. We then want to reference this value at a later point in time, via a different method call.
  • 20. public class RunningTimeCalculator { public void recordCurrentTime() { long currentTime = System.currentTimeMillis(); } public void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); } } Local variables can only be referenced inside the method in which they are declared. LOCAL VARIABLES: VISIBILITY 20 But we cannot reference it within this one. So we can reference the current time variable within this method. Therefore, local variables are typically used to store temporary values not values that need to be used again, like in this example. (This code will not compile.)
  • 21. The formal way to refer to the visibility of a variable is that variable’s scope. When a variable is in scope it is in our sight and visible. ASIDE: SCOPE 21
  • 22. As a local variable can only be referenced within a method, it is only associated with that method. Therefore, more fundamentally, local variables only exist while the method in which they are defined is being executed. LOCAL VARIABLES: LIFETIME 22 public class RunningTimeCalculator { public void recordCurrentTime() { long currentTime = System.currentTimeMillis(); } public void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); } } Values assigned to this variable will disappear after the method finishes executing. What can we do?
  • 23. We can, if we want to, declare this variable inside the class itself. THE LOCATION OF VARIABLES INSIDE CLASSES (2) 23 We call these variables fields (they correspond, somewhat, to global variables). public class RunningTimeCalculator { private long currentTime; public void recordCurrentTime() { currentTime = System.currentTimeMillis(); } } Why would we do this? By default, we keep fields private to the class. We will return to this idea.
  • 24. public class RunningTimeCalculator { private long currentTime; public void recordCurrentTime() { currentTime = System.currentTimeMillis(); } public void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); } } Fields can be referenced anywhere inside a class. So, if we declare a current time field, we can still assign that field when we set the current time, and then also reference it in order to print the running time. FIELDS: VISIBILITY 24 Indentation helps to confirm field visibility. As with method calls, where a field appears in a class does not affect its visibility. The current time field could appear below the print time method, for example.
  • 25. 🤓 As fields can be referenced anywhere within a class, they are associated with the whole class (rather than just a single method). Therefore fields exist while the class is being `executed’ (rather than while a single method is being executed). What is the equivalent of a class being executed? When it is copied into an object. Ergo, fields exist while an object exists. FIELDS: LIFETIME (1) 25 public class RunningTimeCalculator { private long currentTime; public void recordCurrentTime() { currentTime = System.currentTimeMillis(); } public void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); Values assigned to this field won’t disappear after the method that assigns the value has finished executing.
  • 26. This is tough, let’s see this in practice with our current example… 26
  • 27. FIELDS: LIFETIME (2) 27 public class Driver { public static void main(String[] args) { RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator(); copyOfRunningTimeCalculator.recordCurrentTime(); System.out.println("+------+"); System.out.println("|Martin|"); System.out.println("+------+"); copyOfRunningTimeCalculator.printRunningTime(); } } public class RunningTimeCalculator { private long currentTime; public void recordCurrentTime() { currentTime = System.currentTimeMillis(); } public void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); Let’s see how the lifetime of a field allows our run time calculator to operate as intended.
  • 28. FIELDS: LIFETIME (3) 28 public class Driver { public static void main(String[] args) { RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator(); copyOfRunningTimeCalculator.recordCurrentTime(); System.out.println("+------+"); System.out.println("|Martin|"); System.out.println("+------+"); copyOfRunningTimeCalculator.printRunningTime(); } } The life of the object, and thus the fields it contains, and their stored values. Let’s see how the lifetime of a field allows our run time calculator to operate as intended.
  • 29. FIELDS: LIFETIME (4) 29 And we can use it in any program we like. public class Driver { public static void main(String[] args) { RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator(); copyOfRunningTimeCalculator.recordCurrentTime(); System.out.println("+------+"); System.out.println("|Martin|"); System.out.println("+------+"); copyOfRunningTimeCalculator.printRunningTime(); } } ic class RunningTimeCalculator { rivate long currentTime; ublic void recordCurrentTime() { currentTime = System.currentTimeMillis(); ublic void printRunningTime() { 1234567890 Let’s see how the lifetime of a field allows our run time calculator to operate as intended.
  • 30. 🙈 Let’s see how the lifetime of a field allows our run time calculator to operate as intended. FIELDS: LIFETIME (2) 30 public class Driver { public static void main(String[] args) { RunningTimeCalculator copyOfRunningTimeCalculator = new RunningTimeCalculator(); copyOfRunningTimeCalculator.recordCurrentTime(); System.out.println("+------+"); System.out.println("|Martin|"); System.out.println("+------+"); copyOfRunningTimeCalculator.printRunningTime(); } } public class RunningTimeCalculator { private long currentTime; public void recordCurrentTime() { currentTime = System.currentTimeMillis(); } public void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); } } We’ve moved to a new place in the program, and the field still retains the value assigned previously. 1234567890
  • 31. OUTPUT: REUSABLE RUNNING TIME CALCULATOR 31
  • 32. Can’t we just store everything in fields to be sure? We could, there would be nothing wrong with that from a compilation perspective. But stylistically this may be questionable. • Does it make sense to have a variable visible in every method? • Does it keep our class neat and readable? • What about temporary results (e.g. from calculations) as mentioned previously? ASIDE: DO WE EVER NEED LOCAL VARIABLES, THEN? 32
  • 33. 😴 OPEN QUESTION 1: WHAT IF WE WE USE BOTH VARIABLES? 33 public class RunningTimeCalculator { private long currentTime; public void recordCurrentTime() { long currentTime = System.currentTimeMillis(); } void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); } } What if we — intentionally, accidentally or out of intrigue — decided to use a local variable as well as a field, with the same name, what would happen? Would this code compile? This code does compile because Java has specific rules in this case to avoid name conflict. We will return to this idea.
  • 34. It is probably intuitively clear that we can return variables (or the result of manipulating variables) from a method, rather that just literal values, but we have not yet seen this in practice. We have seen this process for method calls (see Topic 2, Slide 43). OPEN QUESTION 2: RETURNING VARIABLES FROM A METHOD (1) 34 int numberOne = 1; printNumber(numberOne); printNumber(1); public long getRunningTime() { return System.currentTimeMillis() - currentTime; } As such, we could modify the method we saw in the previous example to return the running time rather than just printing it. printRunningTime
  • 35. 😴 Whether you return a value (or a variable) from a method or simply printed it is, again (you guessed it), a style choice. Personally I always try and return values from methods rather than printing them, and do as much printing in my Driver class as possible. • When a method gives you a value back, rather than just printing it, you have the option to do something else with that value, so the class becomes more reusable. • So far I’ve been opting for printing within a method, because we don’t yet have all the syntactic tools we need to always return information from methods. We will return to this idea. OPEN QUESTION 2: RETURNING VARIABLES FROM A METHOD (2) 35
  • 36. 😴 Although we’re using our Driver class simply to house our main method, it is still just a class. Therefore, a valid question is, what do fields look like in this class, and how are they referenced? The answer is pretty much in the same way, only remember our rule: anything that is referenced from main must also be static. Thus, fields in the Driver class must be static. OPEN QUESTION 3: WHAT IF WE ADD FIELDS TO OUR DRIVER CLASS? 36 public class Driver { private static int fieldInDriver; public static void main(String[] args) { fieldInDriver = 1; } } This might not be ideal, for (you’ve guessed it) reasons we will return to.
  • 37. Reason #3: Storing Complex Data 37
  • 38. 🤓 Remember: fields are defined as part of a class (not a particular method) and therefore exist while any objects of that class exists. Because of this, in their simplest form, objects are simply multi- slot variables. We can use the fields inside an object to store multiple pieces of data, rather than a single piece of data, which is what we would do with a primitive variable. OBJECTS AND VARIABLES 38 public class RunningTimeCalculator { private long currentTime;
  • 39. Moreover, recall that a data type describes the format of what a variable will contain. CLASSES AND TYPES 39 int myFirstIntegerVariable; Because classes tell us the format of the fields inside an object it is sensible to view our own classes as custom data types. This idea is reinforced by the style of an object declaration, which is very similar to that of a primitive variable declaration, as we have seen…
  • 40. 😴 RELATIONSHIP: TYPES AND CLASSES; VARIABLES AND OBJECTS 40 Class Object Class Copy Type Variable Value int myFirstIntegerVariable = 1; = new RunningTime Calculator(); RunningTime Calculator copyOfRunningTime Calculator Describes the format and structure of holds Describes the format and structure of holds
  • 41. Let’s think about why we might want to define our own type, and why it might be useful. When programming, it is often useful to store pairs of integers (e.g. a coordinate). But there’s no simple data type that allows us to do this. Let’s define our own data type (a class) that allows us to store pairs of integer. DEFINING OUR OWN DATA TYPE (1) 41
  • 42. What will the fields of a Pair class look like? Which methods will a Pair class need? DEFINING OUR OWN DATA TYPE (2) 42
  • 43. DEFINING OUR OWN DATA TYPE (3) - FIELDS 43 public class Pair { private int valueA; private int valueB; }
  • 44. DEFINING OUR OWN DATA TYPE (4) - METHODS 44 When we want to store data in the different fields of an object, and when we want to get that data back again, we need to do so via methods. public class Pair { private int valueA; private int valueB; public void setPair(int passedValueA, int passedValueB) { valueA = passedValueA; valueB = passedValueB; } public int getValueA() { return valueA; } public int getValueB() { return valueB; } }
  • 45. ic class Driver { ublic static void main(String[] args) { Pair pair = new Pair(); pair.setPair(1, 2); System.out.println(pair.getValueA()); DEFINING OUR OWN DATA TYPE (5) - DRIVER 45 Not that I’ve dropped the `copyOf’ object name prefix. public class Pair { private int valueA; private int valueB; public void setPair(int passedValueA, int passedVal valueA = passedValueA; valueB = passedValueB; } public int getValueA() { return valueA; } public int getValueB() { return valueB; } } 1 2
  • 46. 😴 In this example, our methods are predominantly what we call accessors and mutators. Accessors give us the value from a field back; mutators change the value in that field to something else. These are the simplest type of methods. But it’s important to remember that methods can also perform computation based upon data, as in our first example. ASIDE: ACCESSORS AND MUTATORS 46 public void printRunningTime() { System.out.println(System.currentTimeMillis() - currentTime); }
  • 47. ASIDE: A POTENTIAL OPTIMISATION? (1) 47 public class Pair { private int valueA; private int valueB; public void setPair(int passedValueA, int passedValueB) { valueA = passedValueA; valueB = passedValueB; } public int getValueA() { return valueA; } public int getValueB() { return valueB; } } These variable names (parameters) are informative, but they aren’t particularly readable. It would actually be quite nice to be able to use consistent names. public class Pair { private int valueA; private int valueB; public void setPair(int valueA, int valueB) { valueA = passedValueA; valueB = passedValueB; } public int getValueA() { return valueA; } public int getValueB() { return valueB; } } Earlier, we discussed that this will compile (parameters are just another form of local variable), but naming our parameters in this way necessitates the following update. public class Pair { private int valueA; private int valueB; public void setPair(int valueA, int valueB) { valueA = valueA; valueB = valueB; } public int getValueA() { return valueA; } public int getValueB() { return valueB; } } How do we then determine which variable we want to assign to which?
  • 48. ASIDE: A POTENTIAL OPTIMISATION? (2) 48 If we want to reference fields, but we have a (permitted) naming conflict, we can use the prefix this. public class Pair { private int valueA; private int valueB; public void setPair(int valueA, int valueB) { this.valueA = valueA; this.valueB = valueB; } public int getValueA() { return valueA; } public int getValueB() { return valueB; } } When we write the keyword this, we say to Java `get this object’, and when we write a dot after this, we are also saying `and look for the following:’. So, here we are saying, `get this object, and find the field valueA’ (remember fields are part of the object). This syntax allows us to differentiate between local variables and fields with the same name. Because there’s no conflict here, there is no need for the this notation. But some people choose to write this before all field references, as a style choice (I don’t).
  • 49. 🖥 In the laboratories, write your own data type. It doesn’t have to be too complex, but make sure it has the following… • The appropriate fields • The appropriate methods …also make sure you test your data type using a Driver class. DEFINE YOUR OWN DATA TYPE (1) 49Topic4-1
  • 50. The idea of a class as a data type is exciting, because it suggests that we can have multiple objects of the same class, in the same program, in very much the same way that we can have multiple variables of the same type, in the same program. In other words, our classes are not just reusable across different programs, they’re reusable within the same program. This is probably intuitively clear, but let’s see what that looks like anyway… CLASSES AS TYPES (1): MULTIPLE OBJECTS 50
  • 51. I can have as many Pairs as I like in my program. MULTIPLE INSTANCES OF THE SAME CLASS 51 We call each object a different instance of the class. public class Driver { public static void main(String[] args) { Pair pairA = new Pair(); Pair pairB = new Pair(); } }
  • 52. 🤓 More exciting still, if variables of the same type can hold different values, in the same program, then (the fields of) objects of the same class can hold different values, in the same program. In other words, not only can we reuse classes in the same program, we can manipulate the data in the objects of those classes without affecting other instances. CLASSES AS TYPES (2): MULTIPLE OBJECTS WITH DIFFERENT VALUES 52
  • 53. I can manipulate the values in one Pair instance, without affecting the value in another. MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (1) 53 public class Driver { public static void main(String[] args) { Pair pairA = new Pair(); Pair pairB = new Pair(); pairA.setPair(1, 2); pairB.setPair(3, 4); System.out.println(pairA.getValueA()); System.out.println(pairB.getValueA()); } }
  • 54. I can manipulate the values in one Pair instance, without affecting the value in another. MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (2) 54
  • 55. MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (3) 55 This can often be hard to wrap your head around, because we have only physically written one Pair class, and thus one set of the and fields. But because we make a copy of a class when we use the new keyword, we also have virtual copies of the fields, which can be manipulated separately. valueA valueB
  • 56. MANIPULATING INDIVIDUAL INSTANCES (OBJECTS) OF A CLASS (4) 56 public class Driver { public static void main(String[] args) { Pair pairA = new Pair(); Pair pairB = new Pair(); pairA.setPair(1, 2); pairB.setPair(3, 4); System.out.println(pairA.getValueA()); System.out.println(pairB.getValueA()); } } public class Pair { private int valueA; private int valueB; public void setPair(int passed valueA = passedValueA; valueB = passedValueB; } int getValueA() { return valueA; } public int getValueB() { return valueB; } } pairA public class Pair { private int valueA; private int valueB; public void setPair(int passed valueA = passedValueA; valueB = passedValueB; } int getValueA() { return valueA; One pair class; two copies. 1 2 3 4 pairB
  • 57. The ability for there to be multiple instances (objects) of the same class, each with unique values, is like having a template or a blueprint that can be configured in some way once the object it represents is created. ANOTHER WAY TO THINK ABOUT CLASSES AND OBJECTS… 57 Pair Pair pairA = new Pair(); Pair pairB = new Pair(); pairA.setPair(1, 2); pairB.setPair(3, 4);
  • 58. 🖥 DEFINE YOUR OWN TYPE (2) 58Topic4-2 In the laboratories, play with the new type you’ve just made: • Make different instances of this type. • Change the values in this type.
  • 59. How would we go about defining a Pair if we didn’t have the expressivity of objects and classes? ASIDE: A LANGUAGE WITHOUT OBJECTS AND CLASSES 59 public class Driver { public static void main(String[] args) { int valueAInPair1 = 1; int valueBInPair1 = 2; int valueAInPair2 = 3; int valueBInPair2 = 4; } } With no neat way to collectively associate these values, things become messy. This necessitates complex data types like objects.
  • 60. Because classes are types, we can replace any references to primitive types, with references to classes. Remember our printNumber method? CLASSES AS TYPES (3): CUSTOM TYPES IN METHODS (1) 60 /** * Prints the supplied number surrounded by a box. */ public static void printNumber(int num) { System.out.println("+------+"); System.out.println("|" + num + "|"); System.out.println("+------+"); }
  • 61. We can replace a simple integer parameter with a parameter of Pair type, and extract an element from the pair in order to print it out. CLASSES AS TYPES (3): CUSTOM TYPES IN METHODS (2) 61 /** * Prints the first value of the supplied pair * surrounded by a box. */ public static void printNumber(Pair pair) { System.out.println("+------+"); System.out.println("|" + pair.getValueA() + "|"); System.out.println("+------+"); }
  • 62. CLASSES AS TYPES (3): CUSTOM TYPES IN METHODS (3) 62 We can also return objects of our class from a method, by altering the return type to match our custom type. public class Driver { public static void main(String[] args) { System.out.println(getMeAPair().getValueA()); } public static Pair getMeAPair() { Pair pairA = new Pair(); pairA.setPair(3, 4); return pairA; } } This is one way we can return multiple values from a method while still only returning one thing.
  • 63. 🖥 In the laboratories, play even more with the new type you’ve just made: • Create methods that accept this type as a parameter. • Create methods that return objects of this type. DEFINE YOUR OWN TYPE (1) 63Topic4-3
  • 64. CLASSES AS TYPES (4): CUSTOM TYPES AS FIELDS (1) 64 public class TwoPairs { private Pair firstPair; private Pair secondPair; public void setFirstPair(Pair firstPair) { this.firstPair = firstPair; } public void setSecondPair(Pair secondPair) { this.secondPair = secondPair; } public Pair getFirstPair() { return firstPair; } public Pair getSecondPair() { return secondPair; } } Now that we’ve opened ourselves up to the prospect of replacing any primitive type with one of our own class types, it’s natural that we can have methods inside a class that accept types of other classes, and fields that are able to store objects of this type.
  • 65. 😴 Moreover, this now means that we can have methods inside a class that accept types of that class itself. CLASSES AS TYPES (4): CUSTOM TYPES AS FIELDS (2) 65 public class Pair { private int valueA; private int valueB; public void setPair(Pair pair) { valueA = pair.getValueA(); valueB = pair.getValueB(); } We’ll return to this idea later in the topic.
  • 66. In increasing order of importance (to keep the purists happy): Classes and objects provide us with a way to organise our code (Topic 3). Classes and object provide us with a way in which to reuse our code. WHY OBJECT-ORIENTATION: SO FAR WE KNOW… 66 Objects provide us with an place in which to store complex data; classes define what that data looks like.
  • 68. So far we have used classes to model very technical things: e.g. a running time calculator, a pair data type. • We did this for practical purposes such as organisation, reuse and data storage. This is important, as it shows us very early on how object-oriented programming might actually be used. But it’s also important to learn that object-orientation is designed to be a much larger and expressive tool. In fact, we can write code that models almost anything in the world with object-orientation. Why? Because object-orientation comes from the real world. THE EXPRESSIVITY OF OBJECT-ORIENTED PROGRAMMING (1) 68
  • 69. We all have classes in our mind; shared concepts of things in the world. CLASSES AND OBJECTS IN THE REAL WORLD (1) 69
  • 70. We see objects of these classes every day; real instances of these concepts. CLASSES AND OBJECTS IN THE REAL WORLD (2) 70 The idea of a dog doesn’t just exist in our minds, they exist all around us.
  • 71. The fields of these classes are clear; common features that we can all identify as a part of the shared concept. But fields often hold different data; things in the world are in different states. CLASSES AND OBJECTS IN THE REAL WORLD (3): STATE 71
  • 72. The methods of these classes are clear; common actions that we can all identify as being exhibited by the shared concept. Things in the world exhibit certain behaviours. CLASSES AND OBJECTS IN THE REAL WORLD (4): BEHAVIOUR 72
  • 73. It’s clear to see methods updating fields; actions when performed have some effect on the object. Behaviours affect state. CLASSES AND OBJECTS IN THE REAL WORLD (4): STATE AND BEHAVIOUR 73
  • 74. 🤓 This understanding shows us that not only can we model technical things (like pairs and run time counters), but we can also model real world objects. Why would we do this? • Simulation • Games • Learning. Certain concepts are easier to understand when we aim to model real objects. Let’s try it out… THE EXPRESSIVITY OF OBJECT-ORIENTED PROGRAMMING (2) 74
  • 75. In order to revisit the mind bending notion of a class accepting parameters of itself (Slide 64), let’s aim to model a bank account. MODELLING THE REAL WORLD 75 This is a good example, because it’s a concept from the real world, but we can also imagine our code supporting a real banking system.
  • 76. What’s the state (fields) of a bank account? MODELLING A BANK ACCOUNT (1) 76 Let’s just think about a balance for now. Let’s think about deposit, withdraw and transfer. Which behaviours (methods) can a bank account exhibit?
  • 77. MODELLING A BANK ACCOUNT (2): FIELDS 77 public class BankAccount { private double balance; } We should definitely use a floating point format here as we’ll be dealing with currency.
  • 78. public class BankAccount { private double balance; public void deposit(double amount) { balance = balance + amount; } public void printBalance() { System.out.println(balance); } } MODELLING A BANK ACCOUNT (3): DEPOSIT 78 Some more basic arithmetic, which we will return to, along with the idea of reassignment. It is not enough to simply add a value to the amount, we must also reassign the new value, or the result will be lost.
  • 79. 😴 We’ve now seen the plus (+) symbol used for two different things: • Concatenation (placing things side-by-side in output) ASIDE: ADDING OR CONCATENATION? 79 System.out.println("|" + num + "|"); • Adding numbers together. balance = balance + amount; How does Java know which to do? When the operands either side of the symbol are numeric (and the only things present), Java will perform arithmetic such as addition. (Another) Open question: How does Java know when to concatenate?
  • 80. 🖥 ASIDE: DEFAULT VARIABLE VALUES 80Topic4-4 public class BankAccount { private double balance; public void deposit(double amount) { balance = balance + amount; } void printBalance() { System.out.println(balance); } } You’ll notice here that, the first time we deposit an amount, it looks like we’re adding this amount to nothing, as we haven’t explicitly specified the initial amount in the balance field. In reality this isn't strictly true, as primitive types when they are used as fields are given default values. In this case, the default value given to a double value is 0.0, so our code works as intended. In your laboratory session, investigate the default values prescribed to other primitive types when they are used as fields. We first discussed this in Topic 2.
  • 81. MODELLING A BANK ACCOUNT (4): TESTING DEPOSIT 81 public class Driver { public static void main(String[] args) { BankAccount accountA = new BankAccount(); accountA.deposit(10.0); accountA.printBalance(); } } We’ll investigate further exactly what is going on here shortly.
  • 82. MODELLING A BANK ACCOUNT (5): WITHDRAW 82 public class BankAccount { private double balance; public void deposit(double amount) { balance = balance + amount; } public void printBalance() {} public void withdraw(double amount) { balance = balance - amount; } } I’m going to shorten this method down, because we know what it does (I may do this from time-to- time to focus attention on other methods).
  • 83. MODELLING A BANK ACCOUNT (6): TESTING WITHDRAW 83 public class Driver { public static void main(String[] args) { BankAccount accountA = new BankAccount(); accountA.deposit(10.0); accountA.printBalance(); accountA.withdraw(10.0); accountA.printBalance(); } } Again, we’ll examine what is going on here shortly.
  • 84. MODELLING A BANK ACCOUNT (7): TRANSFER 84 public class BankAccount { private double balance; public void deposit(double amount) { balance = balance + amount; } public void printBalance() { … } public void withdraw(double amount) { balance = balance - amount; } public void transfer(BankAccount otherAccount, double amount) { withdraw(amount); otherAccount.deposit(amount); } } We could just interact with the balance field directly, but why not call the method we already have?
  • 85. 🙈 MODELLING A BANK ACCOUNT (8): TESTING TRANSFER 85 s Driver { ublic static void main(String[] args) { BankAccount accountA = new BankAccount(); accountA.deposit(10.0); accountA.printBalance(); accountA.withdraw(10.0); accountA.printBalance(); accountA.deposit(1000.0); BankAccount accountB = new BankAccount(); accountA.transfer(accountB, 100); accountA.printBalance(); accountB.printBalance(); public class BankAccount { private double balance; public void deposit(double amount) { balance = balance + amount; } public void printBalance() { … } public void withdraw(double amount) { balance = balance - amount; } public void transfer(BankAccount otherAccount, double withdraw(amount); otherAccount.deposit(amount); } } 0.0 0.0 0.0 0.0 0.0 10.0 10.0 10.0 10.0 10.0 accountA 10.0 0.0 0.0 0.0 0.0 0.0 1000.0 1000.0 1000.0 1000.01000.0 900.0 900.0 900.0 900.0 900.0 public class BankAccount { private double balance; public void deposit(double amount) { 0.0 0.0 0.0 0.00.0 100100 accountB public class BankAccount { private double balance; public void deposit(double amount) { accountB 100.00.0 accountB 100.0 100.0 100.0 100.0100.0
  • 86. 🖥 The previous slide had 72 animations in it (!). I’ve placed a video of these animations on KEATS. In the lab, go through the video animation by animation, and see if you can write a simple sentence that describes what is going on each time something moves. Keep this description for revision. You should also write out all the bank account code and annotate it with comments (although you should be doing that anyway). FRAME BY FRAME 86Topic4-5
  • 87. 😴 In the previous topic, you should have discerned that Java is always pass by value. ASIDE: BACK TO PASS-BY-REFERENCE AND PASS-BY-VALUE (1) 87 public class NumberChanger { public static void changeNumber(int changeMe) { changeMe = 2; } public static void main(String[] args) { int numberOne = 1; changeNumber(numberOne); System.out.println(numberOne); } } When a variable is passed to a method, it is effectively copied, such that any interactions with that variable have no effect on the original variable. This will print ‘1’.
  • 88. 😴 class Driver { public static void main(String[] args) { BankAccount accountA = new BankAccount(); accountA.deposit(10.0); accountA.printBalance(); accountA.withdraw(10.0); accountA.printBalance(); accountA.deposit(1000.0); BankAccount accountB = new BankAccount(); accountA.transfer(accountB, 100); accountA.printBalance(); accountB.printBalance(); } } So, then, why is it the case that retains the £100 transferred to it, if only a copy of is passed to ‘s transfer method? ASIDE: BACK TO PASS-BY-REFERENCE AND PASS-BY-VALUE (2) 88 accountB accountB accountA
  • 89. 😴 That’s because, at our current level of abstraction, the rule is slightly different for objects. When an object is passed to a method, any interactions with that object will alter the original object. • Reassigning the variable holding the class copy, however, will not alter the object. Next semester, when we look at objects in memory (i.e. a lower level of abstraction), the reasoning behind this should become clearer. ASIDE: OBJECTS IN MEMORY (2) 89
  • 90. 🖥 In a laboratory, go back and model the real world dog object we showed earlier as a class, or even better, pick your own object to model. • This class should have appropriate fields • This class should have appropriate methods. • Methods should update the values in fields, where appropriate. • You should test this class with a Driver class. MODEL YOUR OWN REAL WORLD OBJECT 90Topic4-6
  • 91. In increasing order of importance (to keep the purists happy): Classes and objects provide us with a way to organise our code (Topic 3). Classes and object provide us with a way in which to reuse our code. Objects provide us with an place in which to store complex data; classes define what that data looks like. WHY OBJECT-ORIENTATION: SO FAR WE KNOW… 91 Objects and classes provide a natural way to conceptualise the world.
  • 93. Once you start writing code on a more permanent basis, it’s inevitable that your code will be used by other people. This is particularly true if you write reusable code, like we have been doing so far. The unfortunate thing about people is that they often do things wrong, break your code and then complain. So, it’s important to control how your code is used. The object-oriented paradigm gives us several ways to control how our code is used. SENDING YOUR CODE OUT INTO THE WORLD 93
  • 94. 😴 We use documentation to help us understand our own code, but remember it’s also a useful tool in helping other people to understand our code. CONTROL 1: DOCUMENTING YOUR CODE (1) 94 Accessible code documentation is something that’s indirectly supported by object-orientation (more later). This doesn’t stop people using your code incorrectly, but reduces the risk of it happening. /** * Prints the supplied number surrounded by a box. */ public static void printNumber(int num) { System.out.println("+------+"); System.out.println("|" + num + "|"); System.out.println("+------+"); }
  • 95. CONTROL 1: DOCUMENTING YOUR CODE (2): CLASS DIAGRAMS 95 What are these? We can also use the expressivity of our class diagram to show fields.
  • 96. In Topic 3, I promised I would discuss what the brackets, appearing after the name of the class you wish to copy are for (we also saw these in the previous diagram). Now is this time. This format looks very much like a method call. BRACKETS WHEN INITIALISING AN OBJECT 96 public class Driver { public static void main(String[] args) { MartinPrinter copyOfMartinPrinter } } new MartinPrinter();=
  • 97. That’s because it is! Every class has a hidden method called a constructor which is called every time you write the new command. HIDDEN METHODS 97 public class Driver { public static void main(String[] args) { BankAccount accountA = new BankAccount(); } } public class BankAccount { private double balance; public BankAccount() { } void deposit(double amount) { The method labels match.
  • 98. CONSTRUCTORS 98 public class BankAccount { private double balance; public BankAccount() { } void deposit(double amount) { These hidden methods are known as implicit constructors because they are called when you construct an object (make a copy of the code in a class). Behind the scenes, they help setup the class for use (more later).
  • 99. CONSTRUCTORS: OUT OF OBSCURITY 99 public class BankAccount { private double balance; public BankAccount() { System.out.println("Oooh I'm being constructed"); } public void deposit(double amount) { Constructors don’t have to be hidden, however, we can actually write them into our classes, and place code into them like a normal method. Any code inside a constructor will therefore be called when the class is first copied. Note how we differentiate a constructor from a normal method: 1. No return type 2. The same name as the class
  • 100. Apart from having no return type and having to have the same name as the class — to ensure they are called when the class is first constructed — constructors are just normal methods. This means we can add parameters to a constructor, if we wish to. PARAMETERS IN CONSTRUCTORS (1) 100 public class BankAccount { private double balance; public BankAccount(int initialDeposit) { System.out.println("Oooh I'm being constructed"); }
  • 101. What is the effect of this? Remember that in order to alter Java’s execution order, you typically have to match a certain pattern (i.e. match the signature of a method). Thus, the current way in which we construct an object, in our bank account example, will no longer work, because we do not use the correct pattern to match the constructor and thus create an object of the class. As such, we cannot make copies of or use the class without giving an initial deposit. PARAMETERS IN CONSTRUCTORS (2) 101
  • 102. PARAMETERS IN CONSTRUCTORS (3) 102 public class BankAccount { private double balance; public BankAccount(int initialDeposit) { System.out.println("Oooh I'm being constructed"); } public class Driver { public static void main(String[] args) { BankAccount accountA = new BankAccount(); } }
  • 103. ERROR: CONSTRUCTOR CANNOT BE APPLIED TO GIVEN TYPES 103
  • 104. PARAMETERS IN CONSTRUCTORS (4) 104 public class BankAccount { private double balance; public BankAccount(int initialDeposit) { System.out.println("Oooh I'm being constructed"); } public class Driver { public static void main(String[] args) { BankAccount accountA = new BankAccount(100); } }
  • 105. 🤓 Therefore, when we place a constructor with parameters into our code, we are able to control how our class is used by forcing a user to provide values when an object is made of that class. Why is this beneficial? Because the code in a constructor is the first thing to execute when an object is made of a class, we can take the data supplied to the constructor and store it in one or more fields, to ensure that any methods that rely on data being in these fields do not cause unexpected or erroneous behaviour. CONTROL 2: CONSTRUCTORS (1) 105
  • 106. 😴 In this scenario, of course, we assume that the user makes a sensible deposit (i.e. a positive number). Open question: How would we discern whether a suitable deposit had been made? CONTROL 2: CONSTRUCTORS (2) 106 public class BankAccount { private double balance; public BankAccount(int initialDeposit) { balance = initialDeposit; } public void withdraw(double amount) { balance = balance - amount; } Here, the assignment of the initial deposit will always be the first thing that happens in our class. So when a withdrawal is made, we know that it is being taken after an initial deposit is made. So, in theory, one cannot take money from an account before putting something in
  • 107. As a more practical example, we could consider adding two parameters to a constructor in our Pair class. CONTROL 2: CONSTRUCTORS (3) 107 It also enables more efficient use of the class, as the setting of the values is effectively combined with the construction of the class. This would avoid a user calling an accessor without first specifying what the pair contains, and receiving a zero value. public Pair(int valueA, int valueB) { Pair pair = new Pair(); pair.getValueA(); Pair pair = new Pair(1, 2); pair.getValueA();
  • 108. There’s also an element of style here. The intelligibility of a class is increased if it’s clear what data that class needs to operate by just looking at the constructor. The constructor is also a good place to initialise fields, and stops us having to initialise on the field itself. • Less chance of inefficient reassignment when programming. • Stylistically nicer. CONTROL 2: CONSTRUCTORS (4) - STYLE (1) 108
  • 109. So, if we wanted to give everyone with a bank account £100 (rather than asking for an initial deposit), we would do it like this… CONTROL 2: CONSTRUCTORS (4) - STYLE (2) 109 …instead of this… private double balance = 100; private double balance; public BankAccount() { balance = 100; } (This is a debated topic.) In general, at this stage, I would not do anything outside a method, except declare fields.
  • 110. 😴 There might be a case in which we want to offer the user the ability to pass information to an object when it is constructed, but not force them to. For example, we might want to give the user the option to specify an initial balance when they create an account or to create an account without specifying this value. ASIDE: RELINQUISHING CONTROL FOR FLEXIBILITY (1) 110 public static void main(String[] args) { BankAccount account = new BankAccount(); BankAccount secondAccount = new BankAccount(100); }
  • 111. 😴 If we want to do this we can specify multiple constructors, each of which accepts different parameters, thus giving a user the option to construct an object of our class in different ways. ASIDE: RELINQUISHING CONTROL FOR FLEXIBILITY (2) 111 private double balance; public BankAccount() {} public BankAccount(double deposit) { balance = deposit; } Each of our constructors has a different pattern, and thus there are different ways to match and create objects of the class. Open question: Why is it ok for us to break our `methods with unique names’ (Topic 3, Slide 14) rule here? Specifically, we use an empty constructor. We can see this as replacing the default constructor we had previously, to remove any restrictions from the creation of objects of our class.
  • 112. You’ve been patient, and now it’s time to talk about public and private. The meaning of these access modifiers should be intuitively clear, given that we’ve been calling code in other classes by creating objects: • Anything that is public in a class can be referenced from any other class that is used as part of your program. • When we look at packaging up classes next semester, we will see how this rule changes. • Anything that is private can only be referenced within the class itself. CONTROL 3: FINALLY, TO PUBLIC AND PRIVATE… 112
  • 113. Given this definition, it makes sense for classes to be public as we want to be able to use our classes in as many different places as possible. But it’s less clear why we want our fields to be private, especially when I show you the following piece of bad but oh so tempting syntax: PUBLIC VS. PRIVATE (1) 113 public static void main(String[] args) { Pair pair = new Pair(); pair.valueA = 1; } public class Pair { public int valueA; We can avoid writing accessor and mutator methods if we make our fields public.
  • 114. public static void main(String[] args) { Pair pair = new Pair(); pair. } Remember our dot syntax? As this allows us to reference any public identifiers, we can also access public variables. PUBLIC VS. PRIVATE (2) 114 public class Pair { public int valueA;
  • 115. 🤓 It’s not always the case that we want users to be able to set the values in fields arbitrarily. For example, let’s imagine that we implement a pin system into our bank account, such that a user must supply the right pin before being allowed to make a withdrawal: WHAT’S WRONG WITH PUBLIC FIELDS? (1) 115
  • 116. If our balance field is public, users can simply interact with it directly, circumventing the pin, and do what they wish. WHAT’S WRONG WITH PUBLIC FIELDS? (2) 116 public class BankAccount { private double balance; public void withdraw(double amount, int pin) { balance = balance - amount; } public BankAccount account = new BankAccount(); account.balance = -10000; (This method is incomplete)
  • 117. 🤓 Instead, we want to force all requests to alter the values in fields to come through the parameters of methods. We do this by making fields private. In this way, we can validate that a request is sensible (e.g. the correct pin is supplied) and doesn’t adversely affect the intended operation of our class, inside the method itself. WHAT’S WRONG WITH PUBLIC FIELDS? (3) 117
  • 118. But we’re currently missing the logic to do this. VALIDATING PARAMETERS INSIDE A METHOD 118 public class BankAccount { private double balance; public void withdraw(double amount, int pin) { } Ensure pin is correct before: If not, print: balance = balance - amount; System.out.println("Ah ah ah, you didn't say the magic word.");
  • 119. 🤓 When we ensure users can only change the data (state) of an object (of a class) through methods, we say that our class has a well defined interface. THE CLASS INTERFACE AND ENCAPSULATION 119 The process of hiding data from our user, and carefully selecting which methods are available to manipulate that data, is known as encapsulation. An interface, in this context, means a point of contact.
  • 120. 😴 What about private in unexpected places? • A private class • A private constructor Are these the only access modifiers available to us? What happens if we don’t write an access modifier? We will return to these questions, mostly in the second semester. ASIDE: OPEN QUESTIONS ABOUT ACCESS MODIFIERS? 120
  • 121. In increasing order of importance (to keep the purists happy): Classes and objects provide us with a way to organise our code (Topic 3). Classes and object provide us with a way in which to reuse our code. Objects provide us with an place in which to store complex data; classes define what that data looks like. Objects and classes provide a natural way to conceptualise the world. WHY OBJECT-ORIENTATION? 121 Object-orientation allows us to control how our code is used.
  • 122. Dr. Martin Chapman programming@kcl.ac.uk martinchapman.co.uk/teaching Programming Practice and Applications (4CCS1PPA) Topic 4: Why Object-Orientation? These slides will be available on KEATS, but will be subject to ongoing amendments. Therefore, please always download a new version of these slides when approaching an assessed piece of work, or when preparing for a written assessment. 122 Thursday 13th October