Lecture 1
Lecture 1
Programming II
Lecture 1: Introduction
Instructor
Email:
[email protected]
Text Books
• Example:
public static void main(String[] args) {
…
}
Object Oriented Programming (OOP)
Fundamentals of OOP
• Classes and Objects
• Abstraction
• Encapsulation
• Inheritance
• Polymorphism (Overriding and
Overloading)
Classes & Objects
11
Examples of Objects
12
What is a Class?
• Group of Objects with similar
– properties (attributes)
– behavior
– relationships to other objects
13
Classes and Objects
Person Objects
Vehicle Objects
Polygon Objects
Polygon Class
Abstract Attributes: Vertices, Border,
Into Color, FillColor
Operations: Draw(), Erase(), Move()
15
Abstraction
Abstraction
• An abstraction is a view or representation of an
entity that includes only the most significant
attributes.
• “The process of reducing an object to its
essence so that only the necessary elements are
represented. Abstraction defines an object in
terms of its properties (attributes), behaviors
(functionality), and interface (means of
communicating with other objects)”
17
Abstraction Example
18
Encapsulation
Encapsulation
• The technique of hiding the internal implementation detail of an
object from it’s external views.
20
Inheritance
Inheritance
– Object-oriented programming allows classes to inherit
commonly used state and behavior from other
classes.
22
Inheritance - Example
• Define Person to be a class
– A Person has attributes, such as name, age, height, gender
– Assign values to attributes when describing object
23
Polymorphism
Polymorphism
• Polymorphic which means “many forms” has Greek
roots.
– Poly – many
– Morphos - forms.
25
Polymorphism – Method Overloading
• Multiple methods can be defined with the same name,
different input arguments (different signature).
Method 1 - initialize(int a)
Method 2 - initialize(int a, int b)
26
Advantages of OOP
• simplicity: software objects model real world objects, so the
complexity is reduced and the program structure is very clear
• Declaration syntax:
type name;
– Example:
// each Student object has a name and gpa field
public class Student {
String name;
double gpa;
}
Accessing fields
• Other classes can access/modify an object's fields.
– access: variable.field
– modify: variable.field = value;
• Example:
Point p1 = new Point();
Point p2 = new Point();
System.out.println("the x-coord is " + p1.x); // access
p2.y = 13; // modify
A class and its client
• Point.java is not, by itself, a runnable program.
– A class can be used by client programs.
PointMain.java (client program) Point.java (class of objects)
public class PointMain { public class Point {
main(String args) { int x;
Point p1 = new Point(); int y;
p1.x = 7; }
p1.y = 2;
inde 0 1 2
points x
value null null null
Two-phase initialization
1) initialize the array itself (each element is initially null)
2) initialize each element of the array to be a new object
Output: inde 0 1 2 3 4
word is: null x
Exception in thread "main" value null null null null null
java.lang.NullPointerException
at Example.main(Example.java:8)
Looking before you leap
• You can check for null before calling an object's
methods.
String[] words = new String[5];
words[0] = "hello";
words[2] = "goodbye"; // words[1], [3], [4] are null
Example:
public void shout() {
System.out.println("HELLO THERE!");
}
Mutator method questions
• Write a method setLocation that changes a
Point's location to the (x, y) values passed.
// desired behavior
System.out.println("p is " + p); // p is (10, 7)
The toString method
tells Java how to convert an object into a String
Point@9e8c34
toString syntax
public String toString() {
code that returns a String representing this object;
}
– Example:
// Returns a String representing this Point.
public String toString() {
return "(" + x + ", " + y + ")";
}
- // Now
System.out.println("p is " + p); // p is (10, 7)
Object initialization: constructors
Initializing objects
• Currently it takes 3 lines to create a Point and
initialize it:
Point p = new Point();
p.x = 3;
p.y = 8; // tedious
...
}
Tracing a constructor call
• What happens when the following call is made?
Point p1 = new Point(7, 2);
p1 x 7 y 2
– This declares local variables with the same name as the fields,
rather than storing values into the fields. The fields remain 0.