Professional Documents
Culture Documents
OOEDP wk1 L1
OOEDP wk1 L1
April 12
Objectives
In this lecture, we will Discuss the module organisation Discover why object-orientation is desirable Review the basic object oriented concepts Define classes, create and use instances of classes Examine the structure and syntax of a simple Java program using classes
April 12
Module introduction
this module follows on from the first Semester module, Introduction to Software Development (ISD) we will build on the Java basics we learnt last semester this module introduces object-orientation
writing classes creating and using instances of classes object-oriented concepts developing graphical user interfaces (GUIs) using classes from the Java Swing and awt packages (libraries)
April 12
Module organisation
see Blackboard site for details you must attend 2 lectures and 2 practical sessions per week see your timetable for details
most of you will have the same tutor as ISD Semester 1
you will keep a portfolio of your tutorial exercises throughout the course test based on the portfolio in Week 7
30% of module mark
April 12
April 12
the keyword static means the method belongs to the class as a whole, not to an individual instance (object)
April 12
the Scanner class defines methods that can be used to read the input
next() nextInt() nextDouble()
April 12
April 12
A class has a set of methods that define the functionality that it can provide
For example nextInt is a method of the Scanner class
Pete
Cathy Graham
437
525 214
6
4 3
25000
60000 40000
Claude
Dave
April 12
639
309
4
5
OOEDP Week 1 Lecture 1
35000
15000
11
what are the drawbacks of this approach? Name Employee number Department Salary
Pete
Cathy Graham
437
525 214
6
4 3
25000
60000 40000
Claude
Dave
April 12
639
309
4
5
OOEDP Week 1 Lecture 1
35000
15000
12
store the Employee objects in a 1-D array of type Employee to access Employee information, loop through the array and call an appropriate method on each Employee object
to find the total salary, call a getSalary() method on each Employee object add to a running total
In contrast traditional programming focuses upon the organisation of Algorithms The three main characteristics of OOP are: Encapsulation Inheritance Polymorphism
April 12 OOEDP Week 1 Lecture 1 14
Characteristics of O.O.P.
Encapsulation
combining data structures with the functions that act upon them hiding implementation Member Variables Member Functions (also called Member Methods)
April 12
15
Characteristics of O.O.P.
Inheritance the ability to construct DERIVED classes from existing BASE classes the derived class inherits the member variables and member methods of the base class the derived class can
add additional member variables add new member methods or override or extend the functionality of existing base class methods
April 12
16
Characteristics of O.O.P.
Polymorphism one method can have a different function associated with it for each class in a hierarchy
which function to call can be determined at run time based upon the class of the object against which the method is called
April 12
17
April 12
18
Classes
A CLASS is a user defined type which encapsulates
the representation of the type the operations which access or update objects of that type operations for creating and deleting objects
A class is a structure that can have functions as members as well as data elements The functions are called:
member functions or member methods
April 12
19
Classes
A class Person could be set up to hold and manage information about a person There are many common attributes that could be used to describe a person
name, age sex, occupation
These attributes are held as member variables of the class In this example we will use just two attributes
name and age
April 12
20
Classes
There are many things that we might like to do with the information about a person
set the value of the name set the value of the age get the value of the name get the value of the age display details of the person
April 12
21
Person
Person name age setName setAge getName getAge member methods member functions attributes member variables
displayDetails
April 12
22
Person
setName
setAge
getName
name
age
getAge
displayDetails
April 12
23
Classes in Java
public class Person { String name; int age; public void setName (String theName) {name = theName;} public void setAge (int theAge) {age = theAge;} public String getName () {return name;} public int getAge () {return age;} public void displayDetails () { System.out.println("Name is : " + name ); System.out.println("Age is : " + age ); } }
April 12 OOEDP Week 1 Lecture 1 24
Creating an Object
An object is an instance of a class The tutor is an example of a person
has a name has an age
construct an object
an instance of the class
25
April 12
26
Testing a Class
One way to test a class is to build a simple driver class
The driver contains a main In the main method the class to be tested is instantiated Then each of the methods of the class are invoked
Why can the driver class method main be used without instantiating the driver class? In the driver main is:
public static void main (String [] args)
Methods that are static are at the class level and can be invoked without instantiating the class
April 12 OOEDP Week 1 Lecture 1 28
Summary
In this lecture we have: Discussed the module organisation Introduced why object-orientation is desirable Reviewed the basic object oriented concepts Defined classes, created and used instances of classes Examined the structure and syntax of a simple Java program using classes
April 12
31