Methodsandconstructor
Methodsandconstructor
● Objectives
<attributeDeclaration>*
<constructorDeclaration>*
<methodDeclaration>*
– where
● Example
– where,
● public - means that our class is accessible to other classes outside the package
● Coding Guidelines
● Think of an appropriate name for your class. Don't just call your class XYZ or any random names
you can think of.
● Class names should start with a CAPITAL letter.
● The filename of your class should have the SAME NAME as your class name.
● Declaring Attributes
● Instance Variables
– where,
● private here means that the variables are only accessible within the class. Other
objects cannot access these variables directly.
● Coding Guidelines
● Instance variables, like any other variables should start with a SMALL letter.
● Declare instance variables as private so that only class methods can access them directly.
● Declaring Methods
<statement>*
– where,
● Accessor Methods
● Accessor methods
get<NameOfInstanceVariable>
● Example 1
return name;
– where,
● public - means that the method can be called from objects outside the class
● String - is the return type of the method. This means that the method should
return a value of type String
● () - this means that our method does not have any parameters
● Example 2
double result = 0;
result=(mathGrade+englishGrade+scienceGrade)/3;
return result;
● You can have multiple return statements for a method as long as they are not on the same
block.
● Static methods
return studentCount;
– where,
● public- means that the method can be called from objects outside the class
● ()- this means that our method does not have any parameters
● Coding Guidelines
● Always provide documentation before the declaration of the method. You can use javadocs style
for this. Please see example.
/**
*/
return name;
/**
*/
public void setName( String temp ){
name = temp;
/**
* grades
*/
double result = 0;
return result;
/**
*/
return studentCount;
● Overloading Methods
● Method overloading
– allows a method with the same name but different parameters, to have different
implementations and return values of different types
– different parameters
● Constructors
● Constructors are important in instantiating an object. It is a method where all the initializations
are placed.
– A constructor is just like an ordinary method, however only the following information
can be placed in the header of the constructor,
– You cannot call a constructor directly, it can only be called by using the new operator
during class instantiation.
● Constructors
<statement>*
● Default Constructor
– If the class does not specify any constructors, then an implicit default constructor is
created.
● Example
public StudentRecord()
● Overloading Constructors
public StudentRecord(){
}
public StudentRecord(String temp){
this.name = temp;
this.name = name;
this.address = address;
double sGrade){
mathGrade = mGrade;
englishGrade = eGrade;
scienceGrade = sGrade;
● Using Constructors
"Philippines");
● Access Modifiers
– public
– private
– protected
– Default
● The first three access modifiers are explicitly written in the code to indicate the access type, for
the fourth one which is default, no keyword is used.
● default accessibility
● Default access
– specifies that only classes in the same package can have access to the class' variables
and methods
– no actual keyword for the default modifier; it is applied in the absence of an access
modifier.
● Example
int name;
String getName(){
return name;
● public accessibility
● public access
– specifies that class members are accessible to anyone, both inside and outside the class.
– Any object that interacts with the class can have access to the public members of the
class.
– Keyword: public
● Example
return name;
}
● protected accessibility
● protected access
– specifies that the class members are accessible only to methods in that class and the
subclasses of the class.
– Keyword: protected
● Example
return name;
● private accessibility
● private accessibility
– specifies that the class members are only accessible by the class they are defined in.
– Keyword: private
● Example
return name;
}
● Coding Guidelines
● The instance variables of a class should normally be declared private, and the class will just
provide accessor and mutator methods to these variables.
● Summary
● Overloading Methods
● Packages