Академический Документы
Профессиональный Документы
Культура Документы
Classes
Sang Shin
Michle Garoche
www.javapassion.com
Learn with Passion!
1
Objectives
At the end of the lesson, the students should be able to:
Defining Your
Own Class
3
where
where,
Coding Guidelines
Demo:
Exercise 1: Create your class
1014_javase_createclass.zip
Instance Variables
vs.
Static Variables
9
Instance Variables
This means that they have the same value for all the
object instances in the same class.
10
Class Variables
For example,
11
Instance Variables
12
13
Instance Variables
public class StudentRecord {
// Instance variables
private String
name;
private String
address;
private int
age;
private double mathGrade;
private double englishGrade;
private double scienceGrade;
private double average;
//we'll add more code here later
}
where,
private here means that the variables are only accessible within
the class. Objects of other classes cannot access these
variables directly. We will cover more about accessibility later.
14
Encapsulation
15
Static Variables
16
17
Methods
18
Declaring Methods
where,
Accessor methods
20
where,
() - this means that our method does not have any parameters
21
name;
// some code
// An example in which the business logic is
// used to return a value on an accessor method
public double getAverage(){
double result = 0;
result=(mathGrade+englishGrade+scienceGrade)/3;
return result;
}
}
22
Mutator Methods
23
where,
void - means that the method does not return any value
24
25
26
Static Methods
27
Static methods
public class StudentRecord {
private static int studentCount;
public static int getStudentCount(){
return studentCount;
}
}
where,
29
Computation method
30
name;
private String
address;
private int
age;
31
32
33
Student record
= new StudentRecord();
= new StudentRecord();
= new StudentRecord();
instance method
static method
34
Program Output
Anna
Student Count = 0
35
Demo:
Exercise 2: Static/Instance variables &
Static/Instance methods
1014_javase_createclass.zip
36
Overloading
Methods
37
Method Overloading
Method overloading
39
Example
public static void main( String[] args )
{
StudentRecord annaRecord = new StudentRecord();
annaRecord.setName("Anna");
annaRecord.setAddress("Philippines");
annaRecord.setAge(15);
annaRecord.setMathGrade(80);
annaRecord.setEnglishGrade(95.5);
annaRecord.setScienceGrade(100);
//overloaded methods
annaRecord.print( annaRecord.getName() );
annaRecord.print( annaRecord.getEnglishGrade(),
annaRecord.getMathGrade(),
annaRecord.getScienceGrade());
40
Output
Demo:
Exercise 3: Overloading Methods
1014_javase_createclass.zip
42
Constructors
(Constructor
Methods)
43
Constructors
44
Constructors
45
46
47
48
annaRecord=
new StudentRecord("Anna");
StudentRecord
beahRecord=
new StudentRecord("Beah",
"Philippines");
StudentRecord
crisRecord=
new StudentRecord(80,90,100);
50
Example
1: public StudentRecord(){
2:
this("some string");
3:
4: }
5:
6: public StudentRecord(String temp){
7:
this.name = temp;
8: }
9:
10: public static void main( String[] args )
11: {
12:
13:
StudentRecord
annaRecord = new StudentRecord();
14: }
51
Demo:
Exercise 4: Constructor
1014_javase_createclass.zip
52
this Reference
53
this reference
You can only use the this reference for instance variables
and NOT static variables
this reference
55
Example
public void setAge( int age ){
this.age = age;
}
56
Demo:
Exercise 5: this
1014_javase_createclass.zip
57
Access Modifiers
58
Access Modifiers
protected
default
59
public accessibility
public access
Any object that interacts with the class can have access
to the public members of the class.
Keyword: public
60
61
protected accessibility
protected access
Keyword: protected
62
63
default accessibility
Default access
64
Example
public class StudentRecord {
//default access to instance variable
int name;
//default access to method
String getName(){
return name;
}
}
65
private accessibility
private accessibility
Keyword: private
66
name;
67
68
Coding Guidelines
69
Demo:
Exercise 6: Access Modifiers
1014_javase_createclass.zip
70
Summary
Method overloading
Packages
Thank you!
72