Programming Languages and Paradigms Object-Oriented Programming.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Chapter 3 Implementing Classes. Instance Variables Instance variables store the data of an object; the fields of an object. Instance of a class: an object.
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
16/22/2015 2:54 PM6/22/2015 2:54 PM6/22/2015 2:54 PMObject-Oriented Development Concept originated with simulating objects and their interactions. Adapted.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Programming Review (Part I) Enterprise Systems Programming.
OOP Languages: Java vs C++
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Eclipse – making OOP Easy
Writing Classes (Chapter 4)
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Objective-C and Xcode (Part 2) FA 175 Intro to Mobile App Development.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Introduction to Object-Oriented Programming
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Introduction to Java and Object-Oriented Programming AJSS Computer Camp Department of Information Systems and Computer Science Ateneo de Manila University.
JavaScript, Fourth Edition
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Programming Languages and Paradigms Object-Oriented Programming.
CSE 425: Object-Oriented Programming I Object-Oriented Programming A design method as well as a programming paradigm –For example, CRC cards, noun-verb.
Classes CS 21a: Introduction to Computing I First Semester,
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
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.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
OOP in Java and C++ CS 123/CS 231. Outline zProgram Structure and Execution zEncapsulation and Inheritance zObjects, Variables, and Arrays zConstructors.
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.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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
OOP Review CS 124.
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.
Object-Oriented Programming (Review) CS 123 Key OOP Concepts zObject, Class zInstantiation, Constructors zEncapsulation zInheritance and Subclasses zAbstraction.
CSCI 171 Presentation 15 Introduction to Object–Oriented Programming (OOP) in C++
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Chapter 3 Implementing Classes
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Classes C++ representation of an object
Inheritance and Polymorphism
Chapter 3: Using Methods, Classes, and Objects
SNSCT_CSE_PROGRAMMING PARADIGM_CS206
Object-Oriented Programming
Introduction to Object-Oriented Programming
Classes C++ representation of an object
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

Programming Languages and Paradigms Object-Oriented Programming

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

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

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

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

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

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( ); Method call Constructor call

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)

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

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

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

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

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

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

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

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

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

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

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

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

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( ); )

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

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

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?

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; }

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

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