Classes and Object A Deeper Look Outline 1

Classes and Object: A Deeper Look Outline 1 2 3 4 5 6 7 8 9 10 11 12 Introduction Implementing a Time Abstract Data Type with a Class Scope Controlling Access to Members Referring to the Current Object’s Members with “this” Initializing Class Objects: Constructors Using Overloaded Constructors Using Set and Get Methods Composition Garbage Collection Static Class Members Final Instance Variables 1

2 1 Introduction • Object Oriented Programming (OOP) – Encapsulates data (attributes) and methods (behaviors) • Objects – Allows objects to communicate • Well-defined interfaces, i. e. methods

3 Introduction (cont. ) • Procedural programming language – C is an example – Action-oriented – Functions are units of programming • Object-oriented programming language – Java is an example – Object-oriented – Classes are units of programming • Functions, or methods, are encapsulated in classes

4 1 Introduction (cont. ) • This chapter discusses – How to create objects – How to use objects – You know something about it, but not everything…

2 Implementing a Time Abstract Data Type with a Class • We introduce classes Time 1 and Time. Test – – Time 1. java declares class Time 1 Time. Test. java declares class Time. Test public classes must be declared in separate files Class Time 1 will not execute by itself • Does not have method main • Time. Test, which has method main, creates (instantiates) and uses Time 1 object 5

2 Implementing a Time Abstract Data Type with a Class (cont. ) • Every Java class must extend another class – Time 1 extends java. lang. Object – If class does not explicitly extend another class • class implicitly extends Object • Class constructor – – – Same name as class Initializes instance variables of a class object Called when program instantiates an object of that class Can take arguments, but cannot return values Class can have several constructors, through overloading Class Time 1 constructor Topic 1 6

7 3 Class Scope • Class scope – Class variables and methods – Members are accessible to all class methods – Members can be referenced by name • object. Reference. Name. object. Member. Name – Shadowed (hidden) class variables • this. variable. Name

8 4 Controlling Access to Members • Member access modifiers – Control access to class’s variables and methods – public • Variables and methods accessible to clients of the class – private • Variables and methods not accessible to clients of the class

5 Referring to the Current Object’s Members with this • Keyword this (this reference) – Allows an object to refers to itself Topic 2 9

10 6 Initializing Class Objects: Constructors • Class constructor – Same name as class – Initializes instance variables of a class object – Call class constructor to instantiate object of that class new Class. Name( argument 1, argument 2, …, arugment. N ); • new indicates that new object is created • Class. Name indicates type of object created • arguments specifies constructor argument values

11 7 Using Overloaded Constructors • Overloaded constructors – Methods (in same class) may have same name – Must have different parameter lists Topic 3

12 8 Using Set and Get Methods • Accessor method (“get” method) – public method – Allow clients to read private data • Mutator method (“set” method) – public method – Allow clients to modify private data Topic 4

13 9 Composition • Composition – Class contains references to objects of other classes • These references are members Topic 5

14 10. Garbage Collection • Garbage collection – Returns memory to system – Java performs this automatically • object marked for garbage collection if no references to object • Finalizer method – Returns resources to system – Java provides method finalize • Defined in java. lang. Object • Receives no parameters • Returns void

15 11 Static Class Members • static keyword – static class variable • Class-wide information – All class objects share same data • Access to a class’s public static members – Qualify the member name with the class name and a dot (. ) • e. g. , Math. random() Topic 6

16 12 Final Instance Variables • final keyword – Indicates that variable is not modifiable • Any attempt to modify final variable results in error private final int INCREMENT = 5; • Declares variable INCREMENT as a constant – Enforces principle of least privilege Topic 7

17 A Comprehensive Example • Student class – – – – Name ID Grade for midterm Grade for final Constructors, overloaded Set/get methods Calculate grade etc

18 A Comprehensive Example

19 A Comprehensive Example • Student. Pair class – student 1 – student 2 – group no.

20 A problem to think about Define a class called employee. Each employee object has a name, age, employee number, supervisor (another employee), status (full time or part time) and list of responsibilities, which may grow or shrink with time. We also wish to keep track of how many employees are currently employed and how many employees are full time and how many are part time. Think of possible methods that are likely to be useful.

21 Static variables of employee • What are the variables that we wish to associate with the entire class and not with individual objects representing an employee? • These should be the static variables. • In this case, Num. Full. Time. Employees, Num. Part. Time. Employees are obvious candidates. • We also may want to have a static variable Next. Employee. No.

22 Static variables of employee Num. Full. Time. Employees and Num. Part. Time. Employees should be declared private so that the implementers can decide how these variables may be accessed. Is it also useful to have a variable called Num. Employees? Since we can easily compute it by adding the number of part time and full time employees, it is not needed.

23 Instance variables of employee The obvious instance variables are ; name - a String, age - an int, Employee. Number - an int, supervisor - a reference to another object of class employee, status - a character and List. Responsibilities - an array of objects, each representing a responsibility

24 How to represent a responsibility? A simple solution is to have a class, called say Responsibility with the following instance variables : Job. Name - a String, Percentage. Time - an int When. Started - a variable of the date class Confidential. Evaluation - a String.
- Slides: 24