0% found this document useful (0 votes)
42 views

Object-Oriented Software Development - Tutorial 4

This document discusses object-oriented programming concepts in Java, including defining classes, variables, methods, constructors, and access control. It explains that in object-oriented languages like Java, the basic unit of organization is the class, which creates reusable and modular code through encapsulation compared to non-object-oriented languages. Advantages of the object-oriented approach include reuse of code, managing complexity through separation of concerns, easier debugging, and improved collaboration.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Object-Oriented Software Development - Tutorial 4

This document discusses object-oriented programming concepts in Java, including defining classes, variables, methods, constructors, and access control. It explains that in object-oriented languages like Java, the basic unit of organization is the class, which creates reusable and modular code through encapsulation compared to non-object-oriented languages. Advantages of the object-oriented approach include reuse of code, managing complexity through separation of concerns, easier debugging, and improved collaboration.
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Object-Oriented Software

Development

Tutorial 4

1
Objectives
• Write simple Java applets that define and use
methods and variables
• Explain the difference between public and
private variables and methods
• Explain the difference between class and
instance variables and methods
• Understand the scope of class, instance and
local variables
• Be able to write programs that make use of
classes in packages
2
Reading
This material should be read in conjunction
with Deitel and Deitel Chapter 8 section
8.1-8.4, 8.6-8.9, 8.15
pp. 379-390, 394-412, 427-432.

3
Defining a class in Java
• The implementation of a class in Java always
looks similar to the following:
class Customer   {
// variables
<variable1>
<variable2>
  <variable...>
// methods
  <method1>
  <method...>
 }
4
Variables
• Variables store values. Variables have a name, a
type, and a value.
• For example, in a program concerned with company
accounts, we may define a class called Customer,
representing and managing the details of the
company's customers. Variables of Customer may
include name, address, credit limit, etc. Each object
of class customer may have different values of these
variables.
• Taking the 'Customer' example, let's assume that the
important features of a customer are name, address,
account balance and credit limit. We might define this
as below:

5
Class Customer
class Customer
{
  String name;
  String address;
  int accountBalance;
  int creditLimit;   // printBalance method
  // prints the customer's balance
public void printBalance ()
   {
      System.out.println 
              ("Current balance for " 
               + name 
               + " is " 
               + accountBalance
               + " pounds");
   }
}
6
class TextItem
{
  // variables
  String title;
  int numPages; Class TextItem
  String shelfMark;
  boolean onLoan;
  int daysLate;  
// methods
  void TextItem( String itemTitle,int itemNumPages, 
                  String itemShelfMark)  { // method implementation }
String getTitle() { // method implementation  }
int getNumPages() { // method implementation }
String getShelfMark() { // method implementation }
boolean getLoanStatus() {// method implementation }
void setLoanStatus(boolean newLoanStatus)  { // method implementation}
int getDaysLate() { // method implementation }
void setDaysLate(int newDaysLate) {//method implementation }
double getFine() { // method implementation  }
void informBorrowerOfFine( <args> )  {// method  implementation  }
}

7
getFine()
double getFine()
{
  return (0.05 * daysLate);
}

8
Creation and deletion of objects
• The ‘new’ operator creates new objects of a specific class. 
• An object is an instance of a class. In Java we can create an object
using the keyword new as follows:
new String("Fred Smith");
new TextItem( "Deitel & Deitel", 1041, "Dei
304.167");
new BouncingBall();
Such statements will result in a new object being created — a new instance of the
specified class. When a new object is created, an area of the computer's
memory is allocated to store the object. Objects do not usually have to be
explicitly removed. A process called garbage collection is how the Java run-time
interpreter gets rid of them when they are no longer accessible.

9
Access control
• Java provides several levels of access to the
variables and methods defined in a class.
• The keywords are:
public
private
• Usually all the variables are private, this means
another object cannot get access to them directly.
• Other object must send messages to public methods
to create changes, such as using get and set
methods.
10
Packages and imports
• Related classes are grouped into a package.
• The standard Java package java.lang is imported
by the compiler automatically.
• Example:
import java.applet.Applet;
import java.awt.*;
• If we did not use the import line, we need to
specify the package in the program:
class MyApplet extends java.applet.Applet

11
Classes & Objects
• Every program must have at least one _____.
• A class acts as a ________ for a category of objects.
• Program instructions can only exist inside _______,
which are inside classes.
• The ___ operator creates new objects of a specific class.
• Objects are created and _________ during the execution
of a program.
• Classes do not usually have to be explicitly removed —
the Java system gets rid of them when they are no
longer accessible. This is known as _______
__________.
• _______ in classes can (and usually do) create new
objects.

12
Classes & Objects
• Every program must have at least one class.
• A class acts as a template for a category of objects.
• Program instructions can only exist inside methods,
which are inside classes.
• The new operator creates new objects of a specific
class.
• Objects are created and destroyed during the execution
of a program.
• Classes do not usually have to be explicitly removed —
the Java system gets rid of them when they are no
longer accessible. This is known as garbage collection.
• Methods in classes can (and usually do) create new
objects.
13
Geometric class constants
• Note how each constant is:
public
since they are not much use if they are not
readable by objects of other classes
• Each constant is a `class constant' since it is
defined as
static
• Each constant is constant (i.e. an unchanging
variable) because it is defined as:
final
14
Methods Summary
Complete the following statements that summarize the concepts
explored in the 'Packages and Imports' section of this unit:
• A ___________ is a special method that is
called automatically as soon as an object
is created.  The constructor method should
__________ the object — by assigning
sensible values for its _________. 
• A constructor method is indicated by a
method that has the same _________ (name)
as the class. 
• A constructor is a ______, and may have
_________ like other methods. 
• It is common practice to provide every
class with a both a ___________ and a
toString() method.
15
Methods Summary
Suggested completed summary statements are as follows:
• A constructor is a special method that is called
automatically as soon as an object is created.
• The constructor method should initialize the object
— by assigning sensible values for its variables.
• A constructor method is indicated by a method
that has the same identifier (name) as the class.
• A constructor is a method, and may have
arguments like other methods.
• It is common practice to provide every class with a
both a constructor and a toString() method.

16
Review Question 1
• In non-object oriented programming languages
(like, C, Pascal, FORTRAN, etc.) the basic unit
of organization of a program is the function. A
function is like a method, but not in a class. In
fact, programs in such languages can be
thought of as one very large class, containing
all the methods of the program.
• In what ways might the object-oriented strategy
adopted by Java be superior to this? In what
ways might an object-oriented approach be a
disadvantage?

17
Answer to Review Question 1
• Encapsulation is a very important concept in
software engineering. If a computer program
exhibits encapsulation, this means that it is
made up of independent, self-contained
parts.
• Since there are strict rules about how classes
can interact with one another, then a program
based on classes will tend to exhibit
encapsulation. Good use of encapsulation
leads to the following advantages:
18
Advantages of Encapsulation
• Reuse of program code
• Management of program complexity
• Easier fault-finding
• Improved teamwork

19
object-oriented modeling Object-oriented programming

generalization/specialization inheritance

specializes extends

operation method, member function, function


member
attribute member variable, instance variable,
data member, field
virtual class Abstract class

object object, instance


(nearest term is `method', but a
event, message
`method' and a `message' are not
strictly equivalent)
20
The terms shown in bold are the preferred terms for Java programming.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy