L14-Classes1.ppt
Download
Report
Transcript L14-Classes1.ppt
King Fahd University of Petroleum & Minerals
College of Computer Science & Engineering
Information & Computer Science Department
ICS102
Lecture 14 : Classes
July 17, 2016
Outline
Introduction
Class Definitions
A Class Is a Type
The new Operator
Instance Variables and Methods
Information Hiding
public and private Modifiers
Accessor and Mutator Methods
Encapsulation
- Introduction
Classes are the most important language feature
that make object-oriented programming (OOP)
possible
Programming in Java consists of defining a number
of classes
Every program is a class
All helping software consists of classes
All programmer-defined types are classes
Classes are central to Java
Example
Suppose you are asked to write a program for a
commercial company:
The company has several:
Employees
Clients
Products
Stores
If you use an object-oriented language such as Java,
you will have to define:
Class
Class
Class
Class
Employee :
Client:
Product:
Store:
public
public
public
public
class
class
class
class
Employee{ ..}
Client{..}
Product{..}
Store{..}
- Class Definitions
You already know how to use classes and the
objects created from them, and how to invoke their
methods
For example, you have already been using the predefined
String and Scanner classes
Now you will learn how to define your own classes
and their methods, and how to create your own
objects from them …
next slide
Class Definition
A class definition is composed of two parts:
Data members (Data part)
Methods (Operations part)
Example: define a class Employee
- Details
A class definition specifies the data items and methods
that all of its objects will have
These data items and methods are sometimes called
members of the object
Data items are called fields or instance variables
Instance variable declarations and method definitions can
be placed in any order within the class definition
Object Declaration
It is possible to declare several objects from a class:
e1
e2
Class Employee
e3
e4
s1
The same principle as:
s2
String
s3
s4
Note: classes Employee and TestClass should be
saved in the same directory
- A Class Is a Type
A class is a special kind of programmer-defined type,
and variables can be declared of a class type
A value of a class type is called an object or an
instance of the class
e1
The following sentences are equivalent:
“e1 is of type Employee,"
“e1 is an object of the class Employee," and
“e1 is an instance of the class Employee"
e2
Employee
e3
e4
- Object Creation
The instruction:
Employee e1;
only declares e1. But the object is still not created.
To create the object the operator new must be used:
e1 = new Employee();
These can be combined as follows:
Employee e1 = new Employee();
- Object Creation
Declaration
Creation
Question: What is the name of Employee e1?
How to change the name of Employee e1? … next slide ..
- Instance Variables and Methods …
In order to refer to a particular instance variable, preface it
with its object name as follows:
objectName.instanceVar1
objectName.instanceVar2
Example:
e1.name
e1.age
e1.salary
To change the name of e1:
e1.name = “Mohamed”;
- Instance Variables and Methods …
In order to invoke a method of a class, you need an object
of that class:
objectName.method1()
objectName.method2(argument1)
Example:
e1.outputDetails();
e1.outputDetails();
- Instance Variables and Methods …
Modifying the name
Invoking a method
Calling object
Accessing the name
- Information Hiding and Encapsulation
Information hiding is the practice of separating how to use a class
from the details of its implementation
Abstraction is another term used to express the concept of
discarding details in order to avoid information overload
Encapsulation means that the data and methods of a class are
combined into a single unit (i.e., a class object), which hides the
implementation details
Knowing the details is unnecessary because interaction with
the object occurs via a well-defined and simple interface
In Java, hiding details is done by marking them private
- public and private Modifiers …
Instance variables and methods of a class can be declared
Example:
public, or
private
public String name;
private int age;
public void outputDetails{..}
The modifier public means that there are no restrictions on
where an instance variable or method can be used
The modifier private means that an instance variable or method
cannot be accessed by name outside of the class
It is considered good programming practice to make all instance
variables private
- public and private Modifiers …
Illegal because we try to access a private member (age)
from outside the class Employee
… - public and private Modifiers
Within the definition of a class, private members of
any object of the class can be accessed, not just
public members of the calling object
Problem ..
It is considered good programming practice to make all
instance variables private
Question: how to access and modify the
instance variables of Employee objects e1,
e2, e3 and e4? .. answer .. Use accessor
and mutaor methods …. next slide ..
- Accessor and Mutator Methods …
Accessor methods allow the programmer to obtain the value of an object's
instance variables
The data can be accessed but not changed
The name of an accessor method typically starts with the word get
Example: public String getName()
{ return name;}
Mutator methods allow the programmer to change the value of an object's
instance variables in a controlled manner
Incoming data is typically tested and/or filtered
The name of a mutator method typically starts with the word set
Example: public void setName(String n)
{ name = n;}
- Accessor and Mutator Methods (Example)
Accessor method for instance variable name
Mutator method for instance variable name
Modifying the name of e1 using a mutator method
The end
Important to do at home :
- read sections 4.1 and 4.2 (pages
200-243)
Exercise
Define a class Date with three instance variable:
month of type String
day of type integer
year of type integer
Provide a method WriteOutput to print the details of
the Date
Provide accessor and mutator methods for all
instance variables.
Solution
Solution
Solution