Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Languages and Paradigms Object-Oriented Programming.

Similar presentations


Presentation on theme: "Programming Languages and Paradigms Object-Oriented Programming."— Presentation transcript:

1 Programming Languages and Paradigms Object-Oriented Programming

2 OOP Slide 2 Object-Oriented Programming Objects, classes, fields, methods Encapsulation Inheritance Polymorphism and dynamic binding Program units: Classes

3 OOP Slide 3 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

4 OOP Slide 4 Examples of Objects state/attributes on (true or false) behavior switch on switch off check if on 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 LightBulb Car BankAccount state/attributes balance behavior deposit withdraw check balance 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

5 OOP Slide 5 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

6 OOP Slide 6 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

7 OOP Slide 7 A Class with a Constructor public class BankAccount { private double balance; public BankAccount() { balance = 0; } public double getBalance() { return balance; } public void deposit( double amount ) { balance = balance + amount; } … } BankAccount.java 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 ); Method call Constructor call

8 OOP Slide 8 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)

9 OOP Slide 9 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

10 OOP Slide 10 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

11 OOP Slide 11 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

12 OOP Slide 12 OOP in Java and C++ Program Structure and Execution Encapsulation and Inheritance Objects and Variables Methods Pointers Constructors

13 OOP Slide 13 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

14 OOP Slide 14 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

15 OOP Slide 15 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

16 OOP Slide 16 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

17 OOP Slide 17 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

18 OOP Slide 18 Variables for Built-in Types Variables for built-in types (C++ and Java) int x; … x = 5; 5 X X

19 OOP Slide 19 Reference Variables (in Java) Reference type variables BankAccount x; … x = new BankAccount(100.00); X X 100 BankAccount Object

20 OOP Slide 20 Variables That “hold” Objects (in C++) Declaration of an object variable allocates space for the object BankAccount x(100.00); 100 X

21 OOP Slide 21 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 ); )

22 OOP Slide 22 Pointers (in C++) Variables can be explicitly declared as pointers to objects BankAccount *x; … x = new BankAccount(100.00); X X 100 BankAccount Object

23 OOP Slide 23 Disposing of Allocated 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

24 OOP Slide 24 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?

25 OOP Slide 25 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; }

26 OOP Slide 26 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

27 OOP Slide 27 Next… More advanced OOP features in Java and C++ Arrays Destructors Operator overloading Static vs Dynamic Binding Many others


Download ppt "Programming Languages and Paradigms Object-Oriented Programming."

Similar presentations


Ads by Google