Академический Документы
Профессиональный Документы
Культура Документы
Development
CT010-3-1
Object-Oriented Programming
Prepared by: STH First Prepared on:27 th July 2005 Last Modified on:19th December 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation
Static/Instance members
CT010-3-1 Fundamentals of Software Development
Learning Outcomes
At the end of this topic, you should be able to:
Explain object-oriented programming concepts
using appropriate examples.
Write class definitions with members.
Explain the usage of different visibility controls
using appropriate example.
Create, edit, compile, run and debug simple objectoriented Java programs.
Key Terms
If you have mastered this topic, you should be able to use
the following terms correctly in your assignments and
exams:
Class
Object
Methods
Instance Members
Class Members
Inheritance
Multiple Inheritance
Polymorphism
Encapsulation
Information Hiding
State of an object
Visibility Controls
CT010-3-1 Fundamentals of Software Development
Footballer
Complex Number
Car
Bank Account
Scale
Lion
State
Behaviour
State
A car may have the following features or attributes :
Color
Speed
Attributes
Size
Fuel
moving
Current
stopped
states
Behaviour
Functionally, a car can do the following:
Go
Stop/Brake
Turn right
Turn left
Encapsulation
is the grouping together of related states and behaviors to form
objects, whereby some parts of the object remain visible while some
parts are hidden. Encapsulation involves information hiding.
Information Hiding
Only necessary information to accompish a task is provided to the
user (in this case, the programmer). This is the principle of
information hiding and is a related principle of encapsulation.
Data Encapsulation
The Car Object
Public
API
change
gears
5th gear
Gear implementation
brake
turn
steering
60 mph
Steering
implementation
Private
implementation
details
Brake implementation
Objects Vs Classes
- Class
A class defines a real world or abstract entity. It is the
type or classification of data. A class defines both the
behavior and attributes of a group of objects with similar
characteristics.
Car class
class name
methods (functions)
attributes (data)
Object name
Methods :
start, go, stop, turn
Data / Attributes :
red, 175 m.p.h, full
Defining a class
A class is a user-defined data type.
Once the class type has been defined, we can create
variables of that type using declarations similar to
primitive data types.
Data and methods defined in the class are known as
instance members.
class classname
{
variable(s) declaration
method(s) declaration
}
CT010-3-1 Fundamentals of Software Development
Creating an Object
Class methods
Method Structure
is a set of program statements
fundamental unit of execution
exists as part of a classs
Syntax of a method:
returnType methodName (paramList) {
// body of method
}
returnType return type (value)
declare void if no value returned
methodName name of method
paramList optional list of parameters
CT010-3-1 Fundamentals of Software Development
Class methods
To invoke a method :
methodName(args);
invoke the method by using its name and passing it an
optional set of arguments
Example :
boolean process(int i, String s){
//body of method
}
method that accepts int and String parameters and
returns a boolean type
CT010-3-1 Fundamentals of Software Development
Class methods
Write the program necessary to create a small application calSqrt that print
out Hello Malaysia and has one calculateSqrt() method. The main() method
will call the calculateSqrt() method which calculates the square root of 5 and
then prints out the answer on a new line.
Sample program :
class calSqrt {
public static void main (String[] args) {
System.out.println(Hello Malaysia);
calculateSqrt(5);
}
static void calculateSqrt(double num) {
double numSqrt;
numSqrt = Math.sqrt(num);
System.out.println(The squareroot of 5 : + numSqrt);
Messages
A single object alone is generally not very useful and usually appears as a component of a larger program
or application that contains many other objects ~ thus, object needs to interact with each other.
How do objects interact?
Objects interact by sending messages to each other.
Message
Object B
Object A
Messages
3 components comprise a message :
1. The object to whom the message is addressed
(Your Bicycle)
2. The name of the method to perform
(changeGears)
3. Any parameters needed by the method
(lower gear)
changeGears (lowerGear)
Your Car
You
Visibility control
Term
Meaning
private
protected
public
default
Greetings
Class
class TestGreetings {
public static void main(String[] args) {
Greetings welcome = new Greetings();
welcome.greet(God");
}
}
TestGreetings
class
Greetings
class
class TestGreetings {
public static void main(String[] args) {
Greetings welcome = new Greetings("Hello,");
welcome.greet(God");
}
}
CT010-3-1 Fundamentals of Software Development
TestGreetings
class
Greetings
class
}
class TestGreetings {
public static void main(String[] args) {
Greetings.showTitle();
Greetings welcome = new Greetings();
welcome.greet(God");
}
TestGreetings
class
Static modifier
Static Variables
eg.
private static final int April = 4, May = 5, June = 6;
static modifier may only be used on members at
the class level, not on variables and arguments
within methods.
also known as class variable
Static modifier
Static Methods
referenced through the class itself
eg. main method of a Java program must be
declared static so that main can be executed by
the interpreter without instantiating an object from
the class containing main
eg. Math class in the java.lang package
What is a method?
Follow Up Assignment
Write an app that declares a Rectangle class.
Declare two instance variables width and height
of type double. Declare an instance method
getArea that calculates the area of the
rectangle. Display the area of the rectangle from
main class.
You will be required to demonstrate your
answer on the board during the next lesson.
Static/Instance members
CT010-3-1 Fundamentals of Software Development
Next Session
Inheritance
superclass and subclass
single level inheritance
using extends
Constructors and creating them
Sample programs