Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig
Objects An object has an interface, determined by its class. A class is an abstract data type, or user- defined type. Designing a class means defining its interface.
Built-In Types Think of an int … What is its interface? How do you send it messages? How do you make one? Where does it go when youre done with it?
Example Suppose Ive defined this class in Java: To make one, I type BottleOfJuice myJuice = new BottleOfJuice(); If I want myJuice opened, I say myJuice.open(); Bottle OfJuice
But Why Not Just… This is legal, but just makes a reference variable named myJuice This variable can refer to any BottleOfJuice object, but currently refers to nothing The operator new actually causes an object to be created, so we tell it what kind we want BottleOfJuice myJuice;
Designers Design, Users Use The interface is the critical part, but the details (implementation) are important too. Users use the interface (the public part); the implementation is hidden by access control. Bottle OfJuice
Objects vs. Procedural Libraries C libraries are like this, sort of: The library designer invents a useful struct. Then she provides some useful functions for it. The user creates an instance of the struct, then applies library functions to it. One big difference is that anyone can change any part of the struct. Booo, hsss! Another difference is in initialization.
Two Ways of Reusing Classes Composition: One class has another as a part (indicated by the diamond aggregation symbol). BottleOfJuice CaseOfJuice Shop
Two Ways of Reusing Classes Inheritance: One class is a specialized version of another (indicated by the triangle inheritance symbol). BottleOfJuice BottleOfOrangeJuice
Polymorphism Different subclasses respond to the same message, possibly with different actions. +JuicePlease:Polite +JuicePlease:Rude+JuicePlease:InGerman
Some Java Code Patron p1 = new Patron(); Patron p2 = new YankPatron(); Patron p3 = new BritPatron(); Patron p4 = new GermanPatron(); p1.JuicePlease() // polite request p2. JuicePlease() // rude request p3. JuicePlease() // polite request p4. JuicePlease() // request in German (but polite) Dont forget: Java is a case-sensitive language
Creating Objects We usually assume this is free; with built-in types like int or char, we just say int i; char c; With user-defined types (the ones we make), we need to be explicit about what we want: constructor function This is a very important issue!
Destroying Objects If an object goes out of scope, it can no longer be used (its name is no longer known). In C++, we might need to write an explicit function to free memory allocated to an object. Java uses references and garbage collection.
Example of Object Scope What happens to lect ? The LectureNotes object still exists, but the reference lect disappears (its out of scope after return). Eventually, the garbage collector removes the actual LectureNotes object. public String getTitle(int lectureNumber) { LectureNotes lect; lect = syllabus.getLecture(lectureNumber); String s = lect.getLine(1); return s; }
A Simple Model of Memory The reference variable lect holds a memory location 0x123422C++ Versus Java\n… The LectureNotes object is at that location When you speak of the variable lect, you are referring to the actual LectureNotes object. When lect goes out of scope it is automatically destroyed. The LectureNotes object lives on, but nobody can use it…unless there are other references to it memory
Javas Use of Memory Stack Heap Static variables Constants Non-RAM storage
Javas Primitive Types TypeSizeWrapper type boolean-Boolean char16-bitCharacter byte8-bitByte short16-bitShort int32-bitInteger long64-bitLong float32-bitFloat double64-bitDouble void-Void
Wrapper Types Variables of primitive types are automatic, i.e., they are stored on the stack. They are automatically deleted when they go out of scope. What if you want an object holding a primitive type? Example: char c = x; Character C = new Character(x); Java characters are in Unicode, which is a 16-bit encoding.
Really Big Numbers BigInteger, BigDecimal These are arbitrary precision, as big as they need to be. You cant use the usual operators (+-*/) since they are objects. But there are methods (functions) to do these things.
Creating New Types class MyNewType { // definition here } Now its legal to say MyNewType m = new MyNewType();
Class Members Fields (a.k.a. member variables, data members) Methods (a.k.a. member functions) class MyClass { int a; YourClass b; float memberFunction(int x, float f) { return 0; }
Lets Write Something // Our first program. File: HelloDate.java // Note that the file name is exactly the same // as the class name, including capitalization. import java.util.*; public class HelloDate { public static void main(String[] args) { System.out.println(Hello, it is ); System.out.println(new Date()); }
The Major Issues Editing Use any text editor you like (not a word processor!); save as HelloDate.java Compiling From a DOS or UNIX command line, type > javac HelloDate.java This should produce the file HelloDate.class Running Again from the command prompt, type > java HelloDate Output: Hello, it is: