Object Oriented Programming CMSC 331. Concept of Abstraction “An abstraction is a view or representation of an entity that includes only the attributes.

Slides:



Advertisements
Similar presentations
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
Advertisements

MT311 (Oct 2007) Java Application Development Object-Oriented Programming Languages Tutorial 8.
Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object-Oriented Programming
Object-Oriented Programming CS 3360 Spring 2012 Sec , Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
ISBN Chapter 12 Support for Object-Oriented Programming.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Object-Oriented Programming
Lecture 9 Concepts of Programming Languages
CS884 (Prasad)Java Goals1 “Perfect Quote” You know you've achieved perfection in design, Not when you have nothing more to add, But when you have nothing.
Abstract Data Types and Encapsulation Concepts
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
ISBN Chapter 12 Support for Object- Oriented Programming.
C++ fundamentals.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Lecture 1: Overview of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed.
OOP Languages: Java vs C++
Comparison of OO Programming Languages © Jason Voegele, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
CSCI 224 Introduction to Java Programming. Course Objectives  Learn the Java programming language: Syntax, Idioms Patterns, Styles  Become comfortable.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 11 and 12: Abstract Data Types and Encapsulation Constructs; Support for Object Orientation Lesson 11.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Java Introduction Lecture 1. Java Powerful, object-oriented language Free SDK and many resources at
CS 403 – Programming Languages Class 25 November 28, 2000.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
Programming Languages and Paradigms Object-Oriented Programming.
Introduction and Features of Java. What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++
Introduction to Object Oriented Programming CMSC 331.
Chapter 12 Support for Object-Oriented Programming.
1 Abstract Data Types & Object Orientation Abstract Data Types (ADT) Concepts –Data Abstraction –ADT in PLs –Encapsulation Object Orientation –Principal.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
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.
Chapter 12 Support for Object-Oriented Programming.
Object-Oriented Programming Chapter Chapter
(1) ICS 313: Programming Language Theory Chapter 12: Object Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
ISBN Object-Oriented Programming Chapter Chapter
OOP Review CS 124.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
(1) ICS 313: Programming Language Theory Chapter 11: Abstract Data Types (Data Abstraction)
Classes, Interfaces and Packages
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
A Survey of Object-Oriented Concept Oscar Nierstrasz.
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Design issues for Object-Oriented Languages
Applications Active Web Documents Active Web Documents.
Abstract Data Types and Encapsulation Concepts
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Types of Programming Languages
Abstract Data Types and Encapsulation Concepts
From C++ to Java Java history: Oak, toaster-ovens, internet language, panacea What it is O-O language, not a hybrid (cf. C++) compiled to byte-code, executed.
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Object Oriented Programming CMSC 331

Concept of Abstraction “An abstraction is a view or representation of an entity that includes only the attributes of significance in a particular context.” ~Sebesta, pg. 434 –Process abstraction –Data abstraction

Encapsulation Beyond a certain size, process-abstraction is no longer sufficient to organize large programs Modularization – the organization of code into logically related processes and data Encapsulation – organization of … into a separately or independently compilable unit

Data Abstraction Abstract Data Type (ADT) is an encapsulation containing the data representation and methods for one specific data type.

Data Abstraction: Formally An abstract data type is a data type that satisfies the following two conditions: –The representation, or definition, of the type and the operations on objects of the type are contained in a single syntactic unit. Also, other program units may be allowed to create variables of the defined type. –The representation of objects of the type is hidden from the program units that use the type, so the only direct operations possible on those objects are those provided in the type’s definition.

Advantages Helps to organize program into logical units. Reduces code interdependencies by creating interface ‘boundaries’ – enhancing modification and reuse. Improves reliability by keeping details ‘hidden’.

Example Simple Data Types –Double More Complex –Stack, Queue, Hashtable Very Complex –Tax Return

Design Issues Facilities must be present for encapsulation of type definition and methods Basic objects should have minimal definitions Some entities will be common to most objects

ADT in C++ Both imperative and object type models ADTs implements as Classes, based on Classes of Simula67 –Data elements are data members –Functions are member functions Can be static, stack- or heap-dynamic, and have heap-dynamic members Member functions can be defined in place or by header only Members are private or public

ADT in Java ADTs implemented as Classes All Class instances are heap-dynamic Methods are only defined in the context of Classes Private and public modifiers are applied directly to members Java adds the package, and package scope (like ‘friend’), as an encapsulation construct beyond class

Object Oriented Programming Support for: –Abstract Data Types –Inheritance –Dynamic Binding Precursors: –Procedure oriented programming –Data oriented programming

Inheritance ADTs can be derived from existing ADTs Properties of parent class (superclass) are inherited (by the subclass) Child Classes can add new entities, or override entities from the parent

Inheritance: Benefits Facilitates reuse Allows structuring of Classes into logical hierarchies

Polymorphism Polymorphic variables can hold a value of more than one type –e.g. if B is a derived Class of Class A, and variable x has type A, x can hold a value of type B as well

Polymorphism: Benefits Allows for more flexible code Supports operations on generic objects Provides for use of virtual classes Drawback: Does make type checking more difficult; requires more caution at runtime

Application of OO Language “An executing program in an object-oriented language can be described as a simulation of a collection of computers that communicate with each other through messages.” (Sebesta p. 463) Invoking the method of a class is ‘sending a message’ Collection of methods define a ‘message protocol’ Identify the objects and interactions in your problem, and simulate them

Design Issues Exclusivity of Objects Are Subclasses Subtypes? Implementation and Interface Inheritance Type Checking and Polymorphism Single and Multiple Inheritance Allocation and Deallocation of Objects Dynamic and Static Binding

Issues: Exclusivity of Objects In purest form, ‘everything’ is an object –Benefit of elegance in simplicity –Drawback in that message interface is slower Compromises: –Retain a complete, imperative typing model, add object model –Retain imperative model only for primitive, scalar types

Issues: Subclass = Subtype? Generally, this reduces to the question of whether a subclass inherits all entities of the parent class –Where entities are overriden, they should be compatible

Issues: Inheritance Interface Inheritance –Only the interface of the superclass is seen by the subclass Implementation Inheritance –The implementation details of the superclass are available to the subclass

Issues: Type Checking Strong (static) typing implies –Checking message parameter types against formal parameters –Checking return types against expected return type Dynamic type checking delays check until method call; more expensive, and delays error detection

Issues: Single/Multi-Inheritance Multiple inheritance is of clear value when derived classes should have properties of more than one parent Drawbacks: –Complexity (e.g. name collisions) –Perceived problem of efficiency

Issues: Allocation Allocation –Should objects be static, stack-dynamic, or heap-dynamic Should deallocation be implicit or explicit?

Issues: Dynamic/Static Binding Dynamic binding an essential part of an OO language Alternative; allow user to specify whether dynamic or (faster) static binding is to be used

Summary OO = ADT + Inheritance + Dynamic Binding OO Languages support this with Classes, methods, objects, and message passing Some languages, like C++, are hybrid Others, like Java support only OO

A Few Words on Java… CMSC 331

Java is… Object oriented! Portable! The language of the WWW!

OO The Java programming language provides support for: –Abstract Data Types (classes) –Inheritance (single, with some extensions) –Polymorphism

Portability Java is a hybrid language –Source is compiled to byte code –Byte code is interpreted by a Java Virtual Machine (JVM) JVM has been ported to many platforms Byte code for a Java program should run on any JVM Programs are not 100% portable

Memory Management JVM implements garbage collection –No explicit deallocation of objects –Unreferenced objects are periodically recycled automatically

Self-Documenting Code Java supports the writing of self- documenting code: –A simple markup language for use in source code, which allows embedded html –A documentation compiler, ‘javadoc’, which creates easy to read html Class documentation.

Everything is a Class From basic types, to programs Language contains certain primitive types –int, double –Arrays –… Primitive data types have analogs in the Class hierarchy –Integer, Double, …

Class Hierarchy Everything is an Object

Single Inheritance A class may be ‘derived’ from only one parent class –Class A extends Class B Java improves on this by allowing interfaces (partially defined classes); several interfaces can be used to constrain a new Class –Class A implements Classes C, D and E

Java Standard Libraries Many useful Classes for: –Graphics/Windowing (AWT, Swing) –Internet Programming (Applet) –Math –Basic Data Structures (Vector, HashTable)

Packages Classes can belong to named collections, called packages Source/Classes must be in a directory on your classpath Named subdirectories correspond to named packages: –Classpath = C:\Java\myfiles –Subdirectory C:\Java\myfiles\examples\new contains Classes of package examples.new

Exception Handling Error conditions generate exceptions Like everything else in Java, exceptions are Classes –Predefined –User defined Exceptions can be thrown by a method, or caught and handled

Security Execution inside a virtual machine makes it possible to tightly control access to machine resources –Disk –Network –… There are facilities for digitally signing code

Jar Classes can be bundled together in jar files Jar works just like tar (Unix) Jar files can be used as libraries

Threading Java provides good support for writing multi-threaded programs, and managing concurrency. –Thread Class –Low-level synchronization primitives

Memory? What Memory? No pointers No pointer arithmetic No memory hassles Only references to Class instances

Where is Java??? “I want to use it on UMBC Unix Machines”: –linux1.gl.umbc.edu (RedHat 7.2)has: Java 1.2.2_006 (Classic VM), green threads, nojit –solaris1.gl.umbc.edu (SunOS) has: Java 1.2.2_08 (Solaris VM), native threads, sunwjit “I want to run it on my own Windows/Solaris/Linux Machine”: –Download Sun’s JDK from –Java 2 Platform, Standard Edition, v And there are other sources…

What Do I Do? Create a file with a.java extension Run javac on that file: –javac myfile.java Now you have a Class file (myfile.class); run it: –java myfile

Where Do I Find Examples? Your Java book, On To Java, can be found online at: –