Docsity

Object-Oriented Programming: Class Definitions and Relationships, Cheat Sheet of Computer Science

Partial preview of the text

Download Object-Oriented Programming: Class Definitions and Relationships and more Cheat Sheet Computer Science in PDF only on Docsity!

Prepared By Dr Jamal Zraqou

Part 1 -

Writing

Classes

Part 1

Object Oriented

Programming

Prepared by

Dr Jamal Zaraqo

Part 1

Writing

Classes

  • (^) Part 1 focuses on
    • (^) class definitions and relationships
    • (^) instance data
    • (^) encapsulation and Java modifiers
    • (^) method declaration and parameter passing
    • (^) constructors & destructor
    • (^) method design and overloading 1- 2

Encapsulation

An encapsulated object can be thought of as a black box – its inner workings are hidden from the client The client invokes the interface methods of the object, which manages the instance data 1- 4 Methods Data Client

Abstract Data Types (ADTs)

  • (^) Abstract Data Types (ADTs) are a crucial concept

in computer science and programming, providing a

way to encapsulate data and operations in a manner

that hides the implementation details. They define a

data structure along with the operations that can be

performed on it, without specifying how these

operations are implemented. This separation of

concerns allows for better organization of code,

modularity, and abstraction.

  • (^) Here are some common ADTs along with their

meanings and foundations:

1. Stack

2. Queue

3. Linked List

4. Tree:

5. Graph:

Abstract Data Types (ADTs)

  1. Tree: A tree is a hierarchical data structure composed of nodes, where each node has a value and zero or more child nodes. Trees have a root node at the top and can have various properties like binary trees, binary search trees, balanced trees, etc.
  2. Graph: A graph is a collection of nodes (vertices) and edges that connect pairs of nodes. Graphs can be directed or undirected and can have weighted edges. They are used to model relationships between objects in various applications.
  3. HashTable: A hash table is a data structure that stores key-value pairs. It uses a hash function to compute an index where the value can be stored or retrieved quickly. Hash tables offer efficient insertion, deletion, and lookup operations.
  4. Heap: A heap is a specialized tree-based data structure that satisfies the heap property. In a max heap, the parent node's value is greater than or equal to the values of its children, while in a min heap, the parent node's value is less than or equal to the values of its children. Heaps are commonly used to implement

Visibility

Modifiers

In Java, we

accomplish

encapsulation

through the

appropriate use of

visibility modifiers

We've used the final

modifier to define

constants

Java has three

visibility modifiers:

public, protected,

and private

1- 8

Accessors

and Mutators

 Because instance data is

private, a class usually

provides services to access

and modify data values

 An accessor method returns

the current value of a

variable

 A mutator method changes

the value of a variable

 The names of accessor and

mutator methods take the

form getX and setX,

respectively, where X is the

name of the value

Parameters When a method is called, the actual parameters in the invocation are copied into the formal parameters in the method header char calc (int num1, int num2, string message) { int sum = num1 + num2; char result = message.charAt (sum); return result; } ch = obj.calc (25, count, "Hello");

Destructors Revisited

Each class has only one destructor

(called finalizer in some other OOP

languages)

Name of a destructor is the ~ character,

followed by the class name

Destructors do not receive any

arguments

 Can take no argument(s)
 Cannot return values

1- 13

1. protected void finalize throws Throwable()

  1. {
  2. //resources to be close
  3. }

Static Class Members

  • (^) Recall that a static method is one that can be

invoked through its class name

  • (^) For example, the methods of the Math class are

static:

result = System.Math.sqrt(25)

  • (^) Variables can be static as well
  • (^) Determining if a method or variable should be

static is an important design decision

1- 14

Static Class Members

 (^) The order of the modifiers can be interchanged, but by convention visibility modifiers come first  (^) Recall that the Main method is static – it is invoked by the Java compiler without creating an object  (^) Static methods cannot reference instance variables because instance variables don't exist until an object exists  (^) However, a static method can reference static variables or local variables 1- 16

Class Relationships  (^) Classes in a software system can have various types of relationships with each other  (^) Three of the most common relationships:  (^) Dependency: A uses B  (^) Aggregation: A has-a B  (^) Inheritance: A is-a B  (^) Let's discuss dependency and aggregation further  (^) Inheritance is discussed in detail in Chapter 8

Dependency  (^) Some dependencies occur between objects of the same class  (^) A method of the class may accept an object of the same class as a parameter  (^) For example, the Replace method of the string class takes as two parameters for other string objects: string st1="University",st2=“i”; st1 = st1.Replace(st2, "a"); System.Console.WriteLine(st2); Output: Unaversaty This drives home the idea that the service is being requested from a particular object

Aggregation

An aggregate is an object that is made up of

other objects

Therefore aggregation is a has-a relationship

A car has a chassis

In software, an aggregate object contains

references to other objects as instance data

The aggregate object is defined in part by the

objects that make it up

This is a special kind of dependency – the

aggregate usually relies on the objects that

compose it