0% found this document useful (0 votes)
25 views

Methodsandconstructor

The document discusses how to create classes in Java. It covers defining classes with attributes and methods, using access modifiers, declaring constructors and overloading methods and constructors. It provides examples of creating a StudentRecord class with attributes like name and grade, accessor methods like getName(), overloaded constructors, and using the class in a main method. The goal is to teach how to properly structure classes and control access to members.

Uploaded by

Mary Ann Magleo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Methodsandconstructor

The document discusses how to create classes in Java. It covers defining classes with attributes and methods, using access modifiers, declaring constructors and overloading methods and constructors. It provides examples of creating a StudentRecord class with attributes like name and grade, accessor methods like getName(), overloaded constructors, and using the class in a main method. The goal is to teach how to properly structure classes and control access to members.

Uploaded by

Mary Ann Magleo
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

● 10 Creating your own Classes

● Objectives

At the end of the lesson, the student should be able to:

● Create their own classes

● Declare attributes and methods for their classes

● Use the this reference to access instance data

● Create and call overloaded methods

● Import and create packages

● Use access modifiers to control access to class members

● Defining your own classes

● To define a class, we write:

<modifier> class <name> {

<attributeDeclaration>*

<constructorDeclaration>*

<methodDeclaration>*

– where

● <modifier> is an access modifier, which may be combined with other types of


modifier.

● Example

public class StudentRecord {


//we'll add more code here later

– where,

● public - means that our class is accessible to other classes outside the package

● class - this is the keyword used to create a class in Java

● StudentRecord - a unique identifier that describes our class

● 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

● To declare a certain attribute for our class, we write,

<modifier> <type> <name> [= <default_value>];

● Instance Variables

public class StudentRecord {

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. Other
objects cannot access these variables directly.

● Coding Guidelines

● Declare one variable for each line.

● Instance variables, like any other variables should start with a SMALL letter.

● Use an appropriate data type for each variable you declare.

● Declare instance variables as private so that only class methods can access them directly.

● Class (static) variables

public class StudentRecord {

//instance variables we have declared

private static int studentCount;

//we'll add more code here later


}

– we use the keyword static to indicate that a variable is a static variable.

● Declaring Methods

● To declare methods we write,

<modifier> <returnType> <name>(<parameter>*) {

<statement>*

– where,

● <modifier> can carry a number of different modifiers

● <returnType> can be any data type (including void)

● <name> can be any valid identifier

● <parameter> ::= <parameter_type> <parameter_name>[,]

● Accessor Methods

● Accessor methods

– used to read values from our class variables (instance/static).

– usually written as:

get<NameOfInstanceVariable>

– It also returns a value.

● Example 1

public class StudentRecord {

private String name;

public String getName(){

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

● getName - the name of the method

● () - this means that our method does not have any parameters

● Example 2

public class StudentRecord {

private String name;

public double getAverage(){

double result = 0;

result=(mathGrade+englishGrade+scienceGrade)/3;

return result;

● Multiple return statements

● You can have multiple return statements for a method as long as they are not on the same
block.

● You can also use constants to return values instead of variables.

● Static methods

public class StudentRecord {

private static int studentCount;

public static int getStudentCount(){

return studentCount;

– where,

● public- means that the method can be called from objects outside the class

● static-means that the method is static and should be called by


typing,[ClassName].[methodName]. For example, in this case, we call the
method StudentRecord.getStudentCount()
● int- is the return type of the method. This means that the method should return
a value of type int

● getStudentCount- the name of the method

● ()- this means that our method does not have any parameters

● Coding Guidelines

● Method names should start with a SMALL letter.

● Method names should be verbs

● Always provide documentation before the declaration of the method. You can use javadocs style
for this. Please see example.

● Source Code for class StudentRecord

public class StudentRecord {

private String name;

private String address;

private int age;

private double mathGrade;

private double englishGrade;

private double scienceGrade;

private double average;

private static int studentCount;

● Source Code for class StudentRecord

/**

* Returns the name of the student

*/

public String getName(){

return name;

/**

* Changes the name of the student

*/
public void setName( String temp ){

name = temp;

● Source Code for class StudentRecord

/**

* Computes the average of the english, math and science

* grades

*/

public double getAverage(){

double result = 0;

result = ( mathGrade+englishGrade+scienceGrade )/3;

return result;

/**

* returns the number of instances of StudentRecords

*/

public static int getStudentCount(){

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

– can be used when the same operation has different implementations.

● Always remember that overloaded methods have the following properties:

– the same name

– different parameters

– return types can be different or the same

● Constructors
● Constructors are important in instantiating an object. It is a method where all the initializations
are placed.

● The following are the properties of a constructor:

– Constructors have the same name as the class

– A constructor is just like an ordinary method, however only the following information
can be placed in the header of the constructor,

– scope or accessibility identifier (like public...), constructor's name and parameters if it


has any.

– Constructors does not have any return value

– You cannot call a constructor directly, it can only be called by using the new operator
during class instantiation.

● Constructors

● To declare a constructor, we write,

<modifier> <className> (<parameter>*) {

<statement>*

● Default Constructor

● The default constructor

– is the constructor without any parameters.

– If the class does not specify any constructors, then an implicit default constructor is
created.

● Example

public StudentRecord()

//some code here

● Overloading Constructors

public StudentRecord(){

//some initialization code here

}
public StudentRecord(String temp){

this.name = temp;

public StudentRecord(String name, String address){

this.name = name;

this.address = address;

public StudentRecord(double mGrade, double eGrade,

double sGrade){

mathGrade = mGrade;

englishGrade = eGrade;

scienceGrade = sGrade;

● Using Constructors

● To use these constructors, we have the following code,

public static void main( String[] args ){

//create three objects for Student record

StudentRecord annaRecord=new StudentRecord("Anna");

StudentRecord beahRecord=new StudentRecord("Beah",

"Philippines");

StudentRecord crisRecord=new StudentRecord(80,90,100);

//some code here

● Access Modifiers

● There are four different types of member access modifiers in Java:

– 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

public class StudentRecord {

//default access to instance variable

int name;

//default access to method

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

public class StudentRecord {

//default access to instance variable

public int name;

//default access to method

public String getName(){

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

public class StudentRecord {

//default access to instance variable

protected int name;

//default access to method

protected String getName(){

return name;

● private accessibility

● private accessibility

– specifies that the class members are only accessible by the class they are defined in.

– Keyword: private

● Example

public class StudentRecord {

//default access to instance variable

private int name;

//default access to method

private String getName(){

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

● Defining your own classes

● Declaring Fields (instance, static/class)

● Declaring Methods (accessor, mutator, static)

● Returning values and Multiple return statements

● The this reference

● Overloading Methods

● Constructors (default, overloading, this() call)

● Packages

● Access Modifiers (default, public, private, protected)

You might also like