Java-OOP-data-abstraction-emoon
Java-OOP-data-abstraction-emoon
2
Why OOP?
Essential features of OOP
• Abstraction
• Applying the abstraction paradigm to the design of data
structures gives rise to abstract data type (ADTs).
• An ADT specifies what each operation does, but not how it
does it; expressed by an interface.
• An ADT is realized by a concrete data structure, which is
modeled in Java by a class.
Primitive types
• values immediately map to
machine representations
• operations immediately map to
machine instructions.
An ADT specifies what each operation does, but not how it does it.
In Java,
• An ADT can be expressed by an interface, which is simply a list of
method declaration, where each method has an empty body.
Best practice: Use abstract data types (representation is hidden from the client).
8
Strings
We have already been using ADTs!
defined in terms of its ADT
A Stringis a sequence of Unicode characters. values (typical)
In the Java programming language, strings are objects.
10
O perations (API)
11
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
Examples:
noon, mom, dad, level, kayak, refer.
We can write clear and simple client code that uses numerous
instance methods specified in the API without knowing the internal
implementation of those methods.
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html 14
a a c a a g t t t a c a a g c x 15 x+9 4 x+1 4
memory length
address
Implications
• A constructor creates an object and returns to the client a reference to
that object, not the object itself (i.e., reference type).
• s and t are different strings that have the same value "acaa".
• (s == t) is false (because it compares addresses). 15
2. What is an API?
https://introcs.cs.princeton.edu/java/32class/
19
API, clients, & implementations
Client API Implementation
Client A contract between the client and Java code that implements
A program that uses the implementation the methods in an API, kept
by convention in a file with
methods specified in
the library name and a .java
API. extension.
1) Specify an API.
The purpose of the API is to separate clients from implementations, to
enable modular programming.
API
(operations)
23
a = 3.0 + 4.0i
System.out.println("c = " + a.times(b));
b = -2.0 + 3.0i
a * b = -18.0 + 1.0i
System.out.println("Re(a) = "+a.re());
System.out.println("Im(a) = "+a.im());
} What we expect,
once the implementation is done.
24
Values
methods
….
complex number 3 + 4i −2 + 2i
real part 3.0 −2.0
imaginary part 4.0 2.0
// Constructor
public Complex(double re, double im) {
this.re = re; //this refers to the object known as thing in the caller's context.
this.im = im; //set the instance variable equal to argument.
}
} references to instance variables, which are not declared within the constructor
27
©Robert
2014Sedgewick
Goodrich,| Kevin
Tamassia,
WayneGoldwasser Also see Goodrich et al (p.15)
Check point
28
©Robert
2014Sedgewick
Goodrich,| Kevin
Tamassia,
Wayne
Goldwasser
Scope
• The scope of instance and static
variables is the entire class
regardless of where the variables
are declared.
34
35
37
Client
// sample client
public static void main(String[] args) {
Student std1 = new Student("Harry", "Potter", "101", "Computer Science");
Student std2 = new Student("Hermione", "Granger", "1010", "Computational Biology");
…
}
38
101
Computer Science Harry Potter
Computational Biology Hermione Granger
Biochemistry Hermione Granger
• Each class can define its own main( ) method for unit testing and debugging.
• At a minimum, the test client should call every constructor and instance methods
in the class. 39
40
44