Design issues for Object-Oriented Languages

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

A subclass can add new private instance variables A subclass can add new public, private or static methods A subclass can override inherited methods A.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
Inheritance Inheritance Reserved word protected Reserved word super
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Run-Time Storage Organization
Chapter 9: Subprogram Control
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++
Chapter 12: Adding Functionality to Your Classes.
“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.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Compiler Construction
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Introduction to Object Oriented Programming CMSC 331.
Basic Semantics Associating meaning with language entities.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Object-Oriented Programming Chapter Chapter
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
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
Programming Languages and Paradigms Activation Records in Java.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
A First Book of C++ Chapter 12 Extending Your Classes.
Run-Time Environments Presented By: Seema Gupta 09MCA102.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
The Object-Oriented Thought Process Chapter 03
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Advanced Programming in Java
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Inheritance and Polymorphism
Java Primer 1: Types, Classes and Operators
Inheritance and Run time Polymorphism
Programming Language Concepts (CIS 635)
Inheritance, Polymorphism and the Object Memory Model
Continuing Chapter 11 Inheritance and Polymorphism
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Java Programming Language
Parameter Passing Actual vs formal parameters
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
UNIT V Run Time Environments.
Lecture 10 Concepts of Programming Languages
COP 3330 Object-oriented Programming in C++
Corresponds with Chapter 5
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Advanced Programming in Java
SPL – PS3 C++ Classes.
Presentation transcript:

Design issues for Object-Oriented Languages Exclusivity of objects Type checking of objects used to invoke dynamically bound methods Single versus multiple inheritance Allocation of objects Deallocation of objects Dynamic binding of messages to methods This set of slides discusses these issues, and more, comparing C++ and Java Sections 9.5, 12.3, 12.5, 12.6

Exclusivity of Objects Primitive type – data type that is built into the language; often has a corresponding type on the machine (example: int in C) Exclusivity of objects answers the question: is everything (including primitive types) an object? C++? Java? Sections 9.5, 12.3, 12.5, 12.6

Object syntax Java Car c1; // c1 isn't an object – it's a handle, reference, pointer to an //object c1 = new Car(); //the new car is an object C++ Car c1; //c1 is an object Car * c2; //c2 is a pointer to a Car object c2 = new Car(); //the new car is an object c1.paint(“red”); //how to call paint if c1 is an object c2->paint(“red”); //how to call paint if c2 is a pointer //-> is used for “dereferencing” -- get the object //c2 points to Sections 9.5, 12.3, 12.5, 12.6

Object Allocation Stack dynamic – object allocated space on stack at runtime Static – object allocated space by compiler in the data segment Heap dynamic – object allocated space on heap at run time C++? Java? Sections 9.5, 12.3, 12.5, 12.6

Memory Allocation languages that use a stack for execution (which is nearly all languages) allocate basically three sections of memory stack heap data/text segment Sections 9.5, 12.3, 12.5, 12.6

stack frame (activation record) for a function created during execution time when function is called and pushed onto stack frame is popped from stack when function is exited frame contains: local variables and parameters return address saved registers, old frame pointer Sections 9.5, 12.3, 12.5, 12.6

heap heap contains data dynamically allocated via a new, malloc, alloc, etc. dynamic means “runtime” note that space on the stack is also dynamically allocated (we call this stack dynamic) but is not allocated explicitly via a new Sections 9.5, 12.3, 12.5, 12.6

data/text segment data/text segment contains items that are allocated by the compiler This includes: program text (instructions) string literals (also called c-strings) - “hello” large constants – those that are too large to be encoded in an instruction Sections 9.5, 12.3, 12.5, 12.6

Object deallocation When and how are heap allocated objects deallocated? C++ Java Sections 9.5, 12.3, 12.5, 12.6

Polymorphism/Dynamic Binding Are all messages to methods dynamically bound? Dynamically bound messages are less efficient than statically bound messages C++? Java? Sections 9.5, 12.3, 12.5, 12.6

Assignments between object references Consider classes called Parent, Child where Child inherits from (extends) Parent There exists an IS-A relationship between Child objects and Parent objects (a Child object is a Parent object) The IS-A relationship indicates what assignments between references are valid parentRef = childRef #allowed childRef = parentRef #compiler error Sections 9.5, 12.3, 12.5, 12.6

Java casts of object references a cast of an object reference doesn't change the object; it is only an indication to the compiler what the object will be at run time downcast - cast a reference along the class hierarchy in a direction from the root class towards the children or subclasses; (Child) parentRef upcast - cast a reference along the class hierarchy in a direction from the sub classes towards the root; (Parent) childRef

Java casts of object references compile-time casting rule - catch attempted casts in cases that are simply not possible; happens when we try to attempt casts on objects that are totally unrelated Assume Male, Female both inherit from Gender: (Male) femaleRef #error because femaleRef can #never reference a Male object run-time casting rules – the cast must actually be correct (according to IS-A) relationship or runtime error occurs Parent parentRef = new Parent(); ((Child)parentRef).method();

C++ casts of objects or object references casts of pointers don't change object or create new object casts of objects will cause new object to be created; needed constructor automatically called compile-time casting rule – when casting pointers, same rules as Java apply run-time casting rule – no rule, because C++ doesn't do dynamic type checking

Dynamic binding of method calls to method bodies in Java by default, method calls to method bodies are dynamically bound (meaning JVM determines which method to call, not compiler) exception to this is methods that are declared to be static; these aren't called with an object thus compiler can determine which method is called

Dynamic binding of method calls to method bodies in C++ by default, method calls to method bodies are statically bound (determined by the compiler) A dynamic binding requires polymorphic variable virtual method

Polymorphism assignment of a different meaning to a method call in different context Requires method overriding – method in parent class has same signature as a method in a child class polymorphic variable – can reference a parent class object and a child class object dynamic binding – which method is called is determined at runtime

Polymorphism in Java class Parent { void foo() { ... } } class Child extends Parent .. Parent obj; obj.foo(); //JVM determines which foo is called based upon the type //of object referenced by obj

Polymorphism in C++ class Parent { virtual void foo() { ... } void goo() { ... } } class Child: public Parent virtual void foo() { ... } .. Parent * obj; obj->foo(); //which foo is called depends upon what obj points to obj->goo(); //call to goo is statically bound (compiler determines it) //based on type of obj (Parent *)

Single or multiple inheritance Supporting multiple inheritance adds complexity Name collision Diamond inheritance C++? Java? Sections 9.5, 12.3, 12.5, 12.6

Name Collision Child class inherits from two Parent classes where each define the same name example: Parent1 and Parent2 both have a method called display example: Parent1 and Parent2 both have a data member named number Language designer needs to come up with a technique (scope resolution) to resolve the ambiguity if method is overrided, which is overridden? Sections 9.5, 12.3, 12.5, 12.6

Diamond Inheritance Both Parent classes are derived from a common Grandparent class Should the child class inherit two copies of the Grandparent's data members? Sections 9.5, 12.3, 12.5, 12.6

Type checking of objects used to invoke dynamically bound methods Type checking – ensuring that operands of an operator are of compatible type (includes ensuring whether the appropriate object is used to invoke a specific method) Strongly typed language – a language in which all type errors can be detected either at compile time or at run time Static type checking (static typing) – type checking performed at compile type Dynamic type checking (dynamic typing) type checking performed at runtime C++? Java? Sections 9.5, 12.3, 12.5, 12.6

More C++/Java differences C++ has header files These allow classes to be compiled in isolation even if one class references another class C++ has a reference type and a pointer type Java object handles are always references thus an explicit reference type isn’t needed Sections 9.5, 12.3, 12.5, 12.6

More C++/Java Differences Parameter Passing In mode semantics – formal parameter receives data from actual parameter Out mode semantics – formal parameter transmits data to the actual parameter Inout mode semantics – supports both Pass-by-value – value of actual parameter is used to initialize formal parameter; supports in mode semantics Pass-by-reference – access path is passed to formal method; supports inout semantics C++? Java? Sections 9.5, 12.3, 12.5, 12.6