Session03-Classes and Objects
Session03-Classes and Objects
(http://docs.oracle.com/javase/tutorial/java/javaOO/index.
html)
• Encapsulation
• Inheritance
• Polymorphism
ID_Num ID_Num
Name Name
YearOfBirth inherited YearOfBirth
Address Address
getID_Num() getID_Num()
setID_Num(newID) setID_Num(newID)
...... ......
“is a”
relationship
RollNum
class STUDENT
Score
extensions
RollNum getScore()
Score setSore(newScore)
getScore() ......
setSore(newScore)
......
Son = Father + extensions
Product
code
name
make
price
ElectricProduct Food
guaranty Ceramic
date
voltage type expiredDate
power
Session 03 - Classes and Objects
OOP Concepts: Polymorphism
11
Hints for class design
• Implementing methods: Cohesion is the degree to
which a class or method resists being broken down
into smaller pieces.
class A
M1()
{
class A Operation 1
M() }
{ M2()
Operation 1
{
Operation 2 Operation 2
} }
M()
Low cohesion(bad design) { M1(); M2();
}
• Create an object
ClassName obj1=new ClassName();
ClassName obj2=new ClassName(params);
• Accessing a field of the object
object.field
• Calling a method of an object
object.method(params)
Session 03 - Classes and Objects
Demo: If we do not implement any
constructor, compiler will insert to the class a
system default constructor
(2) Setup
values
y 0
100 x 0
(1) Memory
allocation
p 100
An object variable is a reference
Access level
Applied to
Free-accessing public
interface
package/ subclass
protected
class
outside cannot access
private
members of
interface/class
package
no specifier
(default)
Interface is a group of prototyped
methods and they will be
Order: implemented in a class afterward.
public > protected > default > private It will be introduced later.
Session 03 - Classes and Objects
Common Modifiers
39
Case Study 1 Report
1- Problem Description
• Each person details include code, name,
and age.
• Write a Java program that allows users
adding a new person to the list, removing
a person having a known code from the
list, updating details of a known-code
person, listing all managed persons in
descending order of ages using a simple
menu.
40
Report…
2- Analysis
From the problem description, following use-cases are
identified:
-System/program is
expressed as a bounded
rectangle.
- Each function is
expressed by a verb in an
ellipse
-User runs a function is
expressed as a line
41
Report…
3- Design
3.1- Class Design
From the problem description, concepts in the problem domain
are expressed by following classes:
Class Person
Description for a person
Data: String code; String name; int age
Methods:
Constructors
Getters, setters
void input() for collecting data
String toString() to get data in string format
42
Report…
Class PersonList
Description for a list of persons
Data:
Person[] list; // current list
int count // current number of persons
Methods:
Constructors
Getters, setters
void add(); // add a new person. Data are collected from keyboard
int find (String aCode); // Find the index of the person whose code is known
void remove()// remove a person. His/ her code is accepted from keyboard
void sort(); // descending sort the list based on their ages
void update(); // update a person, data are accepted from keyboard
void print(); // print the list
43
Report…
Class Menu
Description for a menu
Data
String[] hints; // list of hints
int n; // current number of hints
Methods:
Menu(int n): constructor for initializing a menu containing n options
void add (String aHint); // add an option
int getChoice(); // get an option
Class ManagingProgram1
Description for the program
Data: none
Methods:
main(…): main method of the program
44
Report…
3.2- Program structure
Algorithms
Please see comments in codes.
45
Report…
3.3- User interface
46
Report…
4- Implementation
Initial data of the program ( if any, file ………)
Please explore the software structure
Software
Please run the program
5- Testing
No. Case State
1 Add new person Passed
Code: not duplicate Passed
Name: …. not passed
Age: …. ……
2 Remove a person Passed
3 Update aperson Passed
4 List ……..
… ………………. …..
47
Recommendations
Code Conventions:
• Indentation: 4 blanks at the beginning of
each code line
• Comments in the code must be carried out.
• Names:
• One-word name: lowercase
• Multi-word name: The first word: lowercase,
remaining words: The first character is
uppercase, others are lowercase.
50
Case study:
Code Supported
this:
reference of
the current
object
51
Case study: Implementation
52
Case study: Implementation
53
Case study: Implementation
54
Case study: Implementation
55
Case study: Implementation
56
Case study: Implementation
57
Summary
• The anatomy of a class, and how to declare
fields, methods, and constructors.
• Hints for class design:
• Main noun Class
• Descriptive nouns Fields
• Methods: Constructors, Getters, Setters, Normal methods
• Creating and using objects.
• To instantiate an object: Using appropiate
constructior
• Use the dot operator to access the object's
instance variables and methods.
Session 03 - Classes and Objects