Professional Documents
Culture Documents
Oop D1
Oop D1
OOP
OBJECTS AS A PROGRAMMING CONCEPT
• In Java, an OBJECT is an INSTANCE of a CLASS. We can make lots of OBJECTS from the
same CLASS. Each OBJECT requires some memory to store it's details. Each OBJECT
has it's own copy of its VARIABLES and its own copy of the class METHODS.
• But, if the METHODS or VARIABLES are STATIC, then there is only one copy of the
method or variable, and all the OBJECTS share the same copy .
STEPS IN OBJECT CREATION
• From the example you can see, you can call a Constructor method with no parameter values being
given. In that case, the object would be created with whatever default values are stated in the
constructor method that has no parameters. There can be, and often is, more than one
constructor in every class template.
JAVA’S IMPLEMENTATION OF OOP
• As projects contain multiple classes (driver & providers), it is important to show the relationship
between two objects.
• This is a basic example where class A1 (driver) has a main method in which an object of class B1
(provider) is instantiated.
MULTIPLE CLASS DIAGRAMS IN ONE PROJECT
INTERPRET UML DIAGRAMS
Principal Secretary
All of the people who work at your
school are employees. But there are
different types of employee, so we can
decompose employee.
PRACTICE WITH THESE
• Employers
• Traffic simulation model
• Calculator
• Calendar
• Media Collections
RELATIONSHIPS
• Association is the most generic relationship. The other three are more specific and are used in
particular situations.
DEPENDENCY
• We use a dependency relationship to show when one element depends on another element.
• It points from the independent entity to the dependent entity in the system.
• In the figure below, an object of Player class is dependent (or “uses”) an object of Bat class.
ASSOCIATION
• Association is relation between two separate classes which establishes through their Objects.
• Association can be one-to-one, one-to-many, many-to-one, many-to-many.
Java Program
Example- Association
ASSOCIATION VS DEPENDENCY
• We only represent the ones which are essential to convey for understanding the system.
• We need to understand that every association implies a dependency itself.
• However, we prefer not to draw it separately.
AGGREGATION
• Inheritance is the mechanism by which one class is allow to inherit the features (states
and behaviours) of another class.
• Super Class: The class whose features are inherited is known as super class (or a base
class or a parent class).
• Sub Class: The class that inherits the other class is known as sub class (or a derived class,
extended class, or child class). The subclass can add its own states and behaviours in
addition to the superclass states and behaviours.
class Bicycle //Base Class class MountainBike extends Bicycle //Derived
{ class
public int gear; {
public int speed; public int seatHeight;
public Bicycle(int gear,int speed) public MountainBike(int gear,int speed,int
{ startHeight)
this.gear=gear;
{
this.speed=speed;
} super(gear,speed);
public void applyBrake(int decrement) seatHeight=startHeight;
{ }
speed-=decrement; public void setHeight(int newValue)
} {
public void speedUp(int increment) seatHeight=newValue;
{ }
speed+=increment;
public String toString()
}
public String toString() {
{ return(super.toString()+"\nseat height
return ("No of gears are is "+seatHeight);
"+gear+"\n"+"speed of bicycle is "+speed); }
} }
}
end loop
end method
a is a parameter - it allows us to pass a value into the procedure for it to use. Running the following
code would create a multiplication table of 50 up to 5.
multi(50)
The 50 here is called the argument - because it is a value passed in.
parameter
PARAMETER VS ARGUMENT
argument
DECLARING METHODS IN JAVA
No input parameter,
but function returns a value
No input
parameter,
no return
value – void
• Consider the following Java program that passes a primitive type to function.
public class test
{
public static void main(String []args)
{
int x=5;
change(x);
System.out.println(x);
}
public static void change(int x)
{
x=10; OUTPUT ?
}
}
• We pass an int to the method change() and as a result the change
in the value of that integer is not reflected in the main method.
• Java creates a copy of the variable being passed in the method and then
do the manipulations.
• Hence the change is not reflected in the main method.
D1.10 Describe how data items can be
passed to and from actions as parameters.
Passing primitives
▪ We try to add one to i within the method
addOneToThisInt but it doesn't work
▪ Outside the method, the value of i is unchanged
▪ This is because i itself is not passed to the
method, only a copy of i's value
▪ We can change the copy as much as we like, but
the original i doesn't get changed
▪ Analogy: You want access to an important
document that is locked within my filing cabinet. I
make a photocopy of it and give it to you. You can
tear it up, burn it, whatever you like, but my copy
is safe.
▪ (The way to change i's value is to return the
changed variable, and assign the return value to i,
as in returnThisIntWithOneAdded)
Describe how data items can be passed to
and from actions as parameters.
Passing object references
▪ This time we pass to the method a reference to an
object and try to change the object
▪ The object's id gets changed, both inside the method
and outside!
▪ Analogy: You want access to an important document
that is locked within my filing cabinet. I give you the
key to the filing cabinet. If you change the document,
you have changed the only copy.
▪ Some people mistakenly think that this means that
Java is both pass by value (primitives) and pass by
reference (objects)
▪ See the next slide for the truth…
Describe how data items can be passed to
and from actions as parameters.
The truth about Java
▪ Like before we pass to the method a reference to
an object, but this time we try to change the
reference itself by making it point to a totally
different object!
▪ This fails. The reference outside the method still
points to the original object
▪ Analogy: You want access to an important
document that is locked within my filing cabinet. I
give you a copy of the key to the filing cabinet. If
you change the document, you have changed the
only copy, but if you try to re-grind the key (or
something), you have not changed my key.
▪ Hence, whether primitives are passed or object
references are passed, Java is always pass by
value.
Describe how data items can be passed to
and from actions as parameters.
▪ This is an advanced point for
teachers, that may help with fielding
a difficult question from students… ▪ Compare this with the "passing object
references" slide
▪ String is an object type, but you can't change the
object by passing its reference to a method!?
▪ This is because it is an immutable type
▪ Java provides a few immutable types for very
common object types such as String and the
primitive wrapper classes
▪ In fact, it is recommended by software design
gurus that you should always make your classes
immutable where possible
▪ Not providing any setter methods is one way of
doing this!
←Huh?!
REFERENCES