Lecture Notes on Programming Languages

Similar documents
Lecture 13: Object orientation. Object oriented programming. Introduction. Object oriented programming. OO and ADT:s. Introduction

Lecture Notes on Programming Languages

CPS 506 Comparative Programming Languages. Programming Language

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

Concepts of Programming Languages

C++ Important Questions with Answers

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Data Abstraction. Hwansoo Han

Inheritance and Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism

What are the characteristics of Object Oriented programming language?

Lecture Notes on Programming Languages

Lecture 15: Object-Oriented Programming

Object Oriented Paradigm

Chapter 5 Object-Oriented Programming

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Inheritance and Interfaces

CS-202 Introduction to Object Oriented Programming

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Inheritance and Polymorphism

Inheritance and Polymorphism

Lecture 23: Object Lifetime and Garbage Collection

COSC252: Programming Languages: Abstraction and OOP. Jeremy Bolton, PhD Asst Teaching Professor. Copyright 2015 Pearson. All rights reserved.

Programming Exercise 14: Inheritance and Polymorphism

Some instance messages and methods

VIRTUAL FUNCTIONS Chapter 10

Compaq Interview Questions And Answers

Subtyping (Dynamic Polymorphism)

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

What is Inheritance?

CSE 452: Programming Languages. Previous Lecture. From ADTs to OOP. Data Abstraction and Object-Orientation

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Programming II (CS300)

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

CS558 Programming Languages

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

Object Oriented Programming

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Programming Languages & Paradigms PROP HT Course Council. Subprograms. Meeting on friday! Subprograms, abstractions, encapsulation, ADT

Object-oriented Programming. Object-oriented Programming

CS558 Programming Languages Winter 2013 Lecture 8

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

Polymorphism. Arizona State University 1

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Programming II (CS300)

CSE 307: Principles of Programming Languages

Chapter 11 Object and Object- Relational Databases

BBM 102 Introduction to Programming II Spring Inheritance

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

CMSC 132: Object-Oriented Programming II

Introduction to Programming Using Java (98-388)

Object Oriented Issues in VDM++

CS250 Intro to CS II. Spring CS250 - Intro to CS II 1

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CSE 401/M501 Compilers

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Practice for Chapter 11

Chapter 9. Subprograms

Subprograms. Copyright 2015 Pearson. All rights reserved. 1-1

Inheritance. OOP: Inheritance 1

1: Introduction to Object (1)

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Object Oriented Programming with c++ Question Bank

The major elements of the object-oriented model

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview


Names, Scopes, and Bindings II. Hwansoo Han

CSE 303: Concepts and Tools for Software Development

Chapter 9. Subprograms

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Type Hierarchy. Lecture 6: OOP, autumn 2003

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

B.C.A 2017 OBJECT ORIENTED PROGRAMMING USING C++ BCA303T MODULE SPECIFICATION SHEET

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Comp 311 Principles of Programming Languages Lecture 21 Semantics of OO Languages. Corky Cartwright Mathias Ricken October 20, 2010

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

CS 403/503 Exam 4 Spring 2015 Solution

ECE 3574: Dynamic Polymorphism using Inheritance

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Learning Objectives. C++ For Artists 2003 Rick Miller All Rights Reserved xli

Concepts of Programming Languages

G Programming Languages - Fall 2012

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Abstract Data Types and Encapsulation Concepts

Get Unique study materials from

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Short Notes of CS201

What about Object-Oriented Languages?

Course Description. Learn To: : Intro to JAVA SE7 and Programming using JAVA SE7. Course Outline ::

25. Generic Programming

Transcription:

Lecture Notes on Programming Languages 85 Lecture 09: Support for Object-Oriented Programming This lecture discusses how programming languages support object-oriented programming. Topics to be covered include: Introduction Fundamental Principles Design Issues Cases: Support in Specific Languages Copyright 2000 2016 by All rights reserved. No part of this document may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, or otherwise, without prior written permission of the author.

86 9.1 Introduction You are likely aware that OOP has been dominating contemporary programming, and for good reasons. Among the fundamental tenets of OOP are the following: classes, subprogram overloading, operator overloading, inheritance, polymorphism, dynamic binding, and amalgamation. Class definition was discussed in the previous lecture. In most OOPLs, classes are specified by using the keyword class as part of the syntax. On exception to this is Ada, where classes are implemented as specification packages (via the keyword package) and body packages (via keywords body package). The body package defines implementation of the items in the specification package. As pointed out in lecture 8, each OOPL tends to have its own idiosyncrasies when it comes to class definition and implementation. Rather than focusing on that, the rest of this lecture will focus on how some of the other fundamental tenets are implemented in different OOPLs and/or hybrid languages. 9.2 Fundamental Principles In looking at the fundamental principles of OOP, it can be observed that there is widespread acceptance of these principles across various languages; however, implementation details vary from one language to another. Following is a brief survey. 9.2.1 Subprogram Overloading Subprogram overloading is the ability to define a subprogram multiple times, each with a slightly different header definition and corresponding code. The compiler invokes the appropriate subprogram based on the context of usage. This feature is possible only in languages that support dynamic binding. It is supported in languages such as C++, Java, and C#. 9.2.2 Operator Overloading Operator overloading is the ability to redefine the meaning and application of an operator. Most language support operator overloading at the basic level. For example, Java allows the + operator to be applicable to numeric data as well as strings. C++ takes operator overloading to another level by allowing the programmer to redefine the meaning and application of operators. Review your C++ notes. 9.2.3 Inheritance Inheritance is the capacity of an object to adopt the features of its parent class on which it has been defined. It also relates to a class adopting the features of another class. The principle can be traced back to SUMULA 67, but did not really gain momentum until the 1980s.

87 9.2.3 Inheritance (continued) All OOPLs support inheritance but not necessarily in identical ways. Below is a summary of three cases: C++: The language supports single inheritance as well as multiple inheritances. It uses a somewhat cryptic notation. Keywords supported are public, private, and protected. Private properties are not inheritable. Java: Java supports single inheritance, but does not support multiple inheritances. However, through interfaces and abstract classes, limited benefits of multiple inheritances can be achieved, while eliminating the drawbacks. Java can block a method from being inheritable via allowing use of the keyword final. Other keywords supported are public, private, and protected. Private properties are not inheritable. C#: This language supports inheritance in exactly the same way as C++, and with syntax identical to that of C++. 9.2.4 Polymorphism Polymorphism is the capacity of an object, operation, or operator to take on different forms, depending on the context of usage. This is achieved through subprogram overloading, operator overloading, subprogram with default arguments, and subprogram overriding, generic subprograms, object inheritance, and object casting. By way of review, subprogram overriding may proceed in any of the following ways: A subclass may inherit methods from its super-class, and adopt them without changes. A subclass may inherit a method and modify the original behavior to behave differently. A subclass may inherit a method and restrict its original behavior. A subclass may inherit a method and extend the original behavior to include other instructions. All OOPLs support polymorphism to different degrees. Below is a summary: Java supports method overriding, method overloading, and limited operator overloading (programmers are not allowed to overload operators). Method overriding also extends to abstract classes and interfaces. Generic subprograms are supported in a limited way. C++ supports function overloading, function overriding, operator overloading (including the ability to redefine the meaning and application of operators), and function with default arguments, generic subprograms, and object casting. C# supports function overloading in a manner that is similar (and in many respects identical) to how it is done in C++. 9.2.5 Dynamic Binding Dynamic (late) binding is a characteristic feature of all OOPLs. Without it, it would be difficult to support other principles of inheritance and polymorphism. 9.2.6 Amalgamation Amalgamation is also characteristic feature of all OOPLs. Through composition and/or aggregation, complex objects can be constructed with minimum effort.

88 9.3 Design Issues When an OOPL or hybrid language is being designed, the software engineers and language designers have to carefully consider what OOP tenets will be supported in the language, to what extent, and how. These decisions have to be made early in the design phase of the software development life cycle (SDLC), since they tend to affect other aspects of the language. Do note and understand that designing a programming language is a huge software engineering undertaking that requires various skills. Following are some of the main considerations. Object Exclusivity: Should everything be implemented as objects, or should there be exceptions? Subclasses vs Subtypes: Should subclasses always be subtypes? Is the is-a relationship always applicable for subclasses? Abstract Classes: As you are aware, an abstract class is a class for which instances are not created. Abstract classes are purely for the purpose of inheritance. Will they be supported, and if yes, how? Type Checking & Polymorphism: To what level should polymorphism be taken, and how does this affect type checking? In most cases, casting solves this problem. Single vs Multiple-Inheritance: Should multiple-inheritance be supported? If yes, how will the negative sideeffects be avoided? Object Allocation & De-allocation: Know how this is done in different languages. One approach is to treat objects as ADT instances. This means they can be allocated from the program s run-time stack, or explicitly put on the program s heap via an operation such as new. The latter strategy provides more flexibility: heap nodes are object references, while stack nodes tend to be value variables. Another issue is whether objects are de-allocated implicitly or explicitly. Implicit de-allocation requires some internal method of storage recapture; explicit de-allocation raises concerns about treatment of dangling pointers. Binding Strategy: Late binding versus early binding which strategy will be employed? Object Initialization: Will object instantiation be implicit or explicit? Or will the language support both strategies? CS professionals studying programming languages should know how this is done in different languages. Nested Classes: Some languages support nested classes (Java is one though it is not encouraged); others do not (for example C++). 9.4 Cases: Support in Specific Languages Let us look at how the OOP tenets of section 9.1 are met in specific programming languages. We will look at five languages (Smalltalk, C++, Java, C#, and Ruby), but you are encouraged to look at others not covered in this lecture.

89 9.4.1 Smalltalk Purely OOPL Inheritance: Superclass properties are all inherited in the sub-class by default; none can be hidden; single inheritance supported Dynamic binding supported Performance: Slow 9.4.2 C++ C++ is a hybrid language supporting both imperative (procedural) and OO programming C++ supports both classes and structures; the former has member functions while the latter typically does not Inheritance: Multiple-inheritance is supported; the virtual keyword avoids ambiguity. The parent constructor for a sub-class object is automatically called at instantiation Private properties cannot be inherited Scope resolution operator is used to access a class property beyond the boundaries of the class Polymorphism: Supports subprogram overloading and overriding Supports operator overloading more comprehensively than most other OOPLs Supports generic subprograms and classes through templates Late binding is supported; the language is very efficient and portable language 9.4.3 Java Java is accepted as an OOPL but is technically a hybrid language supporting both imperative (procedural) and OO programming Java supports classes only; structures are not supported. Inheritance: Single inheritance is supported; multiple inheritance is supported Instead of multiple-inheritance, Java supports interfaces. A Java class may implement multiple interfaces Java also supports abstract classes classes for which instances are not directly created, but which can be inherited in an inheritance hierarchy. The parent constructor for a sub-class object is not automatically called at instantiation Private properties cannot be inherited The super keyword is used to access a class property beyond the boundaries of the class to the parent class

90 9.4.3 Java (continued) Polymorphism: Java supports subprogram overloading and overriding Java provides operator overloading in a very limited way with respect to the + operator. However, programmers are not allowed to overload operators. Java supports generic subprograms and classes Late binding is supported Java is a very portable language; through the Java Virtual Machine (JVM), platform independence is achieved. 9.4.4 C# C# is accepted as an OOPL but is technically a hybrid language supporting both imperative (procedural) and OO programming C# supports both classes and structures; the former has member functions while the latter typically does not Inheritance: Single inheritance is supported; multiple inheritance is supported C# also supports abstract classes classes for which instances are not directly created, but which can be inherited in an inheritance hierarchy. The parent constructor for a sub-class object is not automatically called at instantiation Private properties cannot be inherited The super keyword is used to access a class property beyond the boundaries of the class to the parent class Polymorphism: C# supports subprogram overloading and overriding C# supports operator overloading in a manner that is similar to C++ C# supports generic subprograms and classes Late binding is supported C# enjoys wide support in the.net family of software development tools. 9.4.5 Ruby Ruby is a pure OOPL: virtually everything is an object and computations are done via message passing. Like Java, Ruby supports classes only; structures are not supported. Data items of a class are by definition private in nature, and are not allowed to be anything else. Constructors have a standard name initialize.

91 9.4.5 Ruby (continued) Inheritance: Single inheritance is supported; multiple inheritance is supported Ruby does not support interfaces. Private properties cannot be inherited The super keyword is used to access a class property beyond the boundaries of the class to the parent class Polymorphism: Ruby does not support subprogram overloading but supports subprogram overriding Like C++, Ruby allows the programmer to define how operators are used in a class. Since Ruby does not support the concept of predefined types, the concept of generic subprogram is irrelevant. Late binding is supported. The language is not as efficient compiled languages. 9.5 Summary and Concluding Remarks Let us summarize the salient points of this lecture: The fundamental tenets of OOP include the following: classes, subprogram overloading, operator overloading, inheritance, polymorphism, dynamic binding, and amalgamation. These principles are widely accepted in OOPLs and hybrid languages. However, they are implemented differently across different language barriers. When an OOPL or hybrid language is being designed, the following are some design issues to be considered early in the design stage: object exclusivity; subclasses versus subtypes; abstract classes; type checking and polymorphism; single versus multiple-inheritance; object allocation and de-allocation; binding strategy; object initialization; nested classes versus stand-alone classes. We looked at how the aforementioned OOP tenets are met in five specific programming languages (Smalltalk, C++, Java, C#, and Ruby), but you are encouraged to look at others not covered in this lecture. Like the previous lecture, this is a rather fascinating topic that we could easily continue exploring. You are encouraged to conduct that exploration with various OOPLs and hybrid languages. Our next lecture will change gear a bit and examine the matter of concurrent programming a rather relevant topic in contemporary programming.

92 9.6 Recommended Readings [Pratt & Zelkowitz 2001] Pratt, Terrence W. and Marvin V. Zelkowits. 2001. Programming Languages: Design and Implementation 4 th Edition. Upper Saddle River, NJ: Prentice Hall. See chapters 7 & 9. [Sebesta 2012] Sebesta, Robert W. 2012. Concepts of Programming Languages 10 th Edition. Colorado Springs, Colorado: Pearson. See chapter 12.