classes revision
TRANSCRIPT
Classes
In object oriented programming classes
represent objects with different
characteristics (attributes, data) and
functionality (operations, methods).
Example
public class Person {
private String name;
private String socialSecurityNo;
public void sleeps() {
}
public void walks() {
}
}
Data declaration
Method declaration
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
Constructor
• Creates (and initialize) an object
• Similar to a method that has:
• the same name as a class
• no return type
• Each class has a default constructor that
accepts no parameters (this is generated
only if no explicit constructor is provided).
Constructor
public class Test {
public static void main(String[] args) {
Person p = new Person();
}
}
The default
constructor is called
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
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");
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
Encapsulation
• Hides the data and implementation details
of an object
• Protects the data integrity by making it
difficult to have unauthorized access
Encapsulation
Uses visibility modifiers (e.g. private) to
deny access
private 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
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 name;
}
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());
}
}Mary
078-05-1120
name
socialSecurityNo
p
Will print Mary
Encapsulation
Mutator method (setter) – changes the
variable value
private String name;
public void setName(String name) {
this.name = name;
}
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.setName("Alice");
System.out.println(p.getName()); //will print Alice
}
}
Mary Alice
078-05-1120
name
socialSecurityNo
p