Beruflich Dokumente
Kultur Dokumente
Declaring Fields
Instance Variables
Declaring Methods
Overloading Methods
Declaring Constructors
"ac#ages
Importing "ac#ages
To create class&
'( Thin# on where you will be using your class and how
your class will be used(
)( Thin# of an appropriate name for the class
*( +ist all the information or properties that you want your
class to contain
,( +ist down the methods that you will be using for your
class(
Introduction to Programming 1 5
Defining your own classes
The value of these variables are the same for all the ob3ects
of the same class(
+et2s now list down the set of functionalities that we want our
class to have(
$ccessor Methods
Mutator Methods
So% given the previous list% we can have the following names
for our accessor and mutator methods%
name . get-ame% set-ame
write address . get$ddress% set$ddress
english grade . get7nglish@rade% set7nglish@rade
math grade . getMath@rade% setMath@rade
science grade . getScience@rade% setScience@rade
average grade . get$verage@rade
studentCount . getStudentCount
Introduction to Programming 1 17
Declaring Metods:
!ccessor " Mutator Metods
The statement%
return name;
in our program signifies that it will return the value of the
instance variable name to the calling method(
-OT7& The return type of the method should have the same
data type as the data in the return statement(
Introduction to Programming 1 19
Declaring Metods:
Multiple return state#ents
7Aample&
public String getNumberInWords( int num ){
String defaultNum = "zero";
if( num == 1 ){
return "one"; //return a constant
}
else if( num == 2){
return "two"; //return a constant
}
//return a variable
return defaultNum;
}
Introduction to Programming 1 21
Declaring Metods:
!ccessor Metods
The statement%
name = temp;
assigns the value of temp to name and thus changes the
data inside the instance variable name(
-OT7& 5ou can only use the this reference for instance
variables and -OT static or class variables(
Introduction to Programming 1 32
'(erloading Metods
Method overloading
0ather than invent new names all the time% method overloading can
be used when the same operation has different implementations(
Introduction to Programming 1 33
'(erloading Metods:
Sa#ple )rogra#
1 /**
2 * overloaded method print() - one parameter
3 */
4 public void print( String temp )
5 {
6 System.out.println("Name:" + name);
7 System.out.println("Address:" + address);
8 System.out.println("Age:" + age);
9 }
10
11 /**
12 * overloaded method print() - three parameters
13 */
14 public void print(double eGrade,double mGrade,double sGrade)
15 {
16 System.out.println("Name:" + name);
17 System.out.println("Math Grade:" + mGrade);
18 System.out.println("English Grade:" + eGrade);
19 System.out.println("Science Grade:" + sGrade);
20 }
Introduction to Programming 1 34
'(erloading Metods:
Sa#ple )rogra#
different parameters
"roperties of a constructor&
'( Constructors have the same name as the class
)( $ constructor is 3ust li#e an ordinary method% however only the
following information can be placed in the header of the constructor%
scope or accessibility identifier li#e public(((!% constructor2s name
and parameters if it has any(
*( Constructors does not have any return value
,( 5ou cannot call a constructor directly% it can only be called by using
the new operator during class instantiation(
Introduction to Programming 1 38
Declaring Constructors:
Te default constructor
7Aample&
public StudentRecord() {
//some code here
}
Introduction to Programming 1 39
Declaring Constructors:
Constructor o(erloading
7Aample&
1 public StudentRecord(){
2 //some initialization code here
3 }
4
5 public StudentRecord(String temp){
6 this.name = temp;
7 }
8
9 public StudentRecord(String name, String address){
10 this.name = name;
11 this.address = address;
12 }
Introduction to Programming 1 40
Declaring Constructors:
Constructor o(erloading
7Aample&
The first statement imports the specific class Color while the
other imports all of the classes in the 3ava(awt pac#age(
import java.awt.Color;
import java.awt.*;
Introduction to Programming 1 46
)ac,ages:
Creating your own pac,ages
Steps&
'( Create a folder named SchoolClasses(
)( Copy all the classes that you want to belong to this pac#age inside
this folder(
*( $dd the following code at the top of the class file(
package SchoolClasses;
public class StudentRecord{
. . . .
Introduction to Programming 1 47
)ac,ages:
Creating your own pac,ages
public
private
protected
default(
Introduction to Programming 1 49
!ccess Modifiers:
default access
default access
This specifies that only classes in the same pac#age can have
access to the class2 variables and methods(
7Aample&
public class StudentRecord {
//default access to instance variable
int name;
//default access to method
String getName(){
return name;
}
}
Introduction to Programming 1 50
!ccess Modifiers:
public access
$ny ob3ect that interacts with the class can have access to
the public members of the class(
7Aample&
public class StudentRecord {
//public access to instance variable
public int name;
//public access to method
public String getName(){
return name;
}
}
Introduction to Programming 1 51
!ccess Modifiers:
protected access
7Aample&
public class StudentRecord {
//protected access to instance variable
protected int name;
//protected access to method
protected String getName(){
return name;
}
}
Introduction to Programming 1 52
!ccess Modifiers:
pri(ate access
7Aample&
public class StudentRecord {
//private access to instance variable
private int name;
//private access to method
private String getName(){
return name;
}
}
Introduction to Programming 1 53
!ccess Modifiers:
Coding Guidelines
Declaring Methods
Overloading Methods
Declaring Constructors
"ac#ages