Programming Languages and Paradigms Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

ITEC200 – Week03 Inheritance and Class Hierarchies.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Object-Oriented PHP (1)
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
OOP Languages: Java vs C++
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
LECTURE LECTURE 17 More on Templates 20 An abstract recipe for producing concrete code.
OOP in Java and C++ CS 123/CS 231. Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors.
Eclipse – making OOP Easy
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Guided Notes Ch. 9 ADT and Modules Ch. 10 Object-Oriented Programming PHP support for OOP and Assignment 4 Term project proposal C++ and Java Designer.
Introduction to Object Oriented Programming CMSC 331.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
Programming Languages and Paradigms Programming C++ Classes.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
OOP in Java and C++ CS 123/CS 231. Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors.
Programming Languages and Paradigms Imperative Programming.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Object Oriented Programming
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
ISBN Object-Oriented Programming Chapter Chapter
Copyright © 2005 Elsevier Object-Oriented Programming Control or PROCESS abstraction is a very old idea (subroutines!), though few languages provide it.
OOP Review CS 124.
1 CS Programming Languages Class 22 November 14, 2000.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Inheritance ndex.html ndex.htmland “Java.
Object-Oriented Programming (Review) CS 123 Key OOP Concepts zObject, Class zInstantiation, Constructors zEncapsulation zInheritance and Subclasses zAbstraction.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
ISBN Chapter 12 Support for Object-Oriented Programming.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 5: Enhancing Classes
Inheritance and Polymorphism
SNSCT_CSE_PROGRAMMING PARADIGM_CS206
Java Programming Language
Presentation transcript:

Programming Languages and Paradigms Object-Oriented Programming

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 2 Object-Oriented Programming Objects, classes, fields, methods Encapsulation inheritance Abstraction and reuse Polymorphism and dynamic binding Program units: Classes

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 4 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/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 5 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 6 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)

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 7 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 8 Abstraction OOP is about abstraction Encapsulation and Inheritance are examples of abstraction What does the verb “abstract” mean?

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 9 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 10 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 11 Java and C++ Comparison Program Structure and Execution Encapsulation and Inheritance Objects, Variables, and Arrays Constructors Methods, Operators, and Binding Containers and Reuse GUI Programming

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 12 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 13 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 14 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 15 Breaking Encapsulation Possible in C++ through the friend keyword A method or class may be declared as a friend of an existing class Allows access to private members

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 18 Variables for Built-in Types Variables for built-in types (C++ and Java) int x; … x = 5; 5 X X

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 19 Reference Variables (in Java) Reference type variables Button x; … x = new Button(“click”); X X “click” Button Object

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 20 Variables That “hold” Objects (in C++) Declaration of an object variable allocates space for the object Button x(“click”); “click” X

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 21 Pointers (in C++) Variables can be explicitly declared as pointers to objects Button *x; … x = new Button(“click”); X X “click” Button Object

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 22 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 23 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?

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 24 Object Construction Constructor place where you include code that initializes the object Default Constructor no additional info required User-defined Constructor with parameters that specify values or sizes

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 25 Arrays int x[20]; Button b[20]; Valid declarations in C++, not in Java Creates 20 ints and 20 Button objects In Java, Declaration and array creation separate For object arrays, individual object creation necessary

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 26 Pointers and Arrays In C++, there is a close relationship between pointers and arrays Instead of int x[20]; can issue int *x; x = new int[20]; to allow for dynamic allocation Usage of the array (e.g., x[3] = 5;) identical in both cases To deallocate, use delete [] x;

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 27 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

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 28 C++ Destructor Special method whose signature is a ~ followed by the name of the class e.g., ~SomeClass(); Particularly if the class contains pointers and the constructor contains calls to new, a destructor needs to be defined e.g., SomeClass() { A = new int[20]; } ~SomeClass() { delete [] A; }

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 29 C++ Control Over Copy and Assignment In C++, the semantics of “a = b” (assignment) can be specified by defining the copy-assignment operator In C++, there is a copy constructor specifies what happens during object copying, e.g., when function parameters are passed There is more low-level control shallow copy vs deep copy

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 30 Methods Defines object behavior Static methods vs instance methods Method overloading within class, two methods with the same name but different signatures Method overriding same signatures across different classes (subclass and superclass)

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 31 Operators In C++, operators like =, +, *, ==, etc. can be defined, just like methods Example: class Matrix { //... Matrix operator+(Matrix m) { … } // … } c = a + b; // equiv to c = a.operator+(b);

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 32 Method Binding Let Teacher be a subclass of Employee Also, suppose promote() is a method defined in both classes Employee variables can refer to Teachers In Java, Employee e; … e = new Teacher(); In C++, Employee *e; … e = new Teacher; e.promote() (or e->promote() ) calls which promote() method?

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 33 Static vs Dynamic Binding In C++, Employee’s promote() is called Determined at compile time and deduced from the type of the variable (static binding) In Java, Teacher’s promote is called Determined at run-time because the actual type of the referred object is checked then (dynamic binding) * C++ uses virtual functions for dynamic binding

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 34 Static Binding Pointer is typed to know the base class and is ignorant of the structure or existence of the derived classes If the virtual keyword is NOT used, if a derived class has its own variation on the implementation of a base class member function, it will NOT cause the derived class version to be selected when a function is invoked on an object of than class through a variable declared in terms of the base class.

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 35 Another example class Employee{ public: double salary() {return sal;} double computeRaise() {return 25;} Employee(double salary) {sal = salary;} private: double sal; }; class Manager: public Employee { public: double computeRaise() {return 100;} Manager(double Salary) : Employee (salary) {} };

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 36 Sample continued Driver Code: Manager * boss1 = new Manager(2000); double boss1Salary = boss1->salary(); // 2000 Employee *boss2 = new Manager(2300); double *boss2Salary = boss2->salary(); //2300 double boss1Raise = boss1->computeRaise(); // 100 double boss2Raise = boss2->computeRaise(); // 25

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 37 C++ Run Time Binding If the intent is for the selection of the function to be determined by the object’s class, not by the declaration of the pointer used to address it:  Declare some base class members to be virtual  If virtual, the compiler will deposit the “type field” of the class in the object

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 38 Virtual Functions Base class usually defines a body for a virtual function. Inherited by derived class as default if it chooses not to override the implementation Virtual keyword in function declaration, not in definition

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 39 Containers Examples: Lists, Stacks, Files, etc. Structures that “contain” elements Often, the element’s type has little or nothing to do with the containers’ operations Possible room for re-use unified container code for a stack of integers, a stack of webpages, a stack of strings,...

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 40 Java and the Object Hierarchy All classes extend the Object class: A variable of class Object can refer to any Java object Example: public class Stack { Object A[]; int top; // … void push(Object elt) //... }

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 41 C++ and Templates Templates allow for a generic definition parameterized definition, where the element type is the parameter Example: template class Stack { T A[MAX]; int top; public: void push(T element) // … }

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 42 C++ Templates indicates that a template is being declared T is the type name (can be a class) Usage example: Stack iStack; Stack cStack; where Cards is a user defined class A type used as a template argument must provide the interface expected by the template

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 43 Defining a Template When defining a template member outside of its class, it must be explicitly declared a template Example template Stack ::Stack()

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 44 C++ Standard Containers Vector : 1-D array of T list : double linked list of T dequeue : double-ended queue of T queue : queue of T stack : stack of T map : associative array of T set : set of T bitset : set of booleans

6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L6: OOP Slide 45 GUI Programming In Java, GUI is part of its development kit java.awt.* is a collection of classes that support visual programming and graphics visual objects (buttons, text fields, etc), layout managers, events, etc. In C++ not part of the language libraries dependent on platform (e.g., MFCs and Motif)