COMPE 438 JAVA PROGRAMMING - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

COMPE 438 JAVA PROGRAMMING

Description:

System.out.println('Student Name:' stu.getName ... Dinosaur. Frogosaur. Multiple Inheritance. abstract class Animal { abstract void talk ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 42
Provided by: CEM4
Category:

less

Transcript and Presenter's Notes

Title: COMPE 438 JAVA PROGRAMMING


1
COMPE 438JAVA PROGRAMMING
  • F. Cemile Serçe

2
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMINGTHE
UNIFIED MODELING LANGUAGE, UML
3
Desirable Qualities of Software Systems
  • Usefulness
  • Timeliness
  • Reliability
  • Maintainability
  • Reusability
  • User friendless
  • Efficiency

4
Desirable Qualities of Software Systems with OO
focus
  • Usefulness
  • Timeliness
  • Reliability
  • Maintainability
  • Reusability
  • User friendless
  • Efficiency
  • Cost
  • Reliability
  • Flexibility

5
Change or die
  • Software
  • is not like a novel that is written once and then
    remains unchanged.
  • is extended, corrected, maintained, ported,
    adapted
  • cannot be maintained will be thrown away.
  • The work is done by different people over time
  • Either it is continuously maintained
  • or it dies.

6
Object-Oriented Programming
  • a programming methodology that views a program as
    consisting of objects that interact with one
    another by means of actions
  • alternative to procedural programming
  • Address problems of procedural programming and
    top-down design
  • Cascading changes
  • Decomposition based on the structure of objects,
    classes and the relationships

7
Object-Oriented Programming
  • Everything is an object
  • A program is a bunch of objects telling each
    other what to do by sending messages
  • Each object has its on memory made up of other
    objects
  • Every object has a type
  • All objects of a particular type can receive the
    same message

8
Objects
  • Reusable software components that model
    real-world items
  • Look all around you
  • People, animals, plants, cars, etc.
  • Attributes
  • Size, shape, color, weight, etc.
  • Behaviors
  • Babies cry, crawl, sleep, etc.

9
Object-Oriented Analysis and Design (OOA/D)
  • Essential for large programs
  • Analyze program requirements, then develop
    solutio
  • Unified Modeling Language (UML
  • Graphical language that uses common notation
  • Allows developers to represent object-oriented
    designs
  • Flexible and extendible

10
History of UML
  • Need developed for process with which to approach
    OOA/
  • Brainchild of Booch, Rumbaugh and Jacobson
  • Object Management Group (OMG) supervised
  • Version 1.6 is current version

11
Classes and Objects
In Real World
In OO Model
represents anything in the real world
that distincly identified
has an indetity, a state and a behavior
OBJECT
represents a set of objects with similar
characteristics and behavior. These objects Are
the instances of the class
characterizes the structure of states and
behaviors that are shared by all its instances
CLASS
12
Classes, Objects, Instances
  • Class
  • Describes the structure and behavior of a set of
    similar objects
  • Object
  • an instance which is present at execution time
    and allocates memory for its instance variables,
    and which behaves according to the protocol of
    its class

Class
Object
13
Classes, Objects, Instances
  • Object-Class relationship

Class
Object
instance of
Shape
Triangle
instance of
14
Classes, Objects, Instances
  • Properties of classes and objects
  • Attributes
  • The structure of the objects
  • Circle -gt Radius and position (x,y)
  • Operations
  • The behavior of the objects
  • Circle -gt display, remove, reposition, resize
  • Constraints
  • Conditions, requirements and rules that objects
    must satisfy
  • Circle -gt radius gt0

15
Classes, Objects, Instances
Circle
Class name
Constraint
radius radius gt 0 centerpoint Point (10,
10)
Attribute name
Initial Value
display() remove() setPosition(pos
Point) setRadius(newRadius)
Attribute type
Parameters (name type initial value)
Methods
Class
16
Classes, Objects, Instances
class Circle int radius Point
centerPoint public void setRadius(int
newRadius) if(newRadius gt 0)
//constraint radius
newRadius public
void setPosition()
public void display()
public void remove()
17
Classes (example)
Point
int x
UML
int y
public void move(int dx, int dy)
Java Code
18
Classes, Objects, Instances
aCircle Circle
Object name
Class name
radius 25 centerpoint (10, 10)
Attribute names
Attribute values
Object
19
Examples for Objects
p1Point
p2Point
UML
x 0
x 24
y 0
y 40
Java Code
20
Packages
  • classes are grouped into a package
  • organized into a hierarchy
  • closely related classes

21
UML Notation for Packages
java.awt
datatransfer
Point
event
image
22
Class Hierarchies
23
Association
Company
Person
0..1
0..
employs
24
Aggregation
Enterprise
Department
Employee
consists of
consists of
1
1..
1
1..
25
Composition Aggregation
Invoice
Invoice Item
has
composition
1
1..
Car
Wheel
has
aggregation
1
3..4
26
The Principles in OOP
  • Modularity
  • Abstraction
  • Encapsulation
  • Polymorphism

27
The Principles in OOP
  • Modularity
  • High Cohesion
  • Low Coupling
  • Abstraction
  • Encapsulation
  • Polymorphism

28
The Principles in OOP
  • Modularity
  • Abstraction
  • Contractual interface
  • Ex telephone service with manual as abstraction
  • Encapsulation
  • Polymorphism

29
The Principles in OOP
  • Modularity
  • Abstraction
  • Encapsulation
  • Polymorphism

30
Encapsulation
  • separate implementation from its contractual
    interface
  • reduce coupling
  • e.g. Customer, waiter and kitchen

31
The Principles in OOP
  • Modularity
  • Abstraction
  • Encapsulation
  • Polymorphism

32
Polymorphism
  • one of the cornerstones that makes
    object-orientation powerful
  • having many forms
  • an operation may behave differently (in different
    classes)

33
Polymorphism
Shape
...
draw()
Circle
Rectangle
...
...
draw()
draw()
34
Polymorphism
  • Objects can respond differently to the same
    message. Both waiter as kitchen respond to 'a
    black coffee.
  • The actions are different though.

35
Inheritance
  • one of the most important relationship in OO
    modeling
  • parent/child relationships among classes
  • supports a form of code reuse
  • precondition for polmorphism
  • types of inheritance
  • single inheritance
  • multiple inheritance
  • Java does not support multiple inheritance

36
Inheritance
  • Examples

37
Inheritance
  • Both waiter and cook are employee. So they both
    have an employee number.
  • Both return a cup of coffee to the question "A
    cup of Coffee please".
  • There are some exceptions. Waiter and Cook have
    different methods to get a cup of coffee.

38
Inheritance
class Person   private String name    public
void setName(String n)      name
n       public String getName()      return
name   
Person
Student
class Student extends Person   private String
stuNum    public void setStuNum(String
sn)      stuNum sn       public String
getStuNum()      return stuNum   
39
Inheritance
public class TestInheritance   public static
void main(String args)      Student stu new
Student()      stu.setName("John
Smith")      stu.setStuNum("12345")      Syste
m.out.println("Student Name" stu.getName())   
   System.out.println("Student Number
stu.getStuNum())   
40
Multiple Inheritance
  • Diamond Problem

Animal
Frog
Dinosaur
Frogosaur
41
Multiple Inheritance
  • abstract class Animal abstract void talk()
  • class Frog extends Animal void talk()
    System.out.println("Ribit, ribit.")
  • class Dinosaur extends Animal void talk()
  • System.out.println("Oh I'm a dinosaur and
    I'm
  • OK...")
  • // (This won't compile, of course, because Java
    // only supports single inheritance.)
  • class Frogosaur extends Frog, Dinosaur

Animal animal new Frogosaur() animal.talk()
?
Write a Comment
User Comments (0)
About PowerShow.com