{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture_03 - ENGR 2200U UOIT/FEAS Fall 2007 ENGR 2710U OO...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design 1 Implementing Classes Professor Saman Kumarawadu ENGR 2710U Fall 2010 – Saman Kumarawadu ENGR 2710U OO Programming and Design Chapter Goals To become familiar with the process of implementing classes To be able to implement simple methods To understand the purpose and use of constructors To understand how to access instance fields and local variables To appreciate the importance of documentation comments
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Instance Variables An object stores its data in instance variables • An instance of a class is an object of the class. Instance variable is a storage location of each object of the class. Instance variable declaration includes: An access specifier Type of the instance variable The name of the instance variable ENGR 2710U OO Programming and Design Syntax 3.1: Instance Variable Declaration Syntax accessSpecifier class ClassName { accessSpecifier typeName variableName; ……….. } Example: public class Counter { private int value; } Instance variable is always private Each object of this class has separate copy of this instance variable
Background image of page 2
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Instance Variables: Example public class Student { private String name; // Student's name. private double test1, test2, test3; // Grades on three tests. public double getAverage() { // compute average test grade return (test1 + test2 + test3) / 3; } } // end of class Student Special statement that terminates the method call and return a result to the method’s caller. ENGR 2710U OO Programming and Design Student std, std1,std2, std3; // Declare four variables type Student. std = new Student(); // Create a new object belonging to Student class and store a reference to that object in the variable std. std1 = new Student(); // Create a second Student object and store a reference to it in the variable std1. std2 = std1; // Copy the reference value in std1 into the variable std2. std3 = null; // Store a null reference in the variable std3. std.name = "John Smith"; // Set values of some instance variables. std1.name = "Mary Jones"; // (Other instance variables have default initial values of zero.)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
ENGR 2200U UOIT/FEAS Fall 2007 © Mike Eklund ENGR 2710U OO Programming and Design Access specifier private The variables defined with access specifier private can be accessed only by the methods in the same class, but not by any other methods. Hiding instance variables by making them private Continued… ENGR 2710U OO Programming and Design Encapsulation A black box: electronic control module of a car Hides its inner workings Encapsulation: the process of hiding implementation details while publishing an interface What is the right concept for each particular black box?
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}