Chapter 12: Support for Object-Oriented Programming
Chapter 12: Support for Object-Oriented Programming
1. Introduction
Overview of Object-Oriented Programming (OOP) Languages:
Many languages support object-oriented programming.
Some support procedural and data-oriented programming:
Example: C++
Some support functional programming:
Example: CLOS (Common Lisp Object System)
Newer languages may not support other paradigms but use imperative structures:
Examples: Java, C#
Some languages are pure OOP languages:
Examples: Smalltalk, Ruby
Some functional languages also incorporate OOP features.
2. Object-Oriented Programming
Major Features of OOP Languages:
Abstract Data Types (ADTs): Refer to data types whose implementation details are hidden.
Inheritance:
Central theme in OOP, allowing classes to inherit characteristics from other classes.
Polymorphism:
Ability for different classes to be treated as instances of the same class through a common interface.
3. Inheritance
Benefits of Inheritance:
Enables productivity increases through code reuse.
ADTs can be challenging to reuse often due to the need for alterations.
Inheritance allows for defining new classes based on existing ones, facilitating reuse by inheriting common parts and creating class hierarchies.
4. Object-Oriented Concepts
Definitions:
ADTs are commonly referred to as classes.
Instances of classes are termed objects.
A class that inherits from another is called a derived class or subclass.
The class being inherited from is known as the parent class or superclass.
Procedures that operate on objects are called methods.
Messaging and Protocols:
Method invocation is termed messages.
The complete set of methods of an object constitutes its message protocol or message interface.
Each message has two components: a method name and a destination object.
Typically, a subclass inherits all entities from its parent class.
5. Inheritance Complicating Factors
Access Control:
Classes can restrict visibility to their members.
Access controls help manage data encapsulation; they can prevent subclasses from accessing certain fields.
Inheritance allows overriding of inherited methods (new methods replace their predecessors).
6. Variations in Subclassing
Variability in Subclassing:
A subclass may add new variables and/or methods to those inherited from the parent class.
A subclass may modify the behavior of inherited methods.
Parent class methods or variables can be declared as private, meaning they are not accessible in the subclass.
7. Variables and Methods in a Class
Types of Class Variables:
Class Variables: Shared across all instances of a class (one per class).
Instance Variables: Unique to each instance of a class (one per object).
Types of Class Methods:
Class Methods: Accept messages directed toward the class and not to objects.
Instance Methods: Accept messages directed at objects only.
Inheritance Complexity:
Single vs. Multiple Inheritance:
Inheritance can create interdependencies between class hierarchies, complicating maintenance.
8. Dynamic Binding
Definition:
A polymorphic variable can reference objects from a class and its subclasses.
Dynamic binding allows for method resolution at runtime, making systems more extensible during development.
9. Dynamic Binding Concepts
Definitions:
Abstract Method: A method that does not include an implementation; it only defines a protocol.
Abstract Class: A class that contains at least one abstract method; it cannot be instantiated directly.
10. Design Issues for OOP Languages
Key Design Considerations:
The Exclusivity of Objects: Everything being treated as an object can enhance elegance but slow performance.
Subclass and Superclass Relationships: Is a subclass a subtype?
Single vs. Multiple Inheritance: Complexity versus convenience.
Object allocation and deallocation methods (stack vs heap).
Binding strategies: dynamic vs static.
Handling nested classes appropriately and initializing objects correctly.
11. Support for OOP in Smalltalk
Characteristics of Smalltalk:
Smalltalk is a pure OOP language where everything is an object.
Objects possess local memory, and all computations occur via message passing.
Unique in that all memory allocation is done on the heap and memory deallocation is implicit.
Inheritance in Smalltalk:
Subclasses inherit all attributes from their superclasses; all subclasses are subtypes.
No multiple inheritance is supported, leading to implementation inheritance exclusively.
Dynamic Binding in Smalltalk:
All message bindings are dynamic; method searches follow the inheritance chain upwards.
Type errors manifest only when a message is sent to an object without the corresponding method.
Evaluation:
Syntax is simple and regular, but execution speed is slower than conventional compiled imperative languages.
Pioneered graphical user interfaces and significantly advanced OOP concepts.
12. Support for OOP in C++
General Characteristics:
Evolved from C and SIMULA 67; one of the most widely used OOP languages.
Mixed typing system supporting both procedural and object-oriented paradigms.
Features constructors, destructors, and intricate access control for class entities.
Inheritance Features in C++:
Classes do not need a superclass; provide varying access controls:
Private: Inaccessible outside the class.
Public: Visible in subclasses and client programs.
Protected: Accessible to subclass and parent class only.
Subclassing and Access Controls:
Defined with access control to manage inheritance visibility effectively (private/public).
Example C++ Inheritance:
Prototypes are discussed regarding base and subclasses, showcasing inherited member visibility.
Multiple Inheritance:
Enabled in C++; name collisions can occur when members share names.
Dynamic Binding:
Methods can be declared virtual for dynamic sending. Pure virtual functions create abstract classes.
Evaluation:
C++ provides strong access controls, unlike Smalltalk.
Allows for both static and dynamic binding, with the programmer responsible for method binding choices.
13. Support for OOP in Java
General Characteristics:
All non-primitive types are objects; primitive types are encapsulated in wrapper classes.
Objects are heap-dynamic; all allocations are typically via the
newkeyword.
Inheritance in Java:
Supports single inheritance with interfaces offering some benefits of multiple inheritance.
Dynamic Binding in Java:
All messages dynamically bind unless specified as final, static, or private, which prevents overriding.
Nested Classes in Java:
Nested classes are hidden to other classes in their package (access level determined by nesting behavior).
Evaluation:
Design principles mirror those in C++; supports a clean OOP structure, no procedural programming.
14. Support for OOP in C
General Characteristics:
Has OOP principles akin to Java, with an additional emphasis on structs (limited functionalities).
Inheritance:
Syntax borrowed from C++ for class definitions, with methods marked using
newto replace inherited behavior.
Dynamic Binding:
For dynamic binding, base class methods must be marked as virtual; overrides must be marked as such.
Nested Classes:
Behave as static nested classes in Java, implementing tighter encapsulation and functionality.
Evaluation:
Comparatively newer, shows minor differences regarding OOP support compared to Java.
15. Support for OOP in Ruby
General Characteristics:
Everything is an object and calculations are performed through message passing.
Class definitions and method definitions are executable, enabling runtime type flexibility.
Access control differs between methods (public, protected, private) and instance data (always private).
Inheritance in Ruby:
Inheritance exhibits looser constraints with access controls compared to other OOP languages.
Dynamic Binding:
All references are typeless, fostering polymorphism.
Evaluation:
Does not support abstract classes or multiple inheritance robustly; access controls are comparatively weaker.
16. Implementing OOP Constructs
Critical Aspects:
Management of instance variable storage and mechanisms for dynamic binding of method calls.
17. Instance Data Storage
Configuration for Class Instance Records (CIRs):
Static CIRs enable storage of an object's state; inheritance structure allows subclass variables to extend parent CIRs.
18. Dynamic Binding of Method Calls
Statically bound methods do not require CIR participation;
Dynamically bound methods must include pointers in the CIR for binding connections to their respective methods, typically organized in a virtual method table (vtable).
19. Reflection
Definition:
Programming languages supporting reflection provide runtime access to types and structures, enabling dynamic behavior modification.
Metadata: Represents the type and structure information.
Introspection: Refers to the program's ability to examine its metadata.
Intercession: Allows intervention during program execution.
Uses of Reflection:
Tools like class browsers and IDEs utilize reflection for accurate type handling.
20. Reflection in Java and C
Java Reflection:
Limited through
java.lang.Class; utilizes instances of Class for reflection and provides methods for method retrieval.
C# Reflection:
Achieved through the
System.Typenamespace; utilizes metadata within assemblies for powerful runtime capabilities.
21. Downsides of Reflection
Performance penalties when using reflection.
Compromises encapsulation by exposing private variables and methods.
Early type checking advantages are diminished.
Security restrictions may limit the functionality of reflection, affecting portability.
22. Summary
Key OOP Concepts:
Abstract Data Types (ADTs), Inheritance, Dynamic Binding.
Significant design issues: subclassing implications, object exclusivity, and allocation strategies.
Smalltalk exemplifies pure OOP principles; C++, Java, C#, and Ruby illustrate various OOP support methodologies and impacts on performance and design.