Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.

Similar presentations


Presentation on theme: "Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static."— Presentation transcript:

1 Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static parts define what objects in the class look like. Every class is automatically in existence when the program runs.

2 Classes and Objects An object is an instance of a class, and is created using the new operator. The non-static part of the class defines what each object looks like. Many instances (objects) can be created from a class … no limit except reality An object contains information and functionality of a “thing”, e.g., Account, Vehicle, Employee, etc.

3 Classes’ and Objects’ Components Classes (and thus also objects) are composed of methods and data values Data values store information Methods do things, and also have their own local data

4 Graphical Representation Account The class name appears on top of the icon. An icon for a class is the rectangle. Account SV129 The class name is placed inside the object icon. The object’s name appears on top of the icon. An icon for an object is the rounded rectangle.

5 Instance-of Relationship Employee Bill Employee Steve Employee Andy The class name can be omitted since it is clear which class these objects belong to. The dotted line shows the instance-of relationship. Before you can create instances of a class, the class must be defined.

6 Visibility Modifiers: public and private The modifiers public and private designate the accessibility of objects’ and class’ data values and methods If a component is declared private, nothing outside the class can access it. If a component is declared public, anything outside the class can access it.

7 In general, be private (military demotion) Make class components private whenever you can This supports the notion of encapsulation, which makes for more robust software development

8 Class and Instance Data Values A class data value (indicated by the static modifier) is used to maintain information shared by all instances or aggregate information about the instances. An instance data value is used to maintain information specific to individual instances. Make instance data values private always

9 Sample Data Values Account SV129 Account SV506 Account SV008 current balance 908.55 1304.98 354.00 Account minimum balance 100.00 There is one copy of minimum balance for the whole class and shared by all instances. All three Account objects possess the same instance data value current balance.

10 Primitive and Reference Data Values Primitive variables contain values Reference variables point at objects byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type

11 Methods Methods have code (to do stuff) and data A method defined for a class is called a class method (indicated by the static modifier) and a method defined for an object is called an instance method.

12 Messages To instruct a class or an object to do something, we a message to one of its methods Values passed to a method when sending a message are called arguments or parameters of the message. The (formal) parameters of a method are local variables that receive the message parameters Methods can return one data value to the calling method

13 Sending a Message Message deposit with the argument 250.00 is sent to chk-008. Account chk-008 deposit deposit 250.00 Message name is usually omitted in the diagram. deposit 250.00

14 Getting an Answer This message has no argument. Account chk-008 getMonthlyFee monthly fee The method returns the value monthly fee back to the message sender.

15 Calling a Class Method Account getAverageBalance average balance The average balance of all accounts is returned.

16 Program Components A Java file is composed of comments, import statements, and class declarations.

17 Files and Classes A Java program file ends with.java There must be one public class per file It must have the same name as the file One public class (i.e., one file) must have the main method

18 Simple Java Programs Simple Java programs can be written in just the one file, containing One public class (with the main method) Other class methods and final data values as required Such programs do not create any objects, but simply run class methods (starting with the main method) and use primitive data.


Download ppt "Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static."

Similar presentations


Ads by Google