Upload
others
View
3
Download
0
Embed Size (px)
Citation preview
CS111: PROGRAMMING
LANGUAGE II
Lecture 1: Introduction to classes
Lecture Contents
Dr. Amal Khalifa, 2014
What is a class?
Encapsulation
Class basics:
Data
Methods
Objects
Defining and using a class
2
In Java
Java is an object-oriented programming language
As the term implies, an object is a fundamental entity in a Java program
Everything must be in a class (Even Main method).
3
Dr. Amal Khalifa, 2014
What is a class?
Class:
Defines a data type (blueprint)
Combines data and operations in
one place:
data / fields/ properties / state
variables
Operations/ behavior / methods that
modify state
Dr. Amal Khalifa, 2014
4
What is a data type?
Any data type includes
Data (range of values)
Operations (that can be performed on data)
Example: int data type has: Data: +-32,767 Operations: +, - , * , / , %
Same with classes
Collection of data values together with set of basic operations to be allowed for the values
6-5 Dr. Amal Khalifa, 2014
Class = Blueprint
One blueprint to create several similar, but different,
houses:
Copyright © 2012 Pearson Education,
Inc.
• The bundling of data and procedures(methods) into a single unit(called class).
• Class : various data elements and member functions are wrapped up together.
• main feature of object oriented programming
Encapsulation 7
Dr. Amal Khalifa, 2014
Objects First with Java - A
Practical Introduction using BlueJ,
© David J. Barnes, Michael Kölling
Objects & Classes
objects
represent ‘things’ from the real world, or from some
problem domain (example: “the red car down there in
the car park”)
classes
represent all objects of a kind (example: “car”)
Objects & Classes
variables of class type are objects
Object:
an instance / realization of a class
same as relationship between a dataType and its
variables
9
Dr. Amal Khalifa, 2014
How to define a Class??
Dr. Amal Khalifa, 2014
10
Type definition
Object declaration
Member access
Objects First with Java - A
Practical Introduction using BlueJ,
© David J. Barnes, Michael Kölling
Basic class structure
public class ClassName
{
Fields
Constructors
Methods
}
The contents of a class
Defining Classes
Dr. Amal Khalifa, 2014
12
Identifying Class Attributes Classes have attributes (data) and operations (behaviors).
Class attributes are implemented in Java programs as fields, and class
operations are implemented as methods.
the Unified Modeling Language (UML)
Dr. Amal Khalifa, 2014
13
can be defined as a modeling language to capture
the architectural, behavioral and structural aspects
of a system.
The UML has an important role in object oriented
analysis and design.
Objects are the key to this object oriented world
The basic requirement of is to identify the object
efficiently.
UML Class diagrams
Dr. Amal Khalifa, 2014
14
Class diagrams describes the objects in a system
and their relationships, so it is widely used by the
developer community.
The UML representation of a class is a rectangle
containing three compartments stacked vertically.
The top compartment shows the class's name.
The middle compartment lists the class's attributes.
The bottom compartment lists the class's operations.
What about a student??
Examples 15
Dr. Amal Khalifa, 2014
The student class - Exercise
Dr. Amal Khalifa, 2014
16
Draw UML class diagram for class
named Student where data is:
name, UnivID, date of birth,
department, level, GPA
Methods are:
PrintData()
A class in Java
Dr. Amal Khalifa, 2014
17
.java file-
name extension
The class
keyword
Public
keyword
access modifier
class is
“available to
the public
No main
can’t execute
// Student.java
public class Student {
// data..
//methods...
//not static!!
}
Student Test
Dr. Amal Khalifa, 2014
18
Another class.
Why not
Import
student.java??
Using new
object
instantiation
Dot separator
// StudentTest.java
public class Students {
public static void main (String[] args)
{
//define object(s)
//call a method
}
Access modifiers
Controlling Access to Members
Computer Science Department
19
Access modifiers
Public Methods
Client’s view of the services provided by the class
Private data
not accessible outside the class
protected.
Public and Private Members
Data in class almost always designated private in
definition!
Upholds principles of OOP (Data Hiding)
Hide data from user
Allow manipulation only via operations
Which are member methods
6-20 Dr. Amal Khalifa, 2014
Marks for UML-supported visibility types 21
Dr. Amal Khalifa, 2014
Mark Visibility type
+ Public
# Protected
- Private
~ Package
Example
Dr. Amal Khalifa, 2014
22
. Modify class
efinition
// Student.java
public class Student {
// data private
//methods public
}
Student Test
Dr. Amal Khalifa, 2014
23
Try to access
private
members!!
What about
the public
ones ?
// StudentTest.java
public class StudentTest {
public static void main (String[] args)
{
//define object(s)
//access different members
}
Set/get methods
Computer Science Department
24
• Object needs to “have access" to its data
• accessor methods
• Allow object to read data
• Also called "get methods"
• Simple retrieval of member data
• Mutator methods
• Allow object to change data
• Also called “set methods"
• Manipulated based on application
Example: set/get methods
Dr. Amal Khalifa, 2014
25
. Class
Methods have
direct access
to class
members
(data or
methods)
// Student.java
public class Student {
// data
//methods
//set methods
//get methods
}
Student Test
Dr. Amal Khalifa, 2014
26
The set
method can
be used to
modify a data
field
the get
method can
be used to
display the
updated
value
// StudentTest.java
public class StudentTest {
public static void main (String[] args)
{
//define object(s)
//modify data!!
}
Constructors
Dr. Amal Khalifa, 2014
27
A constructor can be used to initialize an object of a
class when the object is created.
Example: constructors
Dr. Amal Khalifa, 2014
28
. Constructor
must be public
Same name
as class
no return type
Parameters??
// Student.java
public class Student {
// data
//an empty constructor
public Student()
{
}
//set/get methods
//other methods
}
Student Test
Dr. Amal Khalifa, 2014
29
Keyword new
calls the
class’s
constructor to
perform the
initialization.
// StudentTest.java
public class StudentTest {
public static void main (String[] args)
{
//define object(s)using empty
//constructor
//print data
}
The default Constructor
By default, the compiler provides a default
constructor with no parameters in any class that
does not explicitly include a constructor.
If you declare any constructors for a class, the Java
compiler will not create a default constructor for that class
With the default constructor, its instance variables
are initialized to their default values.
Dr. Amal Khalifa, 2014
30
Overloaded Constructors
Dr. Amal Khalifa, 2014
31
Overloaded constructors
enable objects of a class to be initialized in different
ways.
multiple constructor declarations with different
signatures.
the compiler differentiates signatures by :
the number of parameters,
the types of the parameters and
the order of the parameter types in each signature.
Example: constructors
Dr. Amal Khalifa, 2014
32
. Define more
Constructors
Parameters??
// Student.java
public class Student {
// data
//constructors
public Student()
{
}
//set/get methods
//other methods
}
Student Test
Dr. Amal Khalifa, 2014
33
More objects
Different
initialization
Same
methods,
different data
// StudentTest.java
public class StudentTest {
public static void main (String[] args)
{
//define object(s)using different
//constructors
//print data
}
Using this Reference
Dr. Amal Khalifa, 2014
34
Every object can access a reference to itself with keyword this.
Implicitly:
refer to the object’s instance variables and other methods inside member methods.
Enables the class’s code to know which object should be manipulated.
Explicitly:
Can also use keyword this in a non-static method’s body.
Example: constructors
Dr. Amal Khalifa, 2014
35
. Use this
reference to
access class
members
// Student.java
public class Student {
// data
//constructors
public Student()
{
}
//set/get methods
//other methods
}
Important hint 36
Dr. Amal Khalifa, 2013
Be careful!! 37
Dr. Amal Khalifa, 2013
Text Book:
Chapter 3
That’s all for today…..
Dr. Amal Khalifa, 2014
38