Classes Revision
CST200 – Week 4: Midterm revision

Instructor: Andreea Molnar
Outline
• Classes
• Example
• Constructor
• Encapsulation
Classes
In object oriented programming classes
represent objects with different
characteristics (attributes, data) and
fun...
Example
Person
Characteristics: name, social security
number
Functionality: sleeps, walks
Example
Person
Attributes: name, social security number
Operations: sleeps, walks
Example
Person
Data: name, social security number
Methods: sleeps, walks
Example
Person
Data: define the state of the object
Methods: define the behavior of the object
Example
public class Person {

private String name;

Data declaration

private String socialSecurityNo;

public void sleep...
Example
public class Person {
private String name;

private String socialSecurityNo;

Instance variables –
variables creat...
Constructor
•
•
•

Creates (and initializes) an object

Similar to a method that has:

•
•

the same name as the class

no...
Constructor
public class Test {
public static void main(String[] args) {
Person p = new Person();
}
}

The default
constru...
Constructor
public class Person {
private String name;
private String socialSecurityNo;
public Person(String name, String ...
Constructor
public class Test {
public static void main(String[] args) {
Person p = new Person("Mary", "078-05-1120");
}
}
Constructor
public class Test {
public static void main(String[] args) {
Person p1 = new Person("Mary", "078-05-1120");

P...
Encapsulation
•
•

Hides the data and implementation details
of an object
Protects the data integrity by making it
difficu...
Encapsulation
•

Uses visibility modifiers (e.g. private) to
deny access
private String name;
private String socialSecurit...
Encapsulation

Allows access to instance variables through
methods: accessor method (getter) and
mutator method (setter)
Encapsulation
Accessor method (getter) – returns the
variable value.
private String name;
public String getName() {
return...
Encapsulation
Accessor method (getter) – returns the
variable value.
public class Test {
public static void main(String[] ...
Encapsulation
Mutator method (setter) – changes the
variable value
private String name;
public void setName(String name) {...
Encapsulation
Mutator method (setter) – changes the variable value
public class Test {
public static void main(String[] ar...
Summary
• Initialize instance variables in the
constructor

• Make the instance variables private
unless there is a good r...
Upcoming SlideShare
Loading in …5
×

Classes revision

1,196 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,196
On SlideShare
0
From Embeds
0
Number of Embeds
797
Actions
Shares
0
Downloads
14
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Classes revision

  1. 1. Classes Revision CST200 – Week 4: Midterm revision Instructor: Andreea Molnar
  2. 2. Outline • Classes • Example • Constructor • Encapsulation
  3. 3. Classes In object oriented programming classes represent objects with different characteristics (attributes, data) and functionality (operations, methods).
  4. 4. Example Person Characteristics: name, social security number Functionality: sleeps, walks
  5. 5. Example Person Attributes: name, social security number Operations: sleeps, walks
  6. 6. Example Person Data: name, social security number Methods: sleeps, walks
  7. 7. Example Person Data: define the state of the object Methods: define the behavior of the object
  8. 8. Example public class Person { private String name; Data declaration private String socialSecurityNo; public void sleeps() { } public void walks() { } } Method declaration
  9. 9. Example public class Person { private String name; private String socialSecurityNo; Instance variables – variables created at the class level Each instance of a class (object) has its own instance variables/data space public void sleeps() { } public void walks() { } } Each instance of a class (object) share the method definitions
  10. 10. 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).
  11. 11. Constructor public class Test { public static void main(String[] args) { Person p = new Person(); } } The default constructor is called
  12. 12. Constructor public class Person { private String name; private String socialSecurityNo; public Person(String name, String socialSecurityNo) { this.name = name; Constructor this.socialSecurityNo = socialSecurityNo; } public void sleeps() { } public void walks() { } } this is a reference to the current object
  13. 13. Constructor public class Test { public static void main(String[] args) { Person p = new Person("Mary", "078-05-1120"); } }
  14. 14. 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"); } } name name Mary John socialSecurityNo socialSecurityNo 078-05-1120 078-05-1121 Each instance of a class (object) has its own instance variables/data space.
  15. 15. Encapsulation • • Hides the data and implementation details of an object Protects the data integrity by making it difficult to have unauthorized access
  16. 16. Encapsulation • Uses visibility modifiers (e.g. private) to deny access private String name; private String socialSecurityNo; name and socialSecurityNo can be referenced only within Person class public class Test { public static void main(String[] args) { Person p = new Person("Mary", "078-05-1120"); p.name = "Alice"; } } error
  17. 17. Encapsulation Allows access to instance variables through methods: accessor method (getter) and mutator method (setter)
  18. 18. Encapsulation Accessor method (getter) – returns the variable value. private String name; public String getName() { return name; }
  19. 19. Encapsulation Accessor 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()); } } Will print Mary p name Mary socialSecurityNo 078-05-1120
  20. 20. Encapsulation Mutator method (setter) – changes the variable value private String name; public void setName(String name) { this.name = name; }
  21. 21. Encapsulation Mutator 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 Mary p p.setName("Alice"); System.out.println(p.getName()); //will print Alice } } name Mary Alice socialSecurityNo 078-05-1120
  22. 22. 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

×