270 likes | 302 Views
Programming Languages and Paradigms. Object-Oriented Programming. Object-Oriented Programming. Objects, classes, fields, methods Encapsulation Inheritance Polymorphism and dynamic binding Program units: Classes. Object. Definition: a thing that has identity, state, and behavior
E N D
Programming Languagesand Paradigms Object-Oriented Programming
Object-Oriented Programming • Objects, classes, fields, methods • Encapsulation • Inheritance • Polymorphism and dynamic binding • Program units: Classes
Object Definition: a thing that has identity, state, and behavior • 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
Note • each object is an “instance” of that “class” of object • each instance has its own values for its attributes • e.g., different accounts can have different balances Examples of Objects • state/attributes • # of liters of gas in tank • total # of km run so far • efficiency (km/liter) • behavior • drive • load gas • change efficiency • check gas • check odometer reading • state/attributes • on (true or false) • behavior • switch on • switch off • check if on LightBulb Car • state/attributes • balance • behavior • deposit • withdraw • check balance BankAccount
Class Definition: a collection of data (fields/ variables) and methods that operate on that data • 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
Instantiation • Object creation • Memory is allocated for the object’s fields as defined in the class • Initialization is specified through a constructor • a special method invoked when objects are created
BankAccount.java public class BankAccount { private double balance; public BankAccount() { balance = 0; } public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } A Class with a Constructor • Constructor: special method that handles initialization • Java Example: BankAccount • A constructor is invoked during object construction:BankAccount b;b = new BankAccount();b.deposit( 100.00 ); Constructor call Method call
Encapsulation • A key OO concept: “Information Hiding” • Key points • 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)
Inheritance • Inheritance: • programming language feature that allows for the implicit definition of variables/methods for a class through an existing class • Subclass relationship • B is a subclass of A • B inherits all definitions (variables/methods) in A • Superclass variables, subclass objects • Polymorphism and dynamic binding
Reuse • Inheritance encourages software reuse • Existing code need not be rewritten • Successful reuse occurs only through careful planning and design • when defining classes, anticipate future modifications and extensions
Polymorphism • “Many forms” • allow several definitions under a single method name • Example: • “move” means something for a person object but means something else for a car object • Dynamic binding: • capability of an implementation to distinguish between the different forms during run-time
OOP in Java and C++ • Program Structure and Execution • Encapsulation and Inheritance • Objects and Variables • Methods • Pointers • Constructors
Program Structure • Class definition similar in Java and C++ • Java: two types of programs • application (with main() function) • applet (typically embedded in a web page) • C++ • a program is (still) a collection of functions that may use objects and classes • main() function serves as driver
Program Execution • Java: Virtual Machine (VM) • programs: both compiled and interpreted • compiler produces .class from .java • VM loads .class file(s) as needed • C++: compiled, linked, and loaded • modules separately compiled • linked to produce executable • static vs dynamic libraries
Encapsulation • Enforced through access keywords • public: for interface • private: to make implementation inaccessible • protected: access for subclasses only • In Java • each member is prefixed with a keyword • In C++ • public, private, and protected sections
Inheritance • Feature that allows a class to be defined based on another class • methods and attributes are inherited • Java and C++ difference • Java: public class A extends B { … } • C++: class A: public B { … }(different types of inheritance) • Multiple inheritance possible in C++, not in Java • But in Java, one may implement several interfaces
Objects and Identity • Questions: • How/when are objects created? • What is the relationship between a variable and an object? • Difference between Java and C++ • distinction between primitive (built-in) type variables and variables for objects • reference relationship between variable and actual object
Variables for Built-in Types • Variables for built-in types (C++ and Java) int x; … x = 5; X X 5
Reference Variables(in Java) • Reference type variables BankAccount x; … x = new BankAccount(100.00); X BankAccount Object 100 X
Variables That “hold” Objects (in C++) • Declaration of an object variable allocates space for the object BankAccount x(100.00); X 100
Methods • 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 • Carried out through the dot operator( e.g., x.deposit( 1000.00 ); )
Pointers (in C++) • Variables can be explicitly declared as pointers to objects BankAccount *x; … x = new BankAccount(100.00); X BankAccount Object 100 X
Disposing ofAllocated Memory • In Java, garbage collection is automatic • 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
delete in C++ • There should be a delete for every new • SomeClass *x = new SomeClass(…); • // … use object pointed to by x • delete x; // done using object • Memory leak • Occurs when you forget to delete • Wasted memory • Can this occur in Java?
Object Construction • Constructor • place where you include code that initializes the object • Constructor without parameters • “default” constructior • no additional info required • Constructor with parameters • with parameters that specify initial values or sizes • Example: public BankAccount( double initBal ) { balance = initBal; }
Constructors in Java and C++ • In Java, • a constructor is invoked only through the new keyword • recall that all object variables are references • In C++, • a constructor is called upon variable declaration, or explicitly through new with pointers, or in other situations • other types of constructors
Next… • More advanced OOP featuresin Java and C++ • Arrays • Destructors • Operator overloading • Static vs Dynamic Binding • Many others