Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.

Slides:



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

ISBN Chapter 12 Support for Object-Oriented Programming.
Software Productivity
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 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
Abstract Data Types and Encapsulation Concepts
1 Chapter 12 © 2002 by Addison Wesley Longman, Inc Introduction - Categories of languages that support OOP: 1. OOP support is added to an existing.
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.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 12 Topics Introduction Object-Oriented Programming.
Support for Object-Oriented Programming
Chapter 12 Support for Object-Oriented Programming.
CPS 506 Comparative Programming Languages
(11.1) COEN Data Abstraction and OOP  Data Abstraction – Problems with subprogram abstraction – Encapsulation – Data abstraction – Language issues.
1 Chapter 11 Abstract Data Types and Encapsulation Constructs.
ISBN Chapter 12 Support for Object-Oriented Programming.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
CS 403 – Programming Languages Class 25 November 28, 2000.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Object-Oriented Programming Session 9 Course : T Programming Language Concept Year : February 2011.
The Procedure Abstraction, Part V: Support for OOLs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Introduction to Object Oriented Programming CMSC 331.
Chapter 12 Support for Object oriented Programming.
Chapter 12 Support for Object-Oriented Programming.
ISBN Chapter 12 Support for Object-Oriented Programming.
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 12 Support for Object-Oriented Programming.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 11 Categories of languages that support OOP: 1. OOP support is added to an existing language.
ISBN Object-Oriented Programming Chapter Chapter
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
ISBN Chapter 12 Support for Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
Support for Object-Oriented Programming
12.1 Introduction - Categories of languages that support OOP:
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Inheritance and Polymorphism
Support for Object-Oriented Programming
Lecture 9 Concepts of Programming Languages
Support for Object-Oriented Programming
Object-Oriented Programming
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Object-Oriented Programming
Support for Object-Oriented Programming
Support for Object-Oriented Programming
Lecture 10 Concepts of Programming Languages
Chapter 12 Categories of languages that support OOP:
11.1 The Concept of Abstraction
Lecture 9 Concepts of Programming Languages
Presentation transcript:

Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea

Concepts of Programming LanguagesL4.2 Topics Object-Oriented Programming Design Issues for Object-Oriented Languages Support for Object-Oriented Programming in C++ Support for Object-Oriented Programming in Java

Concepts of Programming LanguagesL4.3 Object-Oriented Programming / Basic Concepts Abstract data types Inheritance –Inheritance is the central theme in OOP and languages that support it Polymorphism –Concept on the foundation of references/pointers and type hierarchies Dynamic Binding –In relationship with polymorphism

Concepts of Programming LanguagesL4.4 Object-Orientation Concepts and Notions ADTs are usually called classes Class instances are called objects Functions that define operations on objects are called methods Calls to methods are called messages –Messages have two parts: method name and the destination object

Concepts of Programming LanguagesL4.5 Inheritance Inheritance extends the concept of ADTs –Allows new ADTs (classes) defined in terms of existing ones (By allowing them to inherit common parts) –Reasons for inheritance: Increasing productivity by reusing code Type hierarchies on the foundations of ADTs A class that inherits is a derived class or a subclass The class from which another class inherits is a parent class or superclass

Concepts of Programming LanguagesL4.6 Inheritance (continued) Access control and encapsulation in the context of inheritance: –A class can also hide entities for its clients while allowing its subclasses to see them. (Called a protected entity) Redefinition of methods in derived classes –The new one overrides the parent class’s one –The method in the parent is overridden –Important: Overloading and overriding are two different concepts!

Concepts of Programming LanguagesL4.7 Object-Oriented Concepts There are two kinds of variables in a class: –Class variables - one/class –Instance variables - one/object There are two kinds of methods in a class: –Class methods – accept messages to the class –Instance methods – accept messages to objects

Concepts of Programming LanguagesL4.8 Abstract Classes An abstract method is one that does not include a definition (it only defines a header) An abstract class is one that includes at least one abstract method An abstract class cannot be instantiated, i.e. for abstract classes we cannot create objects

Concepts of Programming LanguagesL4.9 Polymorphism and Dynamic Binding A polymorphic variable references an object of the variable’s type or some of its derived types. Polymorphism and overriding of methods results in dynamic binding –An object’s type decides the finally called implementation during runtime

Concepts of Programming LanguagesL4.10 Design Issues for OOP Languages Exclusivity of Objects Single and Multiple Inheritance Object Allocation and DeAllocation Dynamic and Static Binding Nested Classes

Concepts of Programming LanguagesL4.11 The Exclusivity of Objects Everything is an object –Advantage - elegance and purity –Disadvantage - slow operations on simple objects Include an imperative-style typing system for primitives but make everything else objects –Advantage - fast operations on simple objects and a relatively small typing system –Disadvantage - still some confusion because of the two type systems

Concepts of Programming LanguagesL4.12 Single and Multiple Inheritance Multiple inheritance allows a new class to inherit from two or more classes Disadvantages of multiple inheritance: –Language and implementation complexity (in part due to name collisions, see C++ example) –Potential inefficiency - dynamic binding costs more with multiple inheritance Advantage: –Sometimes it is quite convenient and valuable

Concepts of Programming LanguagesL4.13 Allocation and DeAllocation of Objects From where and how are objects allocated? –Most flexible approach. Several allocation forms: Static allocation (e.g. global variables) Stack dynamic (e.g. local variables) Heap dynamic (via new) –Simplified scheme: All objects are heap-dynamic. Advantages: References can be uniform through reference variables Dereferencing can be implicit Is deallocation explicit or implicit?

Concepts of Programming LanguagesL4.14 Dynamic and Static Binding Should all binding of messages to methods be dynamic? –If none are, you lose the advantages of dynamic binding… –If all are, it is inefficient… Allow the user to specify (e.g. C++)

Concepts of Programming LanguagesL4.15 Nested Classes If a new class is needed only in the context of one single host class, there is no reason to define it so that it can be seen by other classes… –Bunch of opportunities here, see e.g. Java

Concepts of Programming LanguagesL4.16 Support for OOP in C++ General Characteristics: –Evolved from C and SIMULA 67 –Mixed typing system –Constructors and destructors –Elaborate access controls to class entities

Concepts of Programming LanguagesL4.17 Support for OOP in C++ (continued) Inheritance –A class need not be the subclass of any class –Access controls for members are –Private (visible only in the class and friends) (disallows subclasses from being subtypes) –Public (visible in subclasses and clients) –Protected (visible in the class and in subclasses, but not clients)

Concepts of Programming LanguagesL4.18 Support for OOP in C++ (continued) In addition, the subclassing process can be declared with access controls (private or public), which define potential changes in access by subclasses –Private derivation - inherited public and protected members are private in the subclasses –Public derivation - public and protected members are also public and protected in subclasses

Concepts of Programming LanguagesL4.19 Inheritance Example in C++ class base_class { private: int a; float x; protected: int b; float y; public: int c; float z; }; class subclass_1 : public base_class { … }; // In this one, b and y are protected and // c and z are public class subclass_2 : private base_class { … }; // In this one, b, y, c, and z are private, // and no derived class has access to any // member of base_class

Concepts of Programming LanguagesL4.20 Reexportation in C++ A member that is not accessible in a subclass (because of private derivation) can be declared to be visible there using the scope resolution operator ( :: ), e.g., class subclass_3 : private base_class { base_class :: c; … }

Concepts of Programming LanguagesL4.21 Support for OOP in C++ (continued) Multiple inheritance supported –Possible problem: Nasty ambiguities A: void m() C B: void m() ?

Concepts of Programming LanguagesL4.22 Inheritance and Private Derivation Motivation for using private derivation –Resolution of naming conflicts in the context of multiple inheritance class C : public A, private B { … }

Concepts of Programming LanguagesL4.23 Support for OOP in C++ (continued) Dynamic Binding –A method can be defined to be virtual, which means that they can be called through polymorphic variables and dynamically bound to messages –A pure virtual function has no definition at all –A class that has at least one pure virtual function is an abstract class

Concepts of Programming LanguagesL4.24 Support for OOP in Java Because of its close relationship to C++, focus is on the differences from that language General Characteristics –All data are objects except the primitive types –All primitive types have wrapper classes that store one data value –All objects are heap-dynamic, are referenced through reference variables, and most are allocated with new –A finalize method is implicitly called when the garbage collector is about to reclaim the storage occupied by the object

Concepts of Programming LanguagesL4.25 Support for OOP in Java (continued) Inheritance –Single inheritance supported only, but there is an abstract class category that provides some of the benefits of multiple inheritance ( interface ) –An interface can include only method declarations and named constants, e.g., public interface Comparable { public int comparedTo (Object b); } –Methods can be final (cannot be overriden)

Concepts of Programming LanguagesL4.26 Support for OOP in Java (continued) Dynamic Binding In Java, all messages are dynamically bound, except in the following situations, where we may have static binding: –method is final (i.e., it cannot be overridden in derived classes) –method is static or private (both of which disallow overriding as well)

Concepts of Programming LanguagesL4.27 Support for OOP in Java (continued) There are four kinds of nested classes in Java: –static class: declared as a static member of another class –inner class: declared as an instance member of another class –local inner class: declared inside an instance method of another class –anonymous inner class: like a local inner class, but written as an expression which returns a one- off object

Concepts of Programming LanguagesL4.28 Examples for Nested Classes Static class: class Host { static class Nested { }; }; Host.Nested obj = new Host.Nested(); Inner class: class Host { class Nested { }; }; Host hostObj = new Host(); Host.Nested obj = hostObj.new Host();

Concepts of Programming LanguagesL4.29 Examples for Nested Classes Local inner class: class Host { void method(){ int i; class Nested { }; } } Anonymous inner class: abstract class Host { abstract void m() ; }; Host obj = new Host() {void m() {...}};