Object-Oriented Programming Concepts Quiz

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

What term describes calls to methods in object-oriented programming?

  • Instructions
  • Signals
  • Commands
  • Messages (correct)

How can a subclass differ from its parent class?

  • By limiting the access of inherited methods
  • By modifying private variables
  • By redefining class variables
  • By adding variables or methods (correct)

What is indicated by the term 'dynamic binding' in object-oriented programming?

  • Binding that occurs when a polymorphic variable references its methods (correct)
  • Binding that requires explicit type declaration
  • Binding to a specific object instance at compile time
  • Binding that prevents method overriding

Which of the following best describes encapsulation in the context of inheritance?

<p>Hiding entities from subclasses and clients (B)</p> Signup and view all the answers

Which variables are unique to each instance of a class?

<p>Instance variables (D)</p> Signup and view all the answers

What is a disadvantage of using inheritance for code reuse?

<p>Creation of interdependencies among classes (B)</p> Signup and view all the answers

What component of a message allows it to define the action to be executed?

<p>Method name (D)</p> Signup and view all the answers

What happens when a class overrides a method from its parent class?

<p>The new method completely replaces the parent method (D)</p> Signup and view all the answers

Which feature of object-oriented programming allows a class to inherit properties and behavior from another class?

<p>Inheritance (C)</p> Signup and view all the answers

What is the relationship between a subclass and a superclass in object-oriented programming?

<p>A subclass inherits from its superclass. (A)</p> Signup and view all the answers

In which programming language mentioned is object-oriented programming fully supported?

<p>Smalltalk (D)</p> Signup and view all the answers

Which of the following concepts relates closely to the ability of different classes to be treated as instances of the same class through a common interface?

<p>Polymorphism (C)</p> Signup and view all the answers

What are the instances created from classes called in object-oriented programming?

<p>Objects (C)</p> Signup and view all the answers

Which of the following languages does not primarily support multiple programming paradigms including object-oriented programming?

<p>Smalltalk (C)</p> Signup and view all the answers

What is a significant advantage of using inheritance in object-oriented programming?

<p>It enhances productivity through code reuse. (B)</p> Signup and view all the answers

What term is used to describe classes that are reused with minor modifications to existing abstract data types (ADTs)?

<p>Derived classes (C)</p> Signup and view all the answers

What is a key advantage of having all objects allocated from the heap in dynamic object management?

<p>It provides uniform references through pointers or reference variables. (A)</p> Signup and view all the answers

What is a consequence of having objects that are stack dynamic in terms of subtype behavior?

<p>It can lead to object slicing. (C)</p> Signup and view all the answers

What is one of the limitations related to nested classes in Smalltalk?

<p>Smalltalk classes cannot be nested in other classes. (D)</p> Signup and view all the answers

Which statement regarding dynamic and static binding is true?

<p>A mix of dynamic and static binding can leverage the advantages of both. (B)</p> Signup and view all the answers

When a subclass object is created, how are parent class members treated in Smalltalk?

<p>They inherit by default without explicit management. (D)</p> Signup and view all the answers

What is one defining feature of Smalltalk as an object-oriented programming language?

<p>All operations are performed through messages sent between objects. (D)</p> Signup and view all the answers

What is the implication of implementation inheritance in Smalltalk?

<p>All attributes and methods of parent classes are automatically visible to subclasses. (A)</p> Signup and view all the answers

How does Smalltalk handle deallocation of objects?

<p>Deallocation is implicit for all objects. (D)</p> Signup and view all the answers

An abstract class can be instantiated.

<p>False (B)</p> Signup and view all the answers

Dynamic binding is less efficient with single inheritance compared to multiple inheritance.

<p>False (B)</p> Signup and view all the answers

A derived class must behave the same as its parent class object to be considered a subtype.

<p>True (A)</p> Signup and view all the answers

Multiple inheritance allows a new class to inherit from a single class only.

<p>False (B)</p> Signup and view all the answers

Everything is considered an object in a complete typing system.

<p>True (A)</p> Signup and view all the answers

Adding objects to a typing system results in faster operations on simple objects.

<p>False (B)</p> Signup and view all the answers

The primary disadvantage of multiple inheritance is its convenience.

<p>False (B)</p> Signup and view all the answers

Subclass can add new variables and methods in a compatible way.

<p>True (A)</p> Signup and view all the answers

All binding of messages to methods in Smalltalk is static.

<p>False (B)</p> Signup and view all the answers

In Smalltalk, type checking is performed at compile time.

<p>False (B)</p> Signup and view all the answers

C++ evolved from C and SIMULA 67.

<p>True (A)</p> Signup and view all the answers

Constructors and destructors are features of Smalltalk but not C++.

<p>False (B)</p> Signup and view all the answers

Private access controls allow visibility only within the class and its friends in C++.

<p>True (A)</p> Signup and view all the answers

All members inherited through private derivation in C++ become public in the subclasses.

<p>False (B)</p> Signup and view all the answers

A class in C++ must be a subclass of another class to be valid.

<p>False (B)</p> Signup and view all the answers

Smalltalk introduced the concept of graphical user interfaces.

<p>True (A)</p> Signup and view all the answers

In C++, objects allocated from the stack are automatically deleted when they go out of scope.

<p>True (A)</p> Signup and view all the answers

C++ allows for multiple inheritance, while Smalltalk does not support this feature.

<p>True (A)</p> Signup and view all the answers

Dynamic binding in C++ always results in faster method calls compared to static binding.

<p>False (B)</p> Signup and view all the answers

In Java, all objects are created on the heap and managed by reference variables.

<p>True (A)</p> Signup and view all the answers

The finalize method in Java is called after an object is garbage collected.

<p>False (B)</p> Signup and view all the answers

C++ statically binds methods at runtime for improved performance.

<p>False (B)</p> Signup and view all the answers

In C++, a Circle object allocated from the heap is treated the same as a Circle object allocated from the stack.

<p>False (B)</p> Signup and view all the answers

Smalltalk is significantly faster than C++ due to its dynamic type checking capabilities.

<p>False (B)</p> Signup and view all the answers

C# supports both single and multiple inheritance.

<p>False (B)</p> Signup and view all the answers

In C#, a method that is inherited can be overridden in the derived class by marking it with the keyword override.

<p>True (A)</p> Signup and view all the answers

All variables in Ruby are strictly typed references to their respective classes.

<p>False (B)</p> Signup and view all the answers

Dynamic binding in C# requires that the base class method is marked abstract.

<p>False (B)</p> Signup and view all the answers

In Ruby, access to data members is private by default and cannot be changed.

<p>True (A)</p> Signup and view all the answers

C# allows nested classes that function like Java's non-static classes.

<p>False (B)</p> Signup and view all the answers

In C#, a subclass is considered a subtype only if all members of the parent class are public.

<p>True (A)</p> Signup and view all the answers

Method definitions in Ruby are non-executable and must be declared before use.

<p>False (B)</p> Signup and view all the answers

Access controls to inherited methods can differ from those in the parent class.

<p>True (A)</p> Signup and view all the answers

All variables in Ruby are statically typed and non-polymorphic.

<p>False (B)</p> Signup and view all the answers

A programming language that supports reflection allows programs to modify their types at compile time.

<p>False (B)</p> Signup and view all the answers

Static data structures in Ruby are built at runtime to manage instance variables.

<p>False (B)</p> Signup and view all the answers

Dynamically bound methods in Ruby require entries in the Class Instance Record (CIR).

<p>True (A)</p> Signup and view all the answers

Reflection involves the process of a program observing its own metadata.

<p>True (A)</p> Signup and view all the answers

Ruby fully supports multiple inheritance.

<p>False (B)</p> Signup and view all the answers

The process of a program examining its behavior is known as introspection.

<p>True (A)</p> Signup and view all the answers

Reflection in Java allows access to private fields and methods of a class.

<p>True (A)</p> Signup and view all the answers

The getClass method is used in Java to return the class object of an object.

<p>True (A)</p> Signup and view all the answers

In C#, the getType function serves the same purpose as Java's getClass method.

<p>True (A)</p> Signup and view all the answers

The System.Reflection.Emit namespace in C# allows for the creation of intermediate code and putting it into an assembly.

<p>True (A)</p> Signup and view all the answers

Java provides functionality to create intermediate code and add it to an assembly.

<p>False (B)</p> Signup and view all the answers

Using reflection can improve the performance of a software application.

<p>False (B)</p> Signup and view all the answers

Reflection can lead to code that is nonportable when run under a security manager.

<p>True (A)</p> Signup and view all the answers

The getDeclaredMethod retrieves all methods of a class, including private ones, in Java.

<p>False (B)</p> Signup and view all the answers

The Electronics class directly inherits from the Product class.

<p>True (A)</p> Signup and view all the answers

A class in Java cannot extend more than one class at the same time.

<p>True (A)</p> Signup and view all the answers

The displayInfo method in the Product class is inherited but not overridden in the Electronics class.

<p>False (B)</p> Signup and view all the answers

The ArrayList can be used to store products in the shopping cart implementation.

<p>True (A)</p> Signup and view all the answers

When a method is overridden in a subclass, the original method from the parent class cannot be called.

<p>False (B)</p> Signup and view all the answers

The ShoppingCart class utilizes polymorphism for handling different payment methods.

<p>False (B)</p> Signup and view all the answers

The method makePayment in the CreditCardPayment class requires a double parameter for the payment amount.

<p>True (A)</p> Signup and view all the answers

The ShoppingCart class is designed to manage a list of products using an array.

<p>False (B)</p> Signup and view all the answers

In Java, an interface allows a class to implement multiple behaviors.

<p>True (A)</p> Signup and view all the answers

The Product class has public fields that allow unrestricted access from other classes.

<p>False (B)</p> Signup and view all the answers

The GameObject class defines both move and render methods as pure virtual methods.

<p>True (A)</p> Signup and view all the answers

In the PaymentDemo class, the makePayment method can only be called on CreditCardPayment instances.

<p>False (B)</p> Signup and view all the answers

A derived class in C++ must implement all virtual methods defined in its base class.

<p>True (A)</p> Signup and view all the answers

The displayCart method in the ShoppingCart class prints the contents of the cart to the console.

<p>True (A)</p> Signup and view all the answers

The removal of a product from the ShoppingCart returns a boolean indicating success.

<p>True (A)</p> Signup and view all the answers

What is the primary purpose of the Electronics class in the given Java e-commerce system?

<p>To create a specialized subclass for electronic products (C)</p> Signup and view all the answers

Which of the following best describes the inheritance demonstrated in the Product class and its subclass?

<p>It enables the Electronics class to inherit properties from the Product class (B)</p> Signup and view all the answers

In the provided code, what would be the output of the displayInfo method for the genericProduct instance?

<p>Product Name: Generic Product, Price: $20.00 (C)</p> Signup and view all the answers

Which feature of the Java e-commerce system allows the shopping cart to manage multiple products?

<p>Implementing an ArrayList to store products (D)</p> Signup and view all the answers

Which of the following actions can be performed on the shopping cart as described in the e-commerce system?

<p>Adding and removing products dynamically (A)</p> Signup and view all the answers

Which of the following methods would allow a user to remove a product from the ShoppingCart?

<p>removeProduct(Product product) (A)</p> Signup and view all the answers

What is the advantage of using polymorphism in payment processing?

<p>Code can be modified without changing existing code. (A)</p> Signup and view all the answers

In the context of the ShoppingCart class, what does encapsulation primarily protect?

<p>Direct access to the products list. (D)</p> Signup and view all the answers

What will be printed when a product that is not in the cart is attempted to be removed?

<p>Product not found in the cart. (A)</p> Signup and view all the answers

Which of the following methods in the GameObject class is a pure virtual method?

<p>move() (A)</p> Signup and view all the answers

What is required for a class in Java to implement an interface like Payment?

<p>It must define all methods declared in the interface. (A)</p> Signup and view all the answers

How does the design of the ShoppingCart class ensure data integrity?

<p>By keeping sensitive fields protected or private. (C)</p> Signup and view all the answers

What feature allows different payment methods to be treated uniformly in the payment system?

<p>Polymorphism (D)</p> Signup and view all the answers

Which class should be derived from GameObject if new functionality like collision detection is to be added?

<p>Enemy (B)</p> Signup and view all the answers

What is the purpose of the virtual destructor in the GameObject class?

<p>To ensure proper cleanup of derived classes. (D)</p> Signup and view all the answers

Flashcards

Message Protocol

The collection of methods available for an object.

Message

A call to an object's method, containing method name and object.

Inheritance

A class acquiring characteristics of a parent class.

Access Controls (in inheritance)

Restricting access to class entities (variables and methods) to subclasses or clients.

Signup and view all the flashcards

Method Overriding

Sub classes can modify/replace parent class methods with a new version.

Signup and view all the flashcards

Class Variable

A variable shared by all objects of a class.

Signup and view all the flashcards

Instance Variable

A variable unique to each object of a class.

Signup and view all the flashcards

Dynamic Binding

Choosing the correct method at runtime, based on the object type.

Signup and view all the flashcards

Stack-Dynamic Objects

Objects allocated on the runtime stack. They are automatically created and destroyed when a function or block exits.

Signup and view all the flashcards

Heap-Dynamic Objects

Objects allocated on the heap (using 'new'). They persist until explicitly freed.

Signup and view all the flashcards

Object Slicing

The loss of information when a subclass object is assigned to a base class variable. The subclass-specific data is discarded.

Signup and view all the flashcards

Static Binding

Selecting the appropriate method at compile time, based on the declared type of the variable.

Signup and view all the flashcards

Nested Classes

Classes defined within another class, restricting their visibility and access.

Signup and view all the flashcards

Implicit Initialization

Automatic assignment of default values to object members when an object is created.

Signup and view all the flashcards

Explicit Initialization

Manually assigning specific values to object members during object creation.

Signup and view all the flashcards

Object-Oriented Programming (OOP)

A programming paradigm that uses objects and classes, focusing on data and methods that operate on that data. It emphasizes encapsulation, inheritance, and polymorphism.

Signup and view all the flashcards

Polymorphism

The ability of an object or method to take on multiple forms. In OOP, it allows different classes to respond to the same message in their own way.

Signup and view all the flashcards

Abstract Data Types (ADTs)

Data structures defined as a collection of data and operations that operate on that data.

Signup and view all the flashcards

Classes

Blueprint for creating objects in OOP. They define the data (attributes) and methods (operations) of objects.

Signup and view all the flashcards

Objects

Instances of classes. They have their own data values (attributes) and can execute actions defined by the class (methods).

Signup and view all the flashcards

Methods

Functions or subprograms associated with classes. Define the actions or operations an object can perform.

Signup and view all the flashcards

Derived Class (Subclass)

A class that inherits properties and methods from a parent class (superclass).

Signup and view all the flashcards

Abstract Method

A method declared without implementation. It defines a protocol, leaving the actual behavior to subclasses.

Signup and view all the flashcards

Abstract Class

A class that has at least one abstract method. It cannot be instantiated directly, needing subclasses to provide concrete implementations.

Signup and view all the flashcards

Single Inheritance

A subclass inherits from only one parent class.

Signup and view all the flashcards

Multiple Inheritance

A subclass inherits from two or more parent classes.

Signup and view all the flashcards

Objects on the Heap

Objects allocated dynamically at runtime, using mechanisms like 'new'. They persist until explicitly freed.

Signup and view all the flashcards

Objects on the Stack

Objects allocated automatically on the function call stack. They are automatically created and destroyed as functions enter and exit.

Signup and view all the flashcards

What is C++?

C++ is a popular object-oriented programming language that combines elements from C and Simula 67, offering excellent control over memory and system resources, yet allowing complex object-oriented designs.

Signup and view all the flashcards

C++ Inheritance

In C++, a class can inherit characteristics from another class, allowing for code reuse and creating specializations of the parent class. It follows the concept of inheritance hierarchy.

Signup and view all the flashcards

C++ Access controls

C++ uses access controls (private, public, protected) to restrict how members (variables and methods) of a class can be accessed from other parts of the program, including subclasses.

Signup and view all the flashcards

Private Derivation (C++)

In private derivation, inherited public and protected members of a parent class become private in the inheriting subclass. They are accessible only within the subclass itself.

Signup and view all the flashcards

Public Derivation (C++)

With public derivation, inherited public and protected members of a parent class retain their original access levels in the subclass.

Signup and view all the flashcards

C++ Constructor & Destructor

Constructors are special methods that initialize an object when it is created, while destructors are called when an object is destroyed, performing cleanup tasks.

Signup and view all the flashcards

C++'s Mixed Type System

C++ features a mixed typing system, allowing both static and dynamic type checking. This gives developers flexibility but can lead to runtime errors if not handled carefully.

Signup and view all the flashcards

C++'s Advantages

C++ offers power and flexibility, offering both low-level control and high-level abstraction. It's widely used for diverse applications, from operating systems to game development.

Signup and view all the flashcards

Polymorphism in C++

C++ allows different classes to respond to the same method call in their own way, based on the object's actual type. This is achieved through virtual functions and dynamic binding.

Signup and view all the flashcards

Static Binding in C++

The method to be called is determined at compile time, based on the declared type of the object, not its actual type.

Signup and view all the flashcards

Dynamic Binding in C++

The method to be called is determined at runtime, based on the actual type of the object.

Signup and view all the flashcards

Heap-Dynamic Objects in Java

Java objects are allocated on the heap using the 'new' keyword and are referenced through reference variables.

Signup and view all the flashcards

Java's 'finalize' Method

This method is implicitly called by the garbage collector before it reclaims the memory occupied by an object.

Signup and view all the flashcards

Wrapper Classes in Java

Java provides classes like Integer, Double, etc. to wrap primitive data types and allow them to be treated as objects.

Signup and view all the flashcards

Evaluation of OOP Support in C++ vs. Smalltalk

C++ offers greater access controls, multiple inheritance, but requires programmer decisions on binding types. Smalltalk is more flexible but slower due to dynamic binding and interpretation.

Signup and view all the flashcards

How Java Differs from C++

Java mostly focuses on heap-dynamic objects, uses automatic garbage collection, and has wrapper classes for primitive types.

Signup and view all the flashcards

Overriding Methods in C#

In a derived class, you can replace a method inherited from the parent class. Use the 'new' keyword to signal this.

Signup and view all the flashcards

Calling Parent Class Methods in C#

You can access a method of the parent class from within a derived class using the 'base' prefix.

Signup and view all the flashcards

C# Abstract Methods

A method declared without implementation. Subclasses must provide their own version of it.

Signup and view all the flashcards

Ruby Message Passing

Objects communicate by sending messages to each other, invoking methods.

Signup and view all the flashcards

Ruby Class Definitions

Class definitions in Ruby are executable, allowing you to add members to existing classes.

Signup and view all the flashcards

Ruby Method Access Controls

Ruby methods can be public, private, or protected. Access control is checked at runtime.

Signup and view all the flashcards

What does a CIR store?

A Class Instance Record (CIR) stores the state of an object. It's built at compile time and holds instance variables for an object.

Signup and view all the flashcards

How are subclass variables stored in Ruby?

When a class inherits from a parent class, the subclass's instance variables are added to the parent's CIR. This allows efficient access to all variables.

Signup and view all the flashcards

What is dynamic binding?

Dynamic binding allows choosing the correct method to execute at runtime based on the object's type. It's a key concept in OOP, enabling code to handle different object types with a single interface.

Signup and view all the flashcards

What is the 'vtable' in dynamic binding?

A vtable (virtual method table) is a storage structure used in dynamic binding. It holds pointers to the actual code for dynamically bound methods, allowing efficient method calls.

Signup and view all the flashcards

Define 'reflection' in programming.

Reflection allows programs to analyze their own structure and behavior at runtime, making it possible to dynamically modify their actions.

Signup and view all the flashcards

What is 'metadata' in reflection?

Metadata refers to the descriptive information about a program that allows reflection to work. It includes details about data types, structures, and functions.

Signup and view all the flashcards

What is 'introspection' in reflection?

Introspection refers to a program examining its own metadata, allowing it to understand its structure and behavior.

Signup and view all the flashcards

Define 'intercession' in the context of reflection.

Intercession in reflection involves altering the normal execution flow of a program using its metadata. It allows for modifications to behavior at runtime.

Signup and view all the flashcards

Reflection (in software)

A mechanism allowing programs to inspect and manipulate their own structure (classes, methods, fields) at runtime.

Signup and view all the flashcards

Reflection Uses

Reflection is used by various tools like class browsers, IDEs, debuggers, and testing systems to analyze and interact with code structure.

Signup and view all the flashcards

Java Reflection

Java uses the java.lang.Class class for limited reflection capabilities, providing access to methods like getMethod and getDeclaredMethod.

Signup and view all the flashcards

C# Reflection

C# utilizes System.Type for reflection, allowing code introspection and even dynamic code generation using System.Reflection.Emit.

Signup and view all the flashcards

Reflection Downside: Performance

Using reflection can negatively impact program performance due to the runtime overhead of looking up and invoking things dynamically.

Signup and view all the flashcards

Reflection Downside: Security

Reflection can expose private data and methods, compromising security and potentially violating type checks at compile time.

Signup and view all the flashcards

Reflection Downside: Portability

Some reflection code may not run under a security manager, making the application less portable across different environments.

Signup and view all the flashcards

Reflection Tradeoffs

Reflection provides flexibility and power for dynamic analysis and modification, but comes at costs like reduced performance, security risks, and potential portability issues.

Signup and view all the flashcards

Product Inheritance

A programming technique where a specialized product class (e.g., Electronics) inherits properties and methods from a general Product class, creating a hierarchy of related classes.

Signup and view all the flashcards

Shopping Cart Implementation

A common feature in e-commerce systems where products are temporarily stored in a virtual 'cart' before purchase, often using data structures like ArrayLists to manage the items.

Signup and view all the flashcards

ShoppingCart Class

A class that represents a shopping cart, allowing users to add, remove, and display products.

Signup and view all the flashcards

Product Class

A general class representing a product, possibly with a name, description, and a price.

Signup and view all the flashcards

Electronics Class

A subclass of Product specialized for electronic goods, potentially with additional attributes like brand.

Signup and view all the flashcards

Polymorphism in Payments

Using the same interface (Payment) to represent different payment methods (like credit card or PayPal) without modifying the code.

Signup and view all the flashcards

Payment Interface

An interface defining a common method for making payments, allowing flexible integration of different payment types.

Signup and view all the flashcards

CreditCardPayment Class

A class that implements the Payment interface and uses a credit card to make payments.

Signup and view all the flashcards

PayPalPayment Class

A class implementing the Payment interface, making payments through a PayPal account.

Signup and view all the flashcards

Inheritance (C++)

A class inherits properties and methods from a parent class, allowing code reuse and creating specializations.

Signup and view all the flashcards

GameObject Class (C++)

A base class for objects in a game, often containing virtual methods for movement, rendering, and collision detection.

Signup and view all the flashcards

Enemy Class (C++)

A subclass of GameObject specifically designed for enemies, potentially with additional features like unique movement patterns.

Signup and view all the flashcards

Polymorphism through Payment

Using an interface (like Payment) to represent different payment methods (e.g., Credit Card, PayPal) without modifying the existing code.

Signup and view all the flashcards

Inheritance Hierarchy (C++)

A system in which a subclass can inherit properties and methods from a parent class, creating a family of related classes.

Signup and view all the flashcards

Study Notes

Chapter 12 Topics

  • OOP Languages support procedural and data-oriented programming (e.g., C++) or functional programming (e.g., CLOS).
  • Newer languages may not support other paradigms (e.g., Java, C#).
  • Some languages are purely object-oriented (e.g., Smalltalk, Ruby).
  • Functional languages may support OOP, but aren't discussed here.

Object-Oriented Programming

  • Three major language features are:
    • Abstract data types (Chapter 11)
    • Inheritance
    • Polymorphism
  • Inheritance is central to OOP and supporting languages.

Inheritance

  • Productivity increases come from code reuse.
  • ADTs (Abstract Data Types) are reusable but often require changes.
  • Inheritance defines new classes in terms of existing ones, letting them inherit common parts.
  • Inheritance addresses code reuse after minor changes and the definition of classes in a hierarchy.

Object-Oriented Concepts

  • ADTs are called classes.
  • Class instances are objects.
  • A class that inherits is a derived class or subclass.
  • Classes from which another class inherits are parent classes or superclasses.
  • Subprograms defining operations on objects are called methods.
  • Calls to methods are messages.
  • An object's complete set of methods is its message protocol or message interface.
  • Messages have a method name and destination object.
  • A class often inherits all entities of its parent class.
  • Inheritance can be complex due to access controls to encapsulated entities– a class can hide entities from subclasses or clients.
  • A class can modify an inherited method by overriding it.

Object-Oriented Concepts (continued)

  • Several ways classes differ from their parent: Adding variables/methods, modifying inherited method behavior, and defining private variables/methods.

Object-Oriented Concepts (continued)

  • Two kinds of variables in a class: Class variables (one per class); Instance variables (one per object).
  • Methods in a class include: Class methods (accept messages to the class); Instance methods (accept messages to objects).
  • Disadvantages of inheritance: creates dependencies amongst classes that increase maintenance efforts.

Dynamic Binding

  • A polymorphic variable can reference objects of the class and any descendants.
  • Dynamic binding occurs when methods are called through polymorphic variables in a class hierarchy.
  • This allows for more easily extended software systems during development and maintenance.

Dynamic Binding Concepts

  • An abstract method only defines a protocol (without implementation).
  • An abstract class contains at least one virtual method.
  • An abstract class cannot be instantiated.

Design Issues for OOP Languages

  • Topics of discussion in OOP languages: Exclusivity of objects, relationship between subclasses and subtypes, single/multiple inheritance, object allocation/deallocation, dynamic/static binding, nested classes, object initialization.

The Exclusivity of Objects

  • The advantage of everything being an object is elegance and purity.
  • The disadvantage of these systems is the relatively slow operations on simple objects
  • Adding objects to an imperative-style typing system provides fast operations on simple objects but results in a confusing type system.

Are Subclasses Subtypes?

  • The relationship between a parent class and a subclass object is an "is-a" relationship.
  • If a derived class is "is-a" parent class, subclass objects should behave similarly to parent class objects.
  • A derived class is a subtype if it has an "is-a" relationship with its parent.
  • Subclasses can add variables/methods but must override parent methods "compatibly."
  • Subclasses inherit implementation, but subtypes inherit both interface and behavior.

Single and Multiple Inheritance

  • Multiple inheritance allows new classes to inherit from two or more classes.
  • Disadvantages of multiple inheritance: Increased complexity, potential inefficiency, dynamic binding.
  • In general, in cases where appropriate, multiple inheritance may be useful in cases where such relationships benefit it.

Allocation and Deallocation of Objects

  • Objects can be allocated from the runtime stack following the ADT paradigm, from heap storage or explicitly.
  • If objects are stack-based, a problem exists with subtypes (object slicing).
  • Deallocation can be explicit or implicit.

Dynamic and Static Binding

  • Should binding of messages to methods be dynamic?
  • If none are dynamic, the advantages of dynamic binding are lost; if all are dynamic, it's inefficient.
  • Design should allow the user to specify.

Nested Classes

  • Define a new class that's needed only by one class.
  • Can the new class be nested inside the class that needs it?
  • Issues: visibility facilities of the nesting class to the nested class or vice versa.

Initialization of Objects

  • Are objects initialized to values on creation?
  • Implicit or explicit initialization of objects.
  • Issues with parent class member initialization when creating a subclass object.

Support for OOP in Smalltalk

  • Smalltalk is a pure OOP language.
  • Everything is an object.
  • Objects use local memory to store data.
  • Computing occurs through objects sending messages to other objects.
  • Uses a heap to allocate objects.
  • Deallocation is implicit.
  • Classes cannot be nested in other classes.

Support for OOP in Smalltalk (continued)

  • A Smalltalk subclass inherits all instance variables, methods (instance and class), and parent class superclass.
  • All subclasses are subtypes.
  • Inheritance is implemented.
  • Doesn't support multiple inheritance.
  • Dynamic binding is fundamental, looking for a method in an object, its superclass, etc.
  • Evaluation: Simple syntax and high power, but slower compared to compiled imperative languages.

Support for OOP in C++

  • Evolved from C and SIMULA 67.
  • Widely used OOP language.
  • Mixed type system.
  • Constructors/destructors present.
  • Access controls (private, public, protected) for class entities.
  • Class doesn't need to inherit from another class.

Support for OOP in C++ (continued)

  • Access controls for members: Private (visible to members and friends); Public (visible to subclasses and clients); Protected (visible to class and subclasses; not clients).
  • Inheritance can be "private."
  • Public derivation passes public and protected members to subclasses as public/protected.
  • Reexportation using the scope resolution operator permits accessing a private member in a child class.

Support for OOP in C++ (continued)

  • Multiple inheritance is available.
  • Dynamic binding by defining methods to be virtual.
  • Pure virtual functions have no definition, and have no default behavior.
  • A class with a pure virtual function is abstract.
  • If objects are allocated from the stack, operations differ.
  • Evaluation: extensive access controls, multiple inheritance, and dynamic binding make it a very useful language.

Support for OOP in Java

  • Close relationship to C++.
  • Data (except primitives) are objects.
  • Primitive types have wrapper classes.
  • Objects are heap-dynamic.
  • Finalize method called when garbage collection reclaims storage.
  • Interfaces include only method declarations.
  • Methods can be final.
  • All subclasses are subtypes.

Support for OOP in Java (continued)

  • Dynamic binding is used unless a method is declared final.
  • Methods are static or private to avoid overriding.
  • Nested classes (inner, static, or local are hidden in package) are supported.
  • Evaluation: OOP design decisions similar to C++ but with no procedural programming support, parentless classes, and dynamic method binding (uses interfaces to support multiple inheritance).

Support for OOP in C#

  • Supports OOP mechanisms similar to Java.
  • Includes both classes and structs.
  • Classes similar to Java's; structs are less powerful, stack-dynamic constructs without inheritance.

Support for OOP in C# (continued)

  • Syntax for defining classes similar to C++.
  • Inherited methods in derived classes may be marked 'new' for replacement- otherwise, the original parent method can be invoked using "base.Draw()."
  • Subclasses are subtypes if no members are private on the parent class.
  • Only single inheritance is supported.

Support for OOP in C# (continued)

  • Dynamic binding is possible by marking methods virtual.
  • Derived class methods can override virtual parent methods (override keyword).
  • Abstract classes have abstract methods.
  • All C# classes are ultimately derived from a single class: Object.

Support for OOP in C# (continued)

  • Nested classes resemble Java's static nested classes.
  • C# nested classes cannot behave like Java's non-static nested classes.

Support for OOP in C# (continued)

  • Evaluation:
  • Newer C-based OO language.
  • Differences between it and Java are minor.

Support for OOP in Ruby

  • Everything is an object in Ruby.
  • Computations occur via message passing.
  • Class definitions are executable and allow secondary definitions to expand existing classes; method definitions are executable.
  • All variables are type-less references.
  • Access control is different for data and methods.
  • Methods can be public, private, or protected.
  • Access is checked at runtime.
  • Getters and setters declared using shortcuts.

Support for OOP in Ruby (continued)

  • Access to inherited methods can differ from parent classes.
  • Subclasses aren't necessarily subtypes.
  • All variables are typeless and polymorphic.
  • Doesn't support abstract classes and does not support multiple inheritance.
  • Access controls are weaker than in other languages.

Implementing OO Constructs

  • Interesting issues include storage structures for instance variables and dynamic binding of messages to methods.

Instance Data Storage

  • Class instance records (CIRs) store an object's state.
  • CIRs are static (built at compile time).
  • Subclass CIRs inherit parent variables.
  • Accessing instance variables from CIRs is efficient because access is done as in records.

Dynamic Binding of Methods Calls

  • Statically bound methods in a class aren't contained in the CIR.
  • Methods dynamically bound to CIRs are connected to the code using a pointer.
  • CIR storage structures are sometimes called virtual method tables (vtable).
  • Method calls can be offsets from the beginning of the vtable.

Reflection

  • Programming languages with reflection permit runtime access to object types and structure and ability to modify behavior.
  • Types/structure of programs are called metadata.
  • Examining program metadata is introspection.
  • Interceding program execution is intercession.

Reflection (continued)

  • Programs use reflection for software tools (e.g., class browsers).
  • Visual IDEs use type information.
  • Debuggers examine private fields and methods.
  • Test systems use reflection to know class methods.

Reflection in Java

  • Java provides limited reflection support from java.lang.Class.
  • Java runtime instantiates a Class object for each program object.
  • Class methods, getMethods, getDeclaredMethods/Method find public/all methods.

Reflection in C#

  • .NET languages place intermediate code in assemblies along with program metadata.
  • System.Type is the reflection namespace; getType() is used instead of getClass().

Downsides of Reflection

  • Reflection has performance implications.
  • Reflection exposes private fields and methods.
  • Reflection can negate early type checking.
  • Some code might not run under a security manager (making it non-portable).

Summary

  • OOP involves ADTs, inheritance, and dynamic binding.
  • Design issues include exclusivity of objects and subclasses.
  • Smalltalk is purely object-oriented, and C++ has a mixed type system.
  • Java and C# are primarily object-oriented, and Ruby is purely object-oriented.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser