Programming Languages and Paradigms ObjectOriented Programming 6152005 ObjectOriented
Programming Languages and Paradigms Object-Oriented Programming 6/15/2005
Object-Oriented Programming n Objects, classes, fields, methods Encapsulation Inheritance Polymorphism and dynamic binding n Program units: Classes n n n 2
Object Definition: a thing that has identity, state, and behavior n n n identity: a distinguished instance of a class state: collection of values for its variables behavior: capability to execute methods * variables and methods are defined in a class 3
Examples of Objects n n on (true or false) n behavior n n Light. Bulb n state/attributes n n switch off check if on state/attributes n behavior n n Car n n n state/attributes n n behavior n Bank. Account balance n n deposit withdraw check balance # of liters of gas in tank total # of km run so far efficiency (km/liter) n drive load gas change efficiency check gas check odometer reading Note n each object is an “instance” of that “class” of object n each instance has its own values for its attributes n e. g. , different accounts can have different balances 4
Class Definition: a collection of data (fields/ variables) and methods that operate on that data n n n data/methods define the contents/capabilities of the instances (objects) of the class a class can be viewed as a factory for objects a class defines a recipe for its objects 5
Instantiation n Object creation Memory is allocated for the object’s fields as defined in the class Initialization is specified through a constructor n a special method invoked when objects are created 6
A Class with a Constructor n n n Constructor: special method that handles initialization Java Example: Bank. Account A constructor is invoked during object construction: Bank. Account b; b = new Bank. Account(); b. deposit( 100. 00 ); Constructor call Method call public class Bank. Account { private double balance; Bank. Account. java public Bank. Account() { balance = 0; } public double get. Balance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } 7
Encapsulation n n A key OO concept: “Information Hiding” Key points n n n The user of an object should have access only to those methods (or data) that are essential Unnecessary implementation details should be hidden from the user In Java/C++, use classes and access modifiers (public, private, protected) 8
Inheritance n Inheritance: n n Subclass relationship n n n programming language feature that allows for the implicit definition of variables/methods for a class through an existing class B is a subclass of A B inherits all definitions (variables/methods) in A Superclass variables, subclass objects n Polymorphism and dynamic binding 9
Reuse n n n Inheritance encourages software reuse Existing code need not be rewritten Successful reuse occurs only through careful planning and design n when defining classes, anticipate future modifications and extensions 10
Polymorphism n “Many forms” n n Example: n n allow several definitions under a single method name “move” means something for a person object but means something else for a car object Dynamic binding: n capability of an implementation to distinguish between the different forms during run-time 11
OOP in Java and C++ n n n Program Structure and Execution Encapsulation and Inheritance Objects and Variables Methods Pointers Constructors 12
Program Structure n n Class definition similar in Java and C++ Java: two types of programs n n n application (with main() function) applet (typically embedded in a web page) C++ n n a program is (still) a collection of functions that may use objects and classes main() function serves as driver 13
Program Execution n Java: Virtual Machine (VM) n n programs: both compiled and interpreted compiler produces. class from. java VM loads. class file(s) as needed C++: compiled, linked, and loaded n n n modules separately compiled linked to produce executable static vs dynamic libraries 14
Encapsulation n Enforced through access keywords n n In Java n n public: for interface private: to make implementation inaccessible protected: access for subclasses only each member is prefixed with a keyword In C++ n public, private, and protected sections 15
Inheritance n Feature that allows a class to be defined based on another class n n Java and C++ difference n n n methods and attributes are inherited Java: public class A extends B { … } C++: class A: public B { … } (different types of inheritance) Multiple inheritance possible in C++, not in Java n But in Java, one may implement several interfaces 16
Objects and Identity n Questions: n n n How/when are objects created? What is the relationship between a variable and an object? Difference between Java and C++ n n distinction between primitive (built-in) type variables and variables for objects reference relationship between variable and actual object 17
Variables for Built-in Types n Variables for built-in types (C++ and Java) int x; … x = 5; X X 5 18
Reference Variables (in Java) n Reference type variables X Bank. Account x; … Bank. Account Object X 100 x = new Bank. Account(100. 00); 19
Variables That “hold” Objects (in C++) n Declaration of an object variable allocates space for the object X Bank. Account x(100. 00); 100 20
Methods n n n A method describes a specific behavior applicable to objects of a class A method defines a sequence of instructions (or statements) to be carried out when that method is called A method is called or invoked on an object of the class n Carried out through the dot operator ( e. g. , x. deposit( 1000. 00 ); ) 21
Pointers (in C++) n Variables can be explicitly declared as pointers to objects X Bank. Account *x; X … x = new Bank. Account(100. 00); Bank. Account Object 100 22
Disposing of Allocated Memory n In Java, garbage collection is automatic n n n Memory allocated objects are reclaimed when no variables refer to them Need to set reference variables to null when the object is no longer needed In C++, object destruction is the programmer’s responsibility using the delete keyword 23
delete in C++ n There should be a delete for every new n n Some. Class *x = new Some. Class(…); // … use object pointed to by x delete x; // done using object Memory leak n n n Occurs when you forget to delete Wasted memory Can this occur in Java? 24
Object Construction n Constructor without parameters n n n place where you include code that initializes the object “default” constructior no additional info required Constructor with parameters n n with parameters that specify initial values or sizes Example: public Bank. Account( double init. Bal ) { balance = init. Bal; } 25
Constructors in Java and C++ n In Java, n n n a constructor is invoked only through the new keyword recall that all object variables are references In C++, n n a constructor is called upon variable declaration, or explicitly through new with pointers, or in other situations other types of constructors 26
Next… n n n More advanced OOP features in Java and C++ Arrays Destructors Operator overloading Static vs Dynamic Binding Many others 27
- Slides: 27