1 / 27

Programming Languages and Paradigms

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

rmanuel
Download Presentation

Programming Languages and Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Languagesand Paradigms Object-Oriented Programming

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

  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. 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

  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. 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. 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

  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. 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. 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. 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 in Java and C++ • Program Structure and Execution • Encapsulation and Inheritance • Objects and Variables • Methods • Pointers • Constructors

  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. 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. 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. 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. 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. Variables for Built-in Types • Variables for built-in types (C++ and Java) int x; … x = 5; X X 5

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

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

  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. Pointers (in C++) • Variables can be explicitly declared as pointers to objects BankAccount *x; … x = new BankAccount(100.00); X BankAccount Object 100 X

  23. 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

  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. 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. 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. Next… • More advanced OOP featuresin Java and C++ • Arrays • Destructors • Operator overloading • Static vs Dynamic Binding • Many others

More Related