Introduction Object Oriented Programming OOP Encapsulates data attributes

Introduction • Object Oriented Programming (OOP) �Encapsulates data (attributes) and methods (behaviors) ▴ Objects �Allows objects to communicate • • ▴ Well-defined interfaces 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

Time Abstract Data Type • • Abstract Data Types (ADTs) �Encapsulate methods and data �Simulate real-world entities 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

Figure 8. 3: Member. Access. Test. java • Errors because fields are private

Figure 8. 3: This. Test. java • • • Referring to Current Object's members Keyword this (this reference) �Allows an object to refers to itself I personally think this should have been called my or possibly mine

Implementing the Time ADT • • 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 �A default constructor is used �Can take arguments, but cannot return values �Class can have several constructors, through overloading ▴ Example in Figure 8. 5: Time 2. java ▴ Class Time 2 constructor (lines 12 -15)

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 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

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 Overloaded constructors �Methods (in same class) may have same name �Must have different parameter lists

Using set and get methods • • Allow well-defined ways to interact with object Accessor method (“get” method) �public method �Allow clients to read private data �Not all data needs to be seen Mutator method (“set” method) �public method �Allow clients to modify private data �Not all data needs to be changed �Enforce proper values are set Example �Figure 8. 7: Date. java

Composition • • • Class contains references to objects of other classes �These references are members This class 'has a' widget Example �Figure 8. 8: Employee. java ▴ Employee 'has a' Date ▴ Employee 'has a' String

Enumerations • Uses the enum keyword �Comma separated list of symbolic names �Are static and final (more later) �Can be used like integers (switch/case) ▴ Example in Figure 6. 9: Craps. java �Can be defined in a class ▴ Example in Figure 8. 10: Book. java (and Figure 8. 11)

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 �Exact execution is noneterministic ▴ Example is in Figure 8. 12

Static Class Members • • static keyword �static class variable ▴ Class-wide information � All class objects share same data �Methods can also be static Access to a class’s public static members �Qualify the member name with the class name and a dot (. ) ▴ e. g. , Math. PI Only use in special cases �Example in Figure 8. 12: Employee. java One can import static members and use like locals �See example in Figure 18. 14: Static. Import. Test. java

Final Instance Variables • final keyword �Indicates that variable is not modifiable (assigned to once) ▴ 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 See example in Figure 8. 15: Increment. java

Software Reusability • Java �Framework for achieving software reusability �Rapid applications development (RAD) ▴ e. g. , creating a GUI application quickly

Data Abstraction and Encapsulation • • Information hiding ▴ Developer creates � Hides implementation details from clients ▴ Data abstraction � Abstract data types (ADTs) Example: Stack data structure �Last in-first out (LIFO) • ▴ Push places objects on the stack ▴ Pop removes objects from the stack Example ADT: Queue �Line at grocery store �First-in, first-out (FIFO) ▴ Enqueue to place objects in queue ▴ Dequeue to remove object from queue ▴ Enqueue and dequeue hide internal data representation

Packages • • Creating Packages �Use the package keyword �Package access ▴ Variable or method does not have member access modifier Using Packages �We can import packages into programs ▴ ▴ Group of related classes and interfaces Help manage complexity of application components Facilitate software reuse Provide convention for unique class names � Popular package-naming convention • com. deitel, java. awt, etc
- Slides: 16