Docsity

Classes and Objects, Schemes and Mind Maps of Object Oriented Programming

Partial preview of the text

Download Classes and Objects and more Schemes and Mind Maps Object Oriented Programming in PDF only on Docsity!

Classes and Objects

EECS2030 B: Advanced

Object Oriented Programming

Fall 2018

CHEN-WEI WANG

Separation of Concerns: App/Tester vs. Model

● In EECS1022:

○ (^) Model Component : One or More Java Classes e.g., Person vs. SMS, Student, CourseRecord ○ (^) Another Java class that “manipulates” the model class (by creating instances and calling methods): ● (^) Controller (e.g., BMIActivity, BankActivity). Effects? Visualized (via a GUI) at connected tablet ● (^) Tester with main (e.g., PersonTester, BankTester). Effects? Seen (as textual outputs) at console

● In Java:

○ We may define more than one classes. ○ Each class may contain more than one methods.

object-oriented programming in Java:

○ (^) Use classes to define templates ○ Use objects to instantiate classes ○ (^) At runtime, create objects and call methods on objects, to simulate interactions between real-life entities.

Object-Oriented Programming (OOP)

● In real life, lots of entities exist and interact with each other.

e.g., People gain/lose weight, marry/divorce, or get older. e.g., Cars move from one point to another. e.g., Clients initiate transactions with banks.

● Entities:

○ (^) Possess attributes; ○ (^) Exhibit bebaviour ; and ○ (^) Interact with each other.

● Goals: Solve problems programmatically by

○ (^) Classifying entities of interest Entities in the same class share common attributes and bebaviour. ○ Manipulating data that represent these entities Each entity is represented by specific values.

OO Thinking: Templates vs. Instances (1.1)

A person is a being, such as a human, that has certain

attributes and behaviour constituting personhood: a person

ages and grows on their heights and weights.

● A template called Person defines the common

○ attributes (e.g., age, weight, height) [≈ nouns] ○ (^) behaviour (e.g., get older, gain weight) [≈ verbs]

OO Thinking: Templates vs. Instances (2.1)

Points on a two-dimensional plane are identified by their signed

distances from the X- and Y-axises. A point may move

arbitrarily towards any direction on the plane. Given two points,

we are often interested in knowing the distance between them.

● A template called Point defines the common

○ (^) attributes (e.g., x, y) [≈ nouns] ○ (^) behaviour (e.g., move up, get distance from) [≈ verbs]

OO Thinking: Templates vs. Instances (2.2)

● Points share these common attributes and behaviour.

○ Each point possesses an x-coordinate and a y-coordinate. ○ Each point’s location might be distinct e.g., p1 is located at ( 3 , 4 ) e.g., p2 is located at (− 4 , − 3 )

● Each point, depending on the specific values of their attributes

(i.e., locations), might exhibit distinct behaviour:

○ (^) When p1 moves up for 1 unit, it will end up being at ( 3 , 5 ) ○ (^) When p2 moves up for 1 unit, it will end up being at (− 4 , − 2 ) ○ (^) Then, p1’s distance from origin: [

32 + 52 ]

○ (^) Then, p2’s distance from origin: [

(− 4 )^2 + (− 2 )^2 ]

OOP: Classes ≈ Templates

In Java, you use a class to define a template that enumerates

attributes that are common to a set of entities of interest.

public class Person { int age; String nationality; double weight; double height; }

public class Point { double x; double y; }

OOP:

Define Constructors for Creating Objects (1.1)

● Within class Point, you define constructors , specifying how

instances of the Point template may be created.

public class Point {

... /* attributes: x, y */ Point(double newX, double newY) { x = newX; y = newY; } }

● In the corresponding tester class, each call to the Point

constructor creates an instance of the Point template.

public class PointTester { public static void main(String[] args) { Point p1 = new Point (2, 4); println( p1 .x + " " + p1 .y); Point p2 = new Point (-4, -3); 11 of 73^ println( p2 .x^ +^ "^ "^ +^ p2 .y);^ }^ }

OOP:

Define Constructors for Creating Objects (2.1)

● Within class Person, you define constructors , specifying how

instances of the Person template may be created.

public class Person {

... /* attributes: age, nationality, weight, height */ Person(int newAge, String newNationality) { age = newAge; nationality = newNationality; } }

● In the corresponding tester class, each call to the Person

constructor creates an instance of the Person template.

public class PersonTester { public static void main(String[] args) { Person jim = new Person (50, "British"); println( jim .nationlaity + " " + jim .age); Person jonathan = new Person (60, "Canadian"); println( jonathan .nationlaity + " " + jonathan .age); } }

OOP:

Define Constructors for Creating Objects (2.2)

Person jim = new Person(50, "British");

1. RHS (Source) of Assignment : new Person(50, "British")

creates a new Person object in memory.

50 “British”

age nationality

Person

weight height

2. LHS (Target) of Assignment : Point jim declares a variable

that is meant to store the address of some Person object.

3. Assignment : Executing = stores new object’s address in jim.

50 “British”

age nationality

Person

jim

weight 14 of 73^ height

Visualizing Objects at Runtime (2.1)

After calling a constructor to create an object:

Person jim = new Person(50, "British", 80, 1.8);

nationality “British”

Person

jim

weight

height

age 50

Visualizing Objects at Runtime (2.2)

After calling an accessor to inquire about context object jim:

double bmi = jim.getBMI();

● Contents of the object pointed to by jim remain intact.

● Retuned value ( 180. 8 ) 2 of jim.getBMI() stored in variable bmi.

nationality “British”

Person

jim

weight

height

age 50

Visualizing Objects at Runtime (2.4)

After calling the same accessor to inquire the modified state of

context object jim:

bmi = p.getBMI();

● Contents of the object pointed to by jim remain intact.

● Retuned value 90

( 1. 8 )^2 of^ jim.getBMI()^ stored in variable^ bmi.

nationality “British”

Person

jim

weight

height

age 50

The this Reference (1)

● Each class may be instantiated to multiple objects at runtime.

class Point { double x; double y; void moveUp(double units) { y += units; } }

● Each time when we call a method of some class, using the dot

notation, there is a specific target/context object.

1 Point p1 = new Point(2, 3); 2 Point p2 = new Point(4, 6); 3 p1.moveUp(3.5); 4 p2.moveUp(4.7);

○ p1 and p2 are called the call targets or context objects. ○ (^) Lines 3 and 4 apply the same definition of the moveUp method. ○ (^) But how does Java distinguish the change to p1.y versus the change to p2.y?