Introduction to Classes and Objects Presentation Transcript:
1.Introduction to Classes and Objects
In this Lecture you will learn:
What classes, objects, methods and instance variables are.
How to declare a class and use it to create an object.
How to declare methods in a class to implement the class’s behaviors.
How to declare instance variables in a class to implement the class’s attributes.
How to call an object’s method to make that method perform its task.
The differences between instance variables of a class and local variables of a method.
How to use a constructor to ensure that an object’s data is initialized when the object is created.
The differences between primitive and reference types.
Classes, Objects, Methods and Instance Variables
Declaring a Class with a Method and Instantiating an Object of a Class
Declaring a Method with a Parameter
Instance Variables, set Methods and get Methods
Primitive Types vs. Reference Types
Initializing Objects with Constructors
Floating-Point Numbers and Type double
(Optional) GUI and Graphics Case Study: Using Dialog Boxes
(Optional) Software Engineering Case Study: Identifying the Classes in a Requirements Document
5.Classes, Objects, Methods and Instance Variables
Class provides one or more methods
Method represents task in a program
Describes the mechanisms that actually perform its tasks
Hides from its user the complex tasks that it performs
Method call tells method to perform its task
6.Classes contain one or more attributes
Specified by instance variables
Carried with the object as it is used
7.Declaring a Class with a Method and Instantiating an Object of a Class
Each class declaration that begins with keyword public must be stored in a file that has the same name as the class and ends with the .java file-name extension.
keyword public is an access modifier
Class declarations include:
Pair of left and right braces
Keyword public indicates method is available to public
Keyword void indicates no return type
Access modifier, return type, name of method and parentheses comprise method header
10.Common Programming Error 3.1
Declaring more than one public class in the same file is a compilation error.
11.Java is extensible
Programmers can create new classes
Class instance creation expression
Then name of class to create and parentheses
Calling a method
Object name, then dot separator (.)
Then method name and parentheses
12.Compiling an Application with Multiple Classes
Compiling multiple classes
List each .java file in the compilation command and separate them with spaces
Compile with *.java to compile all .java files in that directory
13.UML Class Diagram for Class GradeBook
UML class diagrams
Top compartment contains name of the class
Middle compartment contains class’s attributes or instance variables
Bottom compartment contains class’s operations or methods
Plus sign indicates public methods
14.Declaring a Method with a Parameter
Additional information passed to a method
Supplied in the method call with arguments
nextLine reads next line of input
next reads next word of input