0% found this document useful (0 votes)
15 views30 pages

02 OOP Concepts

This document discusses object-oriented programming concepts in Java, including objects, classes, data types, primitive vs object types, and examples of object types like String and Scanner. It explains that objects contain both data fields and methods. Classes are templates that define objects. Objects are constructed using the new keyword and a class name. Object variables store references to the objects. Methods can be called on objects using dot notation. The toString() method allows objects to be converted to strings.

Uploaded by

kemii1704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views30 pages

02 OOP Concepts

This document discusses object-oriented programming concepts in Java, including objects, classes, data types, primitive vs object types, and examples of object types like String and Scanner. It explains that objects contain both data fields and methods. Classes are templates that define objects. Objects are constructed using the new keyword and a class name. Object variables store references to the objects. Methods can be called on objects using dot notation. The toString() method allows objects to be converted to strings.

Uploaded by

kemii1704
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

02.

OOP Concepts
Objects

2
Data types

 data type: A category of data values.


 Example: integer, real number, string

 Java data types are divided into two sets:


 primitive types: Java's 8 built-in simple data types for
numbers, text characters, and logic.
 boolean, char, byte, short, int, long,
float, double
 object types: All other types!
 e.g., Scanner, System, String, Math

3
Object types

 So far, we have seen:


 variables, which represent data (categorized by types)
 methods, which represent behavior

 object: An variable that contains data and behavior.


 There are variables inside the object, representing its data.
 There are methods inside the object, representing its behavior.

 class:
 Basic building block of Java programs (what we have seen so far)
AND
 Data types for objects

4
Example object types

 Theoretical examples:
 A class Person could represent objects that store a name,
height, weight, hair color, IQ, etc…
 A class Laptop could represent objects that store speed,
screen size, color, dimensions, brand, etc…

 Examples from Java:


 The class String represents objects that store text
characters.
 The class Scanner represents objects that can tokenize
streams of characters.

5
Point objects

6
Point object

 Data stored in each Point object:


Field name Description
x the point's x-coordinate
y the point's y-coordinate

 Useful methods in each Point object:


Method name Description
distance(p) how far away the point is from point p
setLocation(x, y) sets the point's x and y to the given values
translate(dx, dy) adjusts the point's x and y by the given amounts

7
Constructing objects

 construct: To create a new object.


 Objects are constructed with the new keyword.

 Constructing objects, general syntax:


<class> <name> = new <class>(<arguments>);

 Examples:
Point p = new Point(7, -4);
Scanner console = new Scanner(System.in);

 Q: Wait a minute! Why don’t we construct strings with new?


 A1: Strings are one of the most commonly used objects, so they
have special syntax (quotation marks) to simplify their construction.
 A2: Also, you can if you want to: String s = new String(“hi”);
8
Point object: Construction

 Constructing a Point object, general syntax:


Point <name> = new Point(<x>, <y>);
Point <name> = new Point(); // the origin, (0, 0)

 Examples:
Point p1 = new Point(5, -2);
Point p2 = new Point();

9
Representing objects

 So far, I have drawn primitive types like this:

int x = 7;

X: 7
Representing objects

 I will represent object types like this:

Point p = new Point(7,3);


Fields

p:
x: y:
7 3
Variable,
with slot in Reference, a
memory Data, in another
pointer to the part of memory
object’s data
References

 Object variables are references to the location in


memory where their data resides.
 We draw references as pointers.
 Actually, p stores the address of the location in memory
where its data is.
Fields

p:
x: y:
7 3
Variable, with
slot in
Reference, a
memory Data, in another
pointer to the
part of memory
object’s data
Constructing an object

 What happens when the following call is made?


Point p1 = new Point(7, 2);

p1: x: 7 y: 2

13
null objects

 null is a value for all object types that says,


“this object is a reference to nothing at all”
 We draw null objects with a slash
 Note that null objects
have no memory reserved for their data!

Point p1 = null;
p1:

14
Calling methods on objects

 Since the methods are bundled in the objects, calling these


methods requires specifying which object we are talking to.
 Calling a method of an object, general syntax:
<variable>.<method name>(<parameters>)
 The results may vary from one object to another.

 Examples:
String s1 = “Homey da Clown”;
String s2 = “Bubbles the clown”;
System.out.println(s1.length()); // prints 14
System.out.println(s2.length()); // prints 17

15
Calling methods on objects

 Since the methods are bundled in the objects, calling these


methods requires specifying which object we are talking to.
 Calling a method of an object, general syntax:
<variable>.<method name>(<parameters>)
 The results may vary from one object to another.

 Examples:
Point p0 = new Point(0, 0);
Point p1 = new Point(7, 3);
Point p2 = new Point(2, -2);
System.out.println(p1.distance(p0)); // 7.62
System.out.println(p2.distance(p0)); // 2.83

16
Dereferencing

 When we use the “.” operator on an object, we access


the stuff (methods and/or data) that the object references
(or points to).

 This is called dereferencing.

 The “.” operator is the dereferencing operator.


Three kinds of method calls

 We have seen three ways to call methods:


Type: No ‘.’ used <class>.<method> <variable>.<method>
When it’s used: For methods For static methods For non-static or
defined in the defined in another instance methods
same class as class defined in another
they’re called class
Examples: myMethod(); Math.max(a,b) myString.length()
Integer.parseInt(“6”) console.nextInt()
Server.gcd(15,12) myPoint.translate(2,2)
Common Programming Error

 The dreaded NullPointerException

Point p = null;
p.x = 7; // Error!
p.setLocation(0,0); // Error!

 If you try to dereference a null object, it will cause a


NullPointerException. Why?
 This is a very common error, but one nice thing about Java
is that it is often fairly easy to fix this kind of error (in my
experience).
Point class
Point class
state:
int x, y
behavior:
distance(Point p)
equals(Point p)
setLocation(int x, int y)
toString()
translate(int dx, int dy)

Point object #1 Point object #2 Point object #3


state: state: state:
int x, y int x, y int x, y
behavior: behavior: behavior:
distance(Point p) distance(Point p) distance(Point p)
equals(Point p) equals(Point p) equals(Point p)
setLocation(int x, int y) setLocation(int x, int y) setLocation(int x, int y)
toString() toString() toString()
translate(int dx, int dy) translate(int dx, int dy) translate(int dx, int dy)

20
instances

 Definition: An object is an instance of its class.

 Each instance has its own local copy of the state and
behavior defined in the class template.

 Example: each Point object has its own x,y coordinates,


and its own setLocation method.
CS163 Fall 2018

Methods (toString, equals)


The toString() method

 tells Java how to convert an object into a String


 called when an object is printed or concatenated to a
String:
Point p = new Point(7, 2);
System.out.println(”p: " + p);

 Same as:
System.out.println("p: " + p.toString());
 Every class has a toString(), even if it isn't in your code.
 The default is the class's name and a hex (base-16) hash-code:

Point@9e8c34
toString() implementation

public String toString() {


code that returns a suitable String;
}

 Example: toString() method for our Student class:


public String toString(){
return ”name: " + name+ "\n"
+ ”id: " + id + "\n"
+ ”average: " + average;
}

 // SHOW Eclipse example of Student class


toString in ArrayLists

 ArrayList<Student> students = new ArrayList<>();


…
 System.out.println(students);

 println(students) calls students.toString(), which


automatically calls s.toString() for every points

// SHOW Eclipse example of Student class


Primitive Equality

 Suppose we have two integers i and j


 How does the statement i==j behave?
 i==j if i and j contain the same value
Object Equality

 Suppose we have two pet instances pet1 and pet2


 How does the statement pet1==pet2 behave?
Object Equality
 Suppose we have two pet instances pet1 and pet2
 How does the statement pet1==pet2 behave?
 pet1==pet2 is true if both refer to the same object
 The == operator checks if the addresses of the two
objects are equal
 May not be what we want!
Object Equality - extended

 If you want a different notion of equality define your


own .equals() method.
 Use pet1.equals(pet2) instead of pet1==pet2
 The default definition of .equals() is the value of ==
but for Strings the contents are compared
.equals for the Pet class

public boolean equals (Object other) {


if (!other instanceof Pet) {
return false;
}
Pet otherPet = (Pet) other;
return ((this.age == otherPet.age)
&&(Math.abs(this.weight – otherPet.weight) < 1e-8)
&&(this.name.equals(otherPet.name)));
}

You might also like