Lecture 01
Learning goal
1) Learning efficient data structures and algorithms that use them. 2) Designing and writing large programs. 3) Understanding and designing data abstraction and interfaces. 4) Learning Java.
Object-Oriented programming
- Object: A repository of data.
- Class: Type of object.
- Method: Procedure or function that operates on an object or class.
- Inheritance: A class may inherit properties from a more general class.
- Polymorphism: One method works on several classes even if the classes need different implementations.
addItem
method on every kind of list, though adding item to a ShoppingList is different from a ShoppingCart.
- Object-Oriented: Each object knows its own class & methods.
- Each ShoppingList & ShoppingCart knows which implementation applies to it.
- Polymorphism is the thing that truly distinguishes object-oriented languages from procedural languages (C, Fortran, Basic, Pascal).
Java
int x; x = 1;
This Java declaration does two things.
1) allocate a chunk of memory. 2) name the variable (chunk of memory) "x".
---
x |1|
---
String myString;
This does not create a String object. Instead, it declares a variable that can store a reference to a String object.
myString = new String();
It does two things.
new String()
is called a constructor, which constructs a new String object.- The assignment
=
causes myString to reference the object.+--+ +--------+ MyString | +------->+ | a String object +--+ +--------+
Java Compilation
javac java
.java +-----> .class +-----> answer
Instance variables get default value. (int: 0; float: 0.0; booleans: false; references: null)