CISC6795 Spring 11 Fordham Univ. Introduction to Classes and Objects 1.
01. Introduction to Classes and Objects
-
Upload
lutfi-alief -
Category
Documents
-
view
220 -
download
0
Transcript of 01. Introduction to Classes and Objects
-
8/8/2019 01. Introduction to Classes and Objects
1/25
Introduction to Classes and
Objects
Ridwan Rismanto, S.ST
-
8/8/2019 01. Introduction to Classes and Objects
2/25
Declaring Class
GradeBook class
Display a welcome message
Receive course name as an argument and displaying
it as the part of welcome message
Store the course name in GradeBook object
Using methods to set and obtain the course name
Initialization using constructor
GradeBookTest class
Has the main method to use GradeBook class
-
8/8/2019 01. Introduction to Classes and Objects
3/25
Declaring Class (2)
Each class declaration that begins with
keyword public must be stored in a file that
has the same name as the class, andends with .java extension.
-
8/8/2019 01. Introduction to Classes and Objects
4/25
GradeBook class
-
8/8/2019 01. Introduction to Classes and Objects
5/25
GradeBookTest class
-
8/8/2019 01. Introduction to Classes and Objects
6/25
Inside the GradeBook class
Keyword public is an access modifier
Keyword void indicates that a method
doesnt have a return value Class GradeBook isnt an application
because it doesnt have a main method
It will be error if you try to execute
-
8/8/2019 01. Introduction to Classes and Objects
7/25
Inside the GradeBookTest class
This class is an application because it has mainmethod that will be executed first by the JVM
The main method is special because it is static
Allows us to call the method without creating anobject
In this application, we call GradeBooksdisplayMessage method.
Keyword new creates a new object of the class The objects method can be called using dot
separator (.)
-
8/8/2019 01. Introduction to Classes and Objects
8/25
UML
Graphical representation scheme for
modelling object oriented systems
Class name Class attributes
Class operations (methods)
-
8/8/2019 01. Introduction to Classes and Objects
9/25
UML for GradeBook class
-
8/8/2019 01. Introduction to Classes and Objects
10/25
Class GradeBook declaration with
one method that has a parameter
-
8/8/2019 01. Introduction to Classes and Objects
11/25
-
8/8/2019 01. Introduction to Classes and Objects
12/25
Output
-
8/8/2019 01. Introduction to Classes and Objects
13/25
Inside the GradeBookTest class
A scanner named input for reading the
coursename from user
A variable passing From users input to variable nameOfCourse
And then passing it to displayMessage
method
-
8/8/2019 01. Introduction to Classes and Objects
14/25
Updated UML for GradeBook class
-
8/8/2019 01. Introduction to Classes and Objects
15/25
Instance Variables, setMethods
and getMethods A Class normally consists of methods and
attributes
Attributes are represented as variables ina class declaration
-
8/8/2019 01. Introduction to Classes and Objects
16/25
GradeBook Class with an Instance Variable,
a setMethod and a getMethod
-
8/8/2019 01. Introduction to Classes and Objects
17/25
GradeBookTest Class That
D
emonstrates Class GradeBook
-
8/8/2019 01. Introduction to Classes and Objects
18/25
Output
-
8/8/2019 01. Introduction to Classes and Objects
19/25
Access Modifiers
Declaring instance variables with access
modifier private is known as data hiding
variable courseName is encapsulated (hidden) in the
object and can be accessed only by methods of theobject's class
Private
Can only be accessed by methods that is a member
of the class
Public
Can be accessed by methods outside the class
-
8/8/2019 01. Introduction to Classes and Objects
20/25
Updated UML for GradeBook class
-
8/8/2019 01. Introduction to Classes and Objects
21/25
Initialing Objects with Constructors
Each class we declare can provide a constructor
A method that will be executed first when we create
an object
Java automatically provides default constructor in anyclass
Constructor must have the same name as the
class
Usage example : we want to define a defaultvalues for our classs attributes
-
8/8/2019 01. Introduction to Classes and Objects
22/25
GradeBook class with a constructor that
receives a course name
-
8/8/2019 01. Introduction to Classes and Objects
23/25
Constructor used to initialize GradeBook
objects
-
8/8/2019 01. Introduction to Classes and Objects
24/25
-
8/8/2019 01. Introduction to Classes and Objects
25/25
Updated UML for GradeBook class