Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Object Oriented Programming with Java Background 70's – procedure oriented programming: break programs into subroutines and libraries 80's – data oriented programming: design subroutines for particular kinds of data simulation was (and still is) a common application of computers these ideas evolved into OO programming Smalltalk first purely OO language, 1980. Why OO? OO promotes reuse of code better quality (less bugs) more efficient use of programmers' time manage complexity decomposition into hierarchies of types encapsulation contains complexity easier to develop components in parallel, which means better use of team resources What is OO? objects are first class citizens in the language computation happens when objects communicate with each other, via messages each object has its own state groups of similar objects form classes every object is an instance of a class classes define behaviour classes are organized into hierarchies OO Terminology object – a collection of related methods and data. An object is an instance of one or more classes. class – a description of a set of objects. method – an operation or behaviour. field – data, object state. message – a request to execute a method. OO Thinking Identify objects Describe behaviours of the object Describe state of the object Group similar objects into classes Organize related classes into hierarchies OOA, OOD, and OOP OOA = Object Oriented Analysis Model a real-world problem by identifying types of objects, relationships, etc. OOD = Object Oriented Design Specify a practical and efficient system in OO terms. Objects may not correspond to real-world. OOP = Object Oriented Programming These techniques are related, and often use the same tools (e.g. Unified Modelling Language – UML). Our main focus is OOP, although we will touch on these other areas. Key features of OO languages Abstract data types (classes) Inheritance Dynamic binding OO Languages Smalltalk is one of the purest, but not in common use Java and C++ are common mainstream choices C++ evolved from C, contains many compromises for compatibility C++ is lower level (exposes more details to the programmer), so it can result in faster programs Java was designed for OO from the start Java hides many low level details from the programmer, which can result in better quality programs Many other languages have incorporated OO features, including Perl, Lisp, and Visual Basic .NET C++ and Java Memory management C++ requires explicit management of the heap with the new and delete operators Java has automatic garbage collection Memory Organization Byte order (big vs little endian) and word size determined by target system architecture in C++ Java Virtual Machine specifies size and byte order of primitive data types (NBO, which is different that your PC!) A few more things about Java... no preprocessor no operator overloading no pointers no raw strings supports dynamic loading of classes native multithreading are you sold yet..? Passing parameters to methods Pass by value Copy the value of the argument for use in the method Changes to the value affect only the copy Pass by reference The called method uses a reference to access the same data as the caller Copying can be expensive with complex data types (like objects). Java does pass by reference, C++ can, but defaults to pass by value (for compatibility with C). Example f( int x ) { x = x + 1 } By value: By reference: int a = 0 f( a ) assert( a == 1 ) int a = 0 f( a ) assert( a == 0 ) An abstract data type defines a set of operations, while leaving the implementation unspecified. For example, a stack is defined by the operations: Abstract data types Push: place an element on the top of the stack Pop: remove the top element from the stack How the elements are stored is a detail that can be ignore by programmers using the stack, only the implementer needs to know how it works internally. Two main features of ADT are: Encapsulation: related methods and data are grouped together into an ADT Information Hiding: implementation details are hidden from the programmer using the ADT (the “client programmer”) Classes in Java (and C++) give us the mechanisms to define ADTs ATDs cont'd encapsulation is a natural consequence of defining methods and fields as members of a class information hiding is done using access specifiers: public, private, and protected ADT example: Stack Let's create a simple Stack class in Java: public class Stack { public Stack() { ... } // push/pop are the only two methods someone using the stack // would be concerned with. public void push( Integer x ) { ... } public Integer pop() { ... } } Stack cont'd Now we have enough code to write “client” code that will use our stack: public class StackMain { public static void main( String[] args ) { // instantiate a Stack object Stack myStack = new Stack(); myStack.push( new Integer(1) ); myStack.push( new Integer(2) ); myStack.push( new Integer(3) ); System.out.print( myStack.pop() + “ “ ); myStack.push( new Integer(4) ); System.out.println( myStack.pop() + “ “ ); System.out.println( myStack.pop() + “ “ ); System.out.println( myStack.pop() + “ “ ); } } Stack cont'd As the implementor of the Stack class, we see: public class Stack { public Stack() { m_elements = new Integer[MAX_ELEMENTS]; } // place an element on the top of the stack public void push( Integer x ) { if ( m_numElements < MAX_ELEMENTS ) { m_elements[m_numElements++] = x; } // else error!! } // remove the top element from the stack, and return it public Integer pop() { if ( m_numElements > 0 ) { return m_elements[--m_numElements]; } // else error!! return null; } // implementation details: private static const int MAX_ELEMENTS = 8; private Integer m_elements[]; private int m_numElements = 0; } Running the test program Now when we run our test program, we will see: 3 4 2 1 Stack cont'd That implementation wasn't very good though, since it could only hold a fixed number of elements. Here's a better one: import java.util.Vector; // for convenience public class Stack { public Stack() { m_elements = new Vector(); } // place an element on the top of the stack public void push( Integer x ) { m_elements.add( x ); } // remove the top element from the stack, and return it public Integer pop() { return (Integer)m_elements.remove( m_elements.size() - 1 ); } // implementation details: private Vector m_elements; } What do we need to change in the client code to use our improved Stack implementation? NOTHING! Update client code for new implementation Our client code is does not have any dependencies on the internal structure of the Stack Our improvements only affected the internal structure ADT Recap group related operations and data together hide the details ADTs are not code, they are ABSTRACT. Next lecture Using interfaces to enforce abstraction.