Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Classes User-Defined Classes and ADTs

Similar presentations


Presentation on theme: "Chapter 8 Classes User-Defined Classes and ADTs"— Presentation transcript:

1 Chapter 8 Classes User-Defined Classes and ADTs
class: reserved word; collection of a fixed number of components Components: members of a class Members accessed by name Class categories/modifiers private protected public

2 Classes private: members of class are not accessible outside class
public: members of class are accessible outside class Class members: can be methods or variables Variable members declared like any other variables

3 Syntax The general syntax for defining a class is:
modifier(s) class ClassIdentifier modifier(s) { classMembers } The general syntax for using the operator new is: new className() //Line 1 OR new className(argument1, argument2, ..., argumentN) //Line 2

4 Classes The syntax to access a data member of a class object or method is: referenceVariableName.memberName Shallow copying: two or more reference variables of the same type point to the same object Deep copying: each reference variable refers to its own object

5 Constructors Guarantee that data members are initialized when object is declared Automatically execute when class object created Name of constructor is name of class More than one constructor can be present in one class Default constructor: constructor without parameters

6 The Copy Constructor Executes when an object is instantiated
Initialized using an existing object Syntax: public ClassName(ClassName otherObject)

7 UML Diagram

8 UML Diagram Top box: name of class
Middle box: data members and their data types Bottom box: member methods’ names, parameter list, return type of method + means public method - means private method # means protected method

9 Example: class Clock myClock = new Clock();
yourClock = new Clock(9,35,15);

10 Example: class Clock

11 Example: class Clock

12 The Method toString public value-returning method Takes no parameters
Returns address of String object Output using print and println methods Default definition creates String with name of object’s class name followed by hash code of object

13 The Modifier static In the method heading, specifies that the method can be invoked by using the name of the class If used to declare data member, data member invoked by using the class name Static data members of class exist even when no object of class type instantiated Static variables are initialized to their default values

14 static Data Members of a Class
Illustrate illusObject1 = new Illustrate(3); Illustrate illusObject2 = new Illustrate(5);

15 Finalizers Automatically execute when class object goes out of scope
Have no parameters Only one finalizer per class Name of finalizer: finalize

16 Creating Packages You can create packages using reserved word package
Define the class to be public (If class is not public it can only be used within package) Choose name for package Organize package (create subdirectories)

17 Creating Package for class Clock
package jpfpatpd.ch08.clockPackage; public class Clock { //put instance variables and methods, as before, here } import jpfpatpd.ch08.clockPackage.Clock;

18 The Reference this Refers to instance variables and methods of a class
Used to implement cascaded method calls

19 Inner Classes Defined within other classes
Can be either a complete class definition or anonymous inner class definition Used to handle events

20 Abstract Data Types Definition:
A data type that specifies the logical properties without the implementation details


Download ppt "Chapter 8 Classes User-Defined Classes and ADTs"

Similar presentations


Ads by Google