OOP2024 JavaSlides W5 OOP-Abstraction
OOP2024 JavaSlides W5 OOP-Abstraction
Using Java
Abstraction
Quan Thai Ha
HUS
February 18, 2024
Presentation Outline
1 Abstraction
Abstract Classes
Interfaces
Usage of abstract classes
Usage of interfaces
4 References
1 105
Abstraction
3 105
Abstraction with Abstract Classes
4 105
Abstraction with Interfaces and Abstract Classes
5 105
Example of Designing a Java Project
6 105
How is Abstraction Created?
7 105
How is Abstraction Created?
8 105
Information Hiding
9 105
Meaning of Abstraction
10 105
Presentation Outline
1 Abstraction
Abstract Classes
Interfaces
Usage of abstract classes
Usage of interfaces
4 References
11 105
Abstract methods
An abstract method is a method with only signature (i.e., the method name, the list of
arguments and the return type) without implementation (i.e., the method’s body). You use
the keyword abstract to declare an abstract method.
These abstract methods cannot be invoked because they have no implementation.
Example
12 105
Abstract classes
A class containing one or more abstract methods is called an abstract class. An abstract
class must be declared with a class-modifier abstract.
An abstract class is incomplete in its definition, since the implementation of its abstract
methods is missing. Therefore, an abstract class cannot be instantiated. In other words,
you cannot create instances from an abstract class (otherwise, you will have an incomplete
instance with missing method’s body).
Example
13 105
Example of the abstract class
1 /∗ ∗
∗ T h i s a b s t r a c t s u p e r c l a s s Shape c o n t a i n s an a b s t r a c t method
3 ∗ g e t A r e a ( ) , t o be implemented by i t s s u b c l a s s e s .
∗/
5 p u b l i c abstract c l a s s Shape {
private String color ;
7
p u b l i c Shape ( S t r i n g c o l o r ) {
9 this . color = color ;
}
11
@Override
13 public String toString () {
r e t u r n " Shape [ c o l o r = " + c o l o r + " ] " ;
15 }
14 105
Usage of Abstract Classes
To use an abstract class, you have to derive a subclass from the abstract class. In the
derived subclass, you have to override the abstract methods and provide implementation to
all the abstract methods. The subclass derived is now complete, and can be instantiated. (If
a subclass does not provide implementation to all the abstract methods of the superclass,
the subclass remains abstract.)
1 p u b l i c c l a s s TestShape {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 Shape s 1 = new R e c t a n g l e ( " r e d " , 4 , 5 ) ;
System . o u t . p r i n t l n ( s 1 ) ;
5 System . o u t . p r i n t l n ( " Area i s " + s 1 . g e t A r e a ( ) ) ;
11 / / Cannot c r e a t e i n s t a n c e o f an a b s t r a c t c l a s s
Shape s3 = new Shape("green");
13 / / Compilation error: Shape is abstract; cannot be instantiated.
}
15 }
17 105
Presentation Outline
1 Abstraction
Abstract Classes
Interfaces
Usage of abstract classes
Usage of interfaces
4 References
18 105
Interfaces
A Java interface is a 100% abstract superclass which define a set of methods its subclasses
must support.
An interface contains only public abstract methods (methods with signature and no
implementation) and possibly constants (public static final variables).
You have to use the keyword "interface" to define an interface (instead of keyword "class"
for normal classes).
The keyword public and abstract are not needed for its abstract methods as they are
mandatory.
Unlike a normal class, where you use the keyword "extends" to derive a subclass. For
interface, we use the keyword "implements" to derive a subclass.
Interface Naming Convention: Use an adjective (typically ends with "able") consisting of
one or more words. Each word shall be initial capitalized (camel-case).
▶ For example, Serializable, Extenalizable, Movable, Clonable, Runnable, etc.
19 105
Defines Interface
1 /∗ ∗
∗ The Movable i n t e r f a c e d e f i n e s a l i s t o f p u b l i c a b s t r a c t methods
3 ∗ t o be implemented by i t s s u b c l a s s e s
∗/
5 p u b l i c i n t e r f a c e Movable { / / Use keyword " i n t e r f a c e " ( instead of " c l a s s " ) to
// d e f i n e an interface
7 / / An i n t e r f a c e d e f i n e s a l i s t o f p u b l i c a b s t r a c t methods t o be implemented
// by t h e s u b c l a s s e s
9 [public abstract] v o i d moveUp ( ) ; // " public " and " a b s t r a c t " o p t i o n a l
v o i d moveDown ( ) ;
11 void moveLeft ( ) ;
v o i d moveRight ( ) ;
13 }
20 105
Implements interface
1 /∗ ∗
∗ The s u b c l a s s M o v a b l e P o i n t n e e d s t o
3 ∗ implement a l l t h e a b s t r a c t methods
∗ d e f i n e d i n t h e i n t e r f a c e Movable .
5 ∗/
/ / U s i n g keyword implements i n s t e a d o f extends
7 p u b l i c c l a s s M o v a b l e P o i n t implements Movable {
private int x ;
9 private int y ;
11 p u b l i c MovablePoint ( i n t x , i n t y ) { ... }
13 @Override
p u b l i c v o i d moveUp ( ) {
15 y − −;
}
17
@Override
19 p u b l i c v o i d moveDown ( ) {
y ++;
21 }
...
23 }
21 105
Usage of interface
1 p u b l i c c l a s s TestMovable {
p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) {
3 M o v a b l e P o i n t p1 = new M o v a b l e P o i n t ( 1 , 2 ) ;
System . o u t . p r i n t l n ( p1 ) ; // (1 ,2)
5 p1 . moveDown ( ) ;
System . o u t . p r i n t l n ( p1 ) ; // (1 ,3)
7 p1 . moveRight ( ) ;
System . o u t . p r i n t l n ( p1 ) ; // (2 ,3)
9
/ / Usage o f i n t e r f a c e .
11 Movable p2 = new M o v a b l e P o i n t ( 3 , 4 ) ; / / Upcast . I n t e r f a c e as a datatype .
p2 . moveUp ( ) ; / / T e s t polymorphism .
13 System . o u t . p r i n t l n ( p2 ) ; // (3 ,3)
15 M o v a b l e P o i n t p3 = ( M o v a b l e P o i n t ) p2 ; / / Downcast
System . o u t . p r i n t l n ( p3 ) ; // (3 ,3)
17 }
}
22 105
Why interfaces?
An interface is a contract for what the classes can do. It, however, does not specify how the
classes should do it.
An interface provides a form, a protocol, a standard, a contract, a specification, a set of
rules, an interface, for all objects that implement it. It is a specification and rules that any
object implementing it agrees to follow.
Similar to an abstract superclass, an interface cannot be instantiated. You have to create a
"subclass" that implements an interface, and provide the actual implementation of all the
abstract methods.
In Java, abstract class and interface are used to separate the public interface of a class from
its implementation so as to allow the programmer to program at the interface instead of
the various implementation.
23 105
Why interfaces?
One of the main usage of interface is provide a communication contract between two
objects. If you know a class implements an interface, then you know that class contains
concrete implementations of the methods declared in that interface, and you are
guaranteed to be able to invoke these methods safely. In other words, two objects can
communicate based on the contract defined in the interface, instead of their specific
implementation.
Secondly, Java does not support multiple inheritance (whereas C++ does). Multiple
inheritance permits you to derive a subclass from more than one direct superclass. This
poses a problem if two direct superclasses have conflicting implementations. (Which one
to follow in the subclass?). However, multiple inheritance does have its place. Java does
this by permitting you to "implements" more than one interfaces (but you can only
"extends" from a single superclass). Since interfaces contain only abstract methods without
actual implementation, no conflict can arise among the multiple interfaces. (Interface can
hold constants but is not recommended. If a subclass implements two interfaces with
conflicting constants, the compiler will flag out a compilation error.)
24 105
Summary
An abstract class is a class that is incomplete in the sense it has missing method bodies. In
that it describes a set of operations, but is missing the actual implementation of these
operations.
A class is like a set of plans from which you can create objects. In relation to this analogy,
an abstract class is like a set of plans with some part of the plans missing. E.g. it could be a
car with no engine - you would not be able to make complete car objects without the
missing parts of the plan.
25 105
Summary
Any class containing one or more abstract methods is an abstract class.
You must declare the class with the keyword abstract:
a b s t r a c t c l a s s MyClass { ... }
You can declare a class to be abstract even if it does not contain any abstract methods.
▶ This just prevents the class from being instantiated.
26 105
Presentation Outline
1 Abstraction
Abstract Classes
Interfaces
Usage of abstract classes
Usage of interfaces
4 References
27 105
Why abstract classes?
8 c l a s s C i r c l e e x t e n d s Shape {
v o i d draw ( ) { . . . }
10 ...
}
28 105
A Problem
16 f o r ( Shape s : s h a p e s ) {
/ / I l l e g a l , Shape doesn ’ t have draw ( )
18 s . draw ( ) ;
}
29 105
A Solution
c l a s s Shape {
2 void draw() { ... }
}
4
c l a s s S t a r e x t e n d s Shape {
6 void draw() { ... }
...
8 }
10 c l a s s C i r c l e e x t e n d s Shape {
void draw() { ... }
12 ...
}
14
Shape s h ap e ;
16 s ha p e = new Shape ( ) ; // Legal, but unwanted, because Shape does not need to exist in reality.
s ha p e . draw ( ) ; // Legal, but unwanted, because Shape has no specific shape.
18 s ha p e = new S t a r ( ) ; // L e g a l , b e c a u s e a S t a r i s a Shape .
s ha p e . draw ( ) ; // L e g a l , Shape d o e s have draw ( ) .
30 105
A Better Solution
1 abstract c l a s s Shape {
abstract void draw();
3 }
5 c l a s s S t a r e x t e n d s Shape {
void draw() { ... }
7 ...
}
9
c l a s s C i r c l e e x t e n d s Shape {
11 void draw() { ... }
...
13 }
15 Shape s h ap e ;
shape = new Shape(); / / Illegal, Shape is abstract.
17 s ha p e = new S t a r ( ) ; / / L e g a l , b e c a u s e a S t a r i s a Shape .
s ha p e . draw ( ) ; / / L e g a l , Shape d o e s have draw ( ) .
31 105
Another problem
32 105
Presentation Outline
1 Abstraction
Abstract Classes
Interfaces
Usage of abstract classes
Usage of interfaces
4 References
33 105
Interfaces
34 105
Interfaces
1 i n t e r f a c e Shape {
public abstract v o i d s e t I d ( ) ;
3 public abstract v o i d draw ( ) ;
}
5
i n t e r f a c e MovingShape e x t e n d s Shape {
7 public abstract v o i d move ( P o i n t p ) ;
}
35 105
Interfaces
16 c l a s s C i r c l e extends A b s t r a c t S h a p e {
v o i d draw ( ) { . . . }
18 ...
}
36 105
Default methods
37 105
Static methods
16
Shape . g e t D e s c r i p t i o n ( ) ;
38 105
Multiple inheritance
In Java, a class can only extend one class, but can implement multiple interfaces. This lets
the class fill multiple roles (i.e., multiple set of methods).
▶ Collections (e.g., LinkedList) commonly implement multiple interfaces.
▶ Graphical containers (e.g., JFrame) commonly implement several listeners (i.e., interfaces).
c l a s s L i n k e d L i s t e x t e n d s A b s t r a c t L i s t i m p l e m e n t s L i s t , Queue {
2 ...
}
4
c l a s s A p p l i c a t i o n e x t e n d s JFrame i m p l e m e n t s A c t i o n L i s t e n e r , K e y L i s t e n e r {
6 ...
}
39 105
Multiple inheritance
1 p u b l i c class G r o u d V e h i c l e {
public activateWheels ( ) {
3 ...
}
5 }
7 p u b l i c class W a t e r V e h i c l e {
public activateWaterFans ( ) {
9 ...
}
11 }
13
/ / Not allowed in Java‼ Only one class can be extended!
15 p u b l i c class A n p h i b i a n extends G r o u d V e h i c l e , W a t e r V e h i c l e {
...
17 }
40 105
Multiple inheritance
1 p u b l i c interface G r o u d V e h i c l e {
activateWheels ( ) ;
3 }
5 p u b l i c interface W a t e r V e h i c l e {
activateWaterFans ( ) ;
7 }
9
p u b l i c class A n p h i b i a n implements G r o u d V e h i c l e , W a t e r V e h i c l e {
11 activateWheels ( ) {
...
13 }
15 activateWaterFans ( ) {
...
17 }
}
41 105
Interfaces and instanceof
42 105
Presentation Outline
1 Abstraction
4 References
43 105
Program Design Principles
Abstraction
▶ Concentrate on what it CAN DO and NOT how it does it.
▶ E.g. usage of Abstract Class and Interface.
Coupling
▶ Restrict interdependent relationship among classes to the minimum.
Cohesion
▶ A class should be about a single entity only.
▶ There should be a clear logical grouping of all functionalities.
Information Hiding
▶ Expose only necessary information to outside.
44 105
Information Hiding
45 105
Information Hiding
46 105
Pre- and post-conditions
Pre-conditions
▶ Conditions that must be true before a method is called.
▶ "This is what I expect from you".
▶ The programmer is responsible for making sure that the pre-conditions are satisfied when
calling the method.
Post-conditions
▶ Conditions that must be true after the method is completed.
▶ "This is what I promise to do for you".
47 105
Information Hiding
48 105
Data Structure
Data structure is a construct that can be defined within a programming language to store a
collection of data.
▶ Arrays, which are built into Java, are data structures.
▶ We can create other data structures. For example, we want a data structure (a collection of
data) to store both the names and salaries of a collection of employees.
49 105
Abstract Data Type
When a program needs data operations that are not directly supported by a language, you
need to create your own ADT.
You should first design the ADT by carefully specifying the operations before
implementation.
50 105
Abstract Data Type
A WALL of ADT operations isolates a data structure from the program that uses it.
An Interface is what a program/module/class should understand on using the ADT.
51 105
Abstract Data Type
52 105
Primitive Types as ADTs
53 105
Arrays as ADTs
Constructors (to add, create data)
i n t [ ] z = new i n t [ 4 ] ;
2 int [] x = {2 ,4 ,6 ,8};
x [3] = 10;
1 int y = x [3] + x [ 2 ];
54 105
Presentation Outline
1 Abstraction
4 References
55 105
Complex Number as ADT
A complex number comprises a real part a and an imaginary part b, and is written as a + bi.
i is a value such that i2 = −1.
Examples: 12 + 3i, 15 − 9i, −5 + 4i, −23, 18i.
56 105
Designing complex number
Interface
1 c l a s s CartesianComplex { c l a s s PolarComplex {
p r i v a t e double r e a l ; 2 p r i v a t e d o u b l e ang ;
3 p r i v a t e d o u b l e imag ; p r i v a t e d o u b l e mag ;
} 4 }
57 105
Designing complex number
58 105
Complex Number Implementation without Abstraction
c l a s s CartesianComplex {
2 p r i v a t e double r e a l ;
p r i v a t e d o u b l e imag ;
4
p u b l i c Complex ( d o u b l e r e a l , d o u b l e imag ) {
6 this . real = real ;
t h i s . imag = imag ;
8 }
10 p u b l i c double r e a l P a r t ( ) {
return real ;
12 }
14 p u b l i c double imagPart ( ) {
r e t u r n imag ;
16 }
59 105
Complex Number Implementation without Abstraction
p u b l i c d o u b l e modulus ( ) {
2 r e t u r n Math . s q r t ( r e a l ∗ r e a l + imag ∗ imag ) ;
}
4
p u b l i c d o u b l e argument ( ) {
6 i f ( real != 0) {
i f ( real < 0) {
8 r e t u r n ( Math . P I + Math . a t a n ( imag / r e a l ) ) ;
} else {
10 r e t u r n Math . a t a n ( imag / r e a l ) ;
}
12 } e l s e i f ( imag == 0 ) {
return 0;
14 } e l s e i f ( imag > 0 ) {
r e t u r n Math . P I / 2 ;
16 } else {
r e t u r n −Math . P I / 2 ;
18 }
}
60 105
Complex Number Implementation without Abstraction
1 p u b l i c v o i d add ( C a r t e s i a n C o m p l e x c ) {
r e a l += c . r e a l P a r t ( ) ;
3 imag += c . i m a g P a r t ( ) ;
}
5
p u b l i c v o i d minus ( C a r t e s i a n C o m p l e x c ) {
7 r e a l −= c . r e a l P a r t ( ) ;
imag −= c . i m a g P a r t ( ) ;
9 }
61 105
Complex Number Implementation without Abstraction
1 c l a s s PolarComplex {
p r i v a t e d o u b l e modulus ;
3 p r i v a t e d o u b l e argument ;
62 105
Complex Number Implementation without Abstraction
p u b l i c v o i d add ( P o l a r C o m p l e x c ) {
2 double r e a l = t h i s . r e a l P a r t ( ) + c . r e a l P a r t ( ) ;
d o u b l e imag = t h i s . i m a g P a r t ( ) + c . i m a g P a r t ( ) ;
4
modulus = Math . s q r t ( r e a l ∗ r e a l + imag ∗ imag ) ;
6 i f ( real != 0) {
i f ( real < 0) {
8 argument = ( Math . P I + Math . a t a n ( imag / r e a l ) ) ;
} else {
10 argument = Math . a t a n ( imag / r e a l ) ;
}
12 } e l s e i f ( imag == 0 ) {
argument = 0 ;
14 } e l s e i f ( imag > 0 ) {
argument = Math . P I / 2 ;
16 } else {
argument = −Math . P I / 2 ;
18 }
}
63 105
Complex Number Implementation without Abstraction
1 p u b l i c v o i d minus ( P o l a r C o m p l e x c ) {
d o u b l e r e a l = mag ∗ Math . c o s ( ang ) − c . r e a l P a r t ( ) ;
3 d o u b l e imag = mag ∗ Math . s i n ( ang ) − c . i m a g P a r t ( ) ;
64 105
Complex Number Implementation without Abstraction
1 p u b l i c d o u b l e modulus ( ) {
r e t u r n t h i s . modulus ;
3 }
5 p u b l i c d o u b l e argument ( ) {
r e t u r n t h i s . argument ;
7 }
65 105
Complex Number Implementation without Abstraction
1 / / Testing CartesianComplex
C a r t e s i a n C o m p l e x a = new C a r t e s i a n C o m p l e x ( 1 0 . 0 , 1 2 . 0 ) ;
3 C a r t e s i a n C o m p l e x b = new C a r t e s i a n C o m p l e x ( 1 . 0 , 2 . 0 ) ;
a . add ( b ) ;
5 System . o u t . p r i n t l n ( " a = a + b i s " + a ) ;
a . minus ( b ) ;
7 System . o u t . p r i n t l n ( " a − b ( which i s t h e o r i g i n a l a ) i s " + a ) ;
System . o u t . p r i n t l n ( " A n g l e o f a i s " + a . a n g l e ( ) ) ;
9 a . times ( b ) ;
System . o u t . p r i n t l n ( " a = a ∗ b i s " + a ) ;
11
/ / T e s t i n g PolarComplex
13 P o l a r C o m p l e x c = new P o l a r C o m p l e x ( 1 0 . 0 , Math . P I / 6 . 0 ) ;
P o l a r C o m p l e x d = new P o l a r C o m p l e x ( 1 . 0 , Math . P I / 3 . 0 ) ;
15 System . o u t . p r i n t l n ( " c i s " + c) ;
System . o u t . p r i n t l n ( " d i s " + d ) ;
17 c . add ( d ) ;
System . o u t . p r i n t l n ( " c = c + d i s " + c ) ;
19 c . minus ( d ) ;
System . o u t . p r i n t l n ( " c − d ( which i s t h e o r i g i n a l c ) i s " + c ) ;
21 c . t i m e s ( d ) ;
System . o u t . p r i n t l n ( " c = c ∗ d i s " + c ) ;
66 105
Complex Number as ADT
p u b l i c i n t e r f a c e Complex {
2 p u b l i c double r e a l P a r t ( ) ;
p u b l i c double imagPart ( ) ;
4 p u b l i c d o u b l e modulus ( ) ;
p u b l i c d o u b l e argument ( ) ;
6 p u b l i c v o i d add ( Complex c ) ;
p u b l i c v o i d minus ( Complex c ) ;
8 p u b l i c v o i d t i m e s ( Complex c ) ;
}
67 105
Complex Number as ADT
c l a s s CartesianComplex {
2
...
4
p u b l i c v o i d add ( Complex c ) { . . . }
6 p u b l i c v o i d minus ( Complex c ) { . . . }
p u b l i c v o i d t i m e s ( Complex c ) { . . . }
8 }
10 c l a s s PolarComplex {
12 ...
14 p u b l i c v o i d add ( Complex c ) { . . . }
p u b l i c v o i d minus ( Complex c ) { . . . }
16 p u b l i c v o i d t i m e s ( Complex c ) { . . . }
}
68 105
Complex Number as ADT
1 Complex a = new C a r t e s i a n C o m p l e x ( 1 0 . 0 , 1 2 . 0 ) ;
Complex b = new C a r t e s i a n C o m p l e x ( 1 . 0 , 2 . 0 ) ;
3
Complex c = new P o l a r C o m p l e x ( 1 0 . 0 , Math . P I / 6 . 0 ) ;
5 Complex d = new P o l a r C o m p l e x ( 1 . 0 , Math . P I / 3 . 0 ) ;
69 105
Presentation Outline
1 Abstraction
4 References
70 105
List
71 105
List
72 105
List
1 p u b l i c i n t e r f a c e M yL i st {
p u b l i c boolean isEmpty ( ) ;
3 public int size () ;
public Object g e t F i r s t ( ) ;
5 public Object getAt ( i n t index )
p u b l i c boolean c o n t a i n s ( Object item ) ;
7 p u b l i c void i n s e r t F i r s t ( Object item ) ;
p u b l i c void i n s e r t ( i n t index , Object item )
9 public Object removeFirst ( ) ;
p u b l i c O b j e c t remove ( i n t i n d e x ) ;
11 public void print ( ) ;
}
The MyList above defines the operations (methods) we would like to have in a List ADT.
The operations shown here are just a small sample. An actual List ADT usually contains
more operations.
73 105
List
We will consider two implementation of List ADT, both using the interface MyList.
74 105
List Implementation via Array
A fixed-sized List.
75 105
List Implementation via Array
76 105
List Implementation via Array
For insertion into first position, need to shift "right" (starting from the last element) to
create room.
77 105
List Implementation via Array
For deletion of first element, need to shift “left” (starting from the first element) to close
gap.
78 105
List Implementation via Array
79 105
List Implementation via Array
80 105
List Implementation via Linked List
81 105
List Implementation via Linked List
82 105
List Implementation via Linked List
83 105
List Implementation via Linked List
84 105
List Implementation via Linked List
Idea
▶ Each element in the list is stored in a node, which also contains a next pointer (reference).
▶ Allow elements in the list to occupy non-contiguous memory.
▶ Order the nodes by associating each with its neighbour(s).
85 105
List Implementation via Linked List
86 105
List Implementation via Linked List
87 105
List Implementation via Linked List
88 105
List Implementation via Linked List
The insert(int index, Object item) method.
89 105
Enhancements of Linked List
To address the issue that adding to the end is slow, add an extra data member called tail.
90 105
Enhancements of Linked List
91 105
Presentation Outline
1 Abstraction
4 References
92 105
Modelling Tools and Languages
93 105
UML Benefits
Use graphical notation: more clearly than natural language (imprecise) and code (too
detailed).
Help acquire an overall view of a system.
UML is not dependent on any one language or technology.
UML moves us from fragmentation to standardization.
94 105
UML diagrams
95 105
Class Diagram
96 105
Class
p u b l i c c l a s s Car {
2 protected String licensePlate ;
p r o t e c t e d b o o l e a n isOn ;
4
p u b l i c Car ( S t r i n g l i c e n s e P l a t e , b o o l e a n isOn ) {
6 this . license = licensePlate ;
t h i s . isOn = isOn ;
8 }
10 v o i d turnOn ( ) { ... }
14 @Override
public String toString () { ... }
16 }
97 105
Hierachy
1 p u b l i c c l a s s S e l f D r i v i n g C a r e x t e n d s Car {
boolean i s S e l f D r i v i n g ;
3
p u b l i c S e l f D r i v i n g C a r ( b o o l e a n isOn ,
5 String license ,
boolean i s S e l f D r i v i n g ) {
7 s u p e r ( isOn , l i c e n s e ) ;
this . isSelfDriving = isSelfDriving ;
9 }
11 @Override
v o i d turnOn ( ) { ... }
13
@Override
15 void turnOff ( ) { ... }
17 void turnSelfDrivingOn ( ) { . . . }
void turnSelfDrivingOff ( ) { . . . }
19
@Override
21 public String toString () { ... }
}
98 105
Abstract Class
1 p u b l i c abstract c l a s s Shape {
protected String color ;
3 protected boolean f i l l e d ;
5 p u b l i c Shape ( ) { ... }
...
7
public abstract double getArea();
9 public abstract double getPerimeter();
}
11
p u b l i c c l a s s C i r c l e e x t e n d s Shape {
13 p r i v a t e double radius ;
21 @Override
p u b l i c double getPerimeter ( ) { ... }
23 }
99 105
Association
1 p u b l i c c l a s s Customer {
private String id ;
3 p r i v a t e S t r i n g name ;
p r i v a t e L i s t < Order > o r d e r s ;
5
p u b l i c Customer ( ... ) {
7 ...
}
9 }
11 p u b l i c c l a s s Order {
private String itemId ;
13 private String quantity ;
15 p u b l i c Order ( S t r i n g i t e m I d , S t r i n g q u a n t i t y ) {
t h i s . itemId = itemId ;
17 this . quantity = quantity ;
}
19 }
100 105
Association
1 p u b l i c c l a s s Customer {
private String id ;
3 p r i v a t e S t r i n g name ;
5 p u b l i c Customer ( ... ) {
...
7 }
}
9
p u b l i c c l a s s Order {
11 private String itemId ;
private String quantity ;
13 p r i v a t e Customer c u s t o m e r ;
15 p u b l i c Order ( ... ) {
...
17 }
}
101 105
Interface
1 public i n t e r f a c e Movable {
void moveUp ( ) ;
3 void moveDown ( ) ;
void moveLeft ( ) ;
5 void moveRight ( ) ;
}
7
p u b l i c c l a s s M o v a b l e P o i n t i m p l e m e n t s Movable {
9 private int x ;
private int y ;
11
p u b l i c MovablePoint ( ... ) { ... }
13
@Override
15 p u b l i c v o i d moveUp ( ) { ... }
17 ...
}
102 105
A Complete Example
103 105
Presentation Outline
1 Abstraction
4 References
104 105
References
105 / 105
Thank You!