classes revision

22
Classes Revision CST200 – Week 4: Midterm revision Instructor: Andreea Molnar

Upload: asu-online

Post on 17-May-2015

966 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Classes revision

Classes Revision

CST200 – Week 4: Midterm revision

Instructor: Andreea Molnar

Page 2: Classes revision

Outline

•Classes

•Example

•Constructor

•Encapsulation

Page 3: Classes revision

Classes

In object oriented programming classes represent objects with different characteristics (attributes, data) and functionality (operations, methods).

Page 4: Classes revision

Example

Person

Characteristics: name, social security number

Functionality: sleeps, walks

Page 5: Classes revision

Example

Person

Attributes: name, social security number

Operations: sleeps, walks

Page 6: Classes revision

Example

Person

Data: name, social security number

Methods: sleeps, walks

Page 7: Classes revision

Example

Person

Data: define the state of the object

Methods: define the behavior of the object

Page 8: Classes revision

Example

public class Person {

private String name;

private String socialSecurityNo;

public void sleeps() {

}

public void walks() {

}

}

Data declaration

Method declaration

Page 9: Classes revision

Examplepublic class Person {

private String name;

private String socialSecurityNo;

public void sleeps() {

}

public void walks() {

}

}

Instance variables – variables created at

the class level

Each instance of a class (object) share the method

definitions

Each instance of a class (object) has its own instance

variables/data space

Page 10: Classes revision

Constructor

•Creates (and initializes) an object

•Similar to a method that has:

• the same name as the class

•no return type

•Each class has a default constructor that accepts no parameters (this is generated only if no explicit constructor is provided).

Page 11: Classes revision

Constructor

public class Test {

public static void main(String[] args) {

Person p = new Person();

}

}

The default constructor is

called

Page 12: Classes revision

Constructorpublic class Person {

private String name;

private String socialSecurityNo;

public Person(String name, String socialSecurityNo) {

this.name = name;

this.socialSecurityNo = socialSecurityNo;

}

public void sleeps() {

}

public void walks() {

}

}

Constructor

this is a reference to the current

object

Page 13: Classes revision

Constructor

public class Test {

public static void main(String[] args) {

Person p = new Person("Mary", "078-05-1120");

}

}

Page 14: Classes revision

Constructor

public class Test {

public static void main(String[] args) {

Person p1 = new Person("Mary", "078-05-1120");

Person p2 = new Person(“John", "078-05-1121");

}

}Mary

078-05-1120

John

078-05-1121

Each instance of a class (object)

has its own instance

variables/data space.

name name

socialSecurityNo

socialSecurityNo

Page 15: Classes revision

Encapsulation

•Hides the data and implementation details of an object

•Protects the data integrity by making it difficult to have unauthorized access

Page 16: Classes revision

Encapsulation•Uses visibility modifiers (e.g. private) to

deny accessprivate String name;

private String socialSecurityNo;

public class Test {

public static void main(String[] args) {

Person p = new Person("Mary", "078-05-1120");

p.name = "Alice";

}

}

name and socialSecurityNo can be referenced

only within Person class

error

Page 17: Classes revision

Encapsulation

Allows access to instance variables through methods: accessor method (getter) and mutator method (setter)

Page 18: Classes revision

Encapsulation

Accessor method (getter) – returns the variable value.

private String name;public String getName() {

return name;}

Page 19: Classes revision

EncapsulationAccessor method (getter) – returns the variable value.

public class Test {public static void main(String[] args) {

Person p = new Person("Mary", "078-05-1120");

System.out.println(p.getName());}

}Mary

078-05-1120

name

socialSecurityNo

p

Will print Mary

Page 20: Classes revision

Encapsulation

Mutator method (setter) – changes the variable value

private String name;public void setName(String name) {

this.name = name;}

Page 21: Classes revision

EncapsulationMutator method (setter) – changes the variable value

public class Test {public static void main(String[] args) {

Person p = new Person("Mary", "078-05-1120");System.out.println(p.getName()); //will print Maryp.setName("Alice");System.out.println(p.getName()); //will print Alice

}}

Mary Alice

078-05-1120

name

socialSecurityNo

p

Page 22: Classes revision

Summary

•Initialize instance variables in the constructor

•Make the instance variables private unless there is a good reason to do otherwise

•Allow access to instance variables through setter and getter methods