Chapter 1: Important Features of Java - PowerPoint PPT Presentation

1 / 100
About This Presentation
Title:

Chapter 1: Important Features of Java

Description:

Title: Chapter 1: Important Features of Java Author: William Collins Last modified by: Becky Created Date: 8/27/2000 1:33:39 AM Document presentation format – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 101
Provided by: William1053
Learn more at: http://www.mscs.mu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 1: Important Features of Java


1
Chapter 1
Object-Oriented Concepts
2
  • A class consists of variables called fields
    together with functions called methods that act
    on those fields.

3
Lets look at the String class
4
An object is a variable whose type is a class. An
object has the fields and can call the methods
of its class.
5
A String object is a variable that contains a
string (a sequence of characters) and can call
methods in the String class.
6
String s In this declaration, s is not a String
object, but rather a String reference, that is,
a variable that can hold the address of a String
object.
7
  • To store the address of a String object in s,
  • we will
  • Allocate space for a new String object.
  • Initialize the fields in that object.
  • Assign to s the address of that object.

8
s new String()
9
A method with the same name as the class is
called a constructor. The purpose of a
constructor is to initialize the objects fields.
A classs default constructor has no parameters.
10
The String classs default constructor
initializes the fields so that the String object
represents an empty string.
11
Another constructor in the String class has a
String parameter. Here is the heading

parameter public String (String original) String
t new String (Aloha)
argument
is a reference to Aloha

12
Now the String objects referenced by s and t can
invoke String methods s.length() //
returns 0 t.toLowerCase() // returns (a
reference to) //
aloha . t is still a // reference to
Aloha
13
/ Returns the index within this String
object of the first occurrence of the
specified substring. _at_param str the
specified substring _at_return the index of the
first occurrence of str in
this String object, or 1 if
str is not a substring of this
String object
14
_at_throws NullPointerException if str is
null / public int indexOf
(String str) The JAVADOC comments plus the
method heading constitute the method
specification A users view of the method.
15
System.out.println (t.indexOf (ha)) System.ou
t.println (t.indexOf (a)) System.out.println
(s.indexOf (ha)) Hint Indexes start at 0.
16
String w null w does not contain the address
of any String object, so w cannot call any
methods.
17
The equals method tests for equality of objects,
and the operator tests for equality of
references. String z new String (Aloha)
18
s.equals () s t.equals (Aloha) t
Aloha t.equals (null) t.equals (z) t
z w.equals (null) w null
19
String y1 Aloha String y2 Aloha These
statements create two references, y1 and y2, to
the same string object, so y1 y2 // returns
true y1 t // returns false but y1.equals (t)
// returns true
20
So far, we have studied what a class does, not
how the class does it. That is, we have studied
a class from the users perspective (method
specifications) rather than from a developers
perspective (fields and method definitions)
21
Principle of data abstraction A users code
should not access the implementation details of
the class used.
22
Many of the classes we will study share the same
method specifications.
23
When we abstract these specifications from the
classes we get an interface. An interface
consists of method specifications and constants
only.
24
For example, here is an interface for the
employees in a company. The information read in
for each employee consists of the employees
name and gross pay.
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
Note Each method is automatically public, and
each method heading is followed by a semicolon.
29
To implement that interface, we will create a
class with fields and, using those fields,
definitions of at least the two methods.
30
(No Transcript)
31
(No Transcript)
32
(No Transcript)
33
(No Transcript)
34
(No Transcript)
35
public boolean makesMoreThan
(Employee otherEmployee) if
(!(otherEmployee instanceof FullTimeEmployee))
return false
FullTimeEmployee full
(FullTimeEmployee)otherEmployee
return grossPay gt full.grossPay // method
makesMoreThan
Note The parameter type must be Employee because
that is the parameter type in the interface.
36
(No Transcript)
37
(No Transcript)
38
(No Transcript)
39
In a method definition, when a member (field or
method) appears without an object reference, a
reference to the calling object is assumed.
40
Now suppose we want to find the best-paid
full-time employee in a company. We will create a
Company class.
41
There are methods to initialize a Company object,
to find the best-paid full-time employee, and
to print that employees name and gross pay.
42
(No Transcript)
43
(No Transcript)
44
(No Transcript)
45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
Finally, we need a main method to get everything
started.
49
(No Transcript)
50
Exercise Make up sample input, and the
corresponding output.
51
Inheritance
52
Inheritance is the ability to define a new class
that includes all the fields and some or all of
the methods of an existing class.
53
Existing class superclass base class New
class subclass derived class
SUPERCLASS
SUBCLASS
54
The subclass may declare new fields and methods,
and may override existing methods by giving them
method definitions that differ from those in the
superclass.
55
Example Find the best-paid hourly full-time
employee with no overtime (40 hours) Input
Name, Hours worked, Pay rate
56
Modify FullTimeEmployee class?
57
The Open-Closed Principle
Every class should be Open extendible through
inheritance Closed stable for existing
applications
58
Specifically, the FullTimeEmployee class should
be stable for the existing application of finding
the best-paid employee in a company. And
extendible for this new application!
59
(No Transcript)
60
Find best paid employee project
Find best paid hourly employee project
FullTimeEmployee Class
HourlyEmployee Class
61
Overridden methods?
62
The declarations of name and grossPay must be
altered in the FullTimeEmployee class those
fields cannot be private. Would it be a good
idea to make them public?
63
(No Transcript)
64
(No Transcript)
65
(No Transcript)
66
(No Transcript)
67
(No Transcript)
68
(No Transcript)
69
(No Transcript)
70
(No Transcript)
71
(No Transcript)
72
(No Transcript)
73
(No Transcript)
74
(No Transcript)
75
(No Transcript)
76
(No Transcript)
77
(No Transcript)
78
(No Transcript)
79
(No Transcript)
80
(No Transcript)
81
(No Transcript)
82
(No Transcript)
83
Object-Oriented Essentials
1. Encapsulation 2. Inheritance 3. Polymorphism
84
(No Transcript)
85
(No Transcript)
86
(No Transcript)
87
(No Transcript)
88
(No Transcript)
89
(No Transcript)
90
(No Transcript)
91
(No Transcript)
92
(No Transcript)
93
A virtual method is a method that is bound to its
method identifier at run-time.
In Java, almost all methods are virtual.
94
(No Transcript)
95
(No Transcript)
96
(No Transcript)
97
(No Transcript)
98
(No Transcript)
99
(No Transcript)
100
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com