Copy of Computer Science Notes Object Orientation.docx
OBJECT-ORIENTED PROGRAMMING MAIN CONCEPTS
Java is an Object oriented (OO) language
What is OOP?
- A programming paradigm based on the concept of "objects," which can contain data and methods.
Kep principles of OOP
- Encapsulation: Bundling of data (attributes) and methods (behavior) within a class.
- Access modifiers (private, public, protected, default) control visibility.
- Example: Using private instance variables with public getter and setter methods.
- Inheritance: Classes can derive properties and behavior from other classes.
- Keywords: extends for classes, super to refer to parent class methods.
- Relationship: "is-a" (e.g., Dog is-a Animal).
- Single Inheritance: Java supports only single inheritance for classes.
- Polymorphism: The ability of different objects to respond to the same method call in different ways.
- Types:
- Compile-time (method overloading).
- Runtime (method overriding).
- Types:
- Abstraction: Hiding the implementation details and exposing only the functionality.
- Achieved via abstract classes or interfaces.
Combined Java Notes with Vocabulary List
Combined Topics Overview
- Object Orientation Basics
- Object, Class, Instance, Method, Constructor
- Core OOP principles: Encapsulation, Inheritance, Polymorphism, Abstraction
- Access Modifiers (private, public, protected)
- Objects and Classes
- Defining and using classes
- Creating and managing objects
- Attributes and behaviors of objects
- this and super keywords for object interactions
- Accessors and Mutators
- Getters (Accessor methods) and Setters (Mutator methods)
- Encapsulation through controlled access to private fields
- Inheritance and Polymorphism
- Parent and child classes
- Overriding vs Overloading
- Dynamic Polymorphism
- Importance of the super keyword
Key Vocabulary
General OOP Concepts
- Object: A specific instance of a class, containing attributes and behaviors.
- Class: A blueprint for creating objects, defining their attributes and methods.
- Instance Variable: A variable defined in a class that represents an attribute of an object.
- Method: A function defined in a class that represents a behavior of an object.
- Constructor: A special method that initializes new objects.
- Encapsulation: The bundling of data and methods within a class, often using access modifiers to restrict direct access to internal details.
- Inheritance: A mechanism by which one class (child) can acquire the properties and methods of another class (parent).
- Polymorphism: The ability of objects to take on multiple forms, typically by overriding methods in a parent class.
- Abstraction: The concept of hiding implementation details while exposing only the necessary functionality.
Java-Specific Keywords
- private: Access modifier restricting access to within the class.
- public: Access modifier allowing access from any class.
- protected: Allows access within the same package and subclasses.
- static: Defines methods or variables shared by all instances of a class.
- final: Prevents modification of variables, methods, or classes.
Constructors
- Default Constructor: A no-argument constructor provided by Java or explicitly defined in a class.
- Parameterized Constructor: A constructor with arguments to initialize an object with specific values.
- Constructor Overloading: Defining multiple constructors with different parameter lists.
Accessors and Mutators
- Getter (Accessor): A method that retrieves the value of a private instance variable.
- Setter (Mutator): A method that sets or updates the value of a private instance variable.
- toString(): A method that returns a string representation of an object.
this and super Keywords
- this: Refers to the current instance of a class.
- Used to differentiate instance variables from parameters with the same name.
- Enables chaining constructors within the same class.
- super: Refers to the immediate parent class.
- Used to call a superclass constructor or method.
- Resolves conflicts in overridden methods or fields.
Inheritance and Method Behavior
- Parent Class (Superclass): The class from which properties and methods are inherited.
- Child Class (Subclass): The class that inherits properties and methods from a parent class.
- Overriding: Redefining a method in a subclass that already exists in the parent class.
- Overloading: Defining multiple methods with the same name but different parameter lists.
- Dynamic Polymorphism: Method overriding determined at runtime based on the object’s actual type.
Common Practices
- Encapsulation: Keep fields private and provide getters and setters for controlled access.
- Chaining: Use this() or super() for constructor chaining.
- Abstraction: Use abstract classes and interfaces to define generic behaviors.
These terms and concepts provide a foundational understanding of Java programming and object-oriented principles. Let me know if you'd like to expand any particular area or need examples!
CLASSES AND OBJECTS
Class
- A class is a file in java
- A class is a description of a kind of object.
- Contains attributes and methods that define the behavior and state of the object
- The class definition serves as a blueprint for the object creation
- A class is merely a plan for a possible object.
- It does not by itself create any objects.
- When a programmer wants to create an object the new operator is used with the name of the class.
- Creating an object is called instantiation.
- When we instantiate an object, we create an instance of a class
- We use classes to create objects of a certain type

- Syntax
Classes are like cookie cutters
- They can be used many times to make many objects
- Different objects may have different characteristics, even though they follow the same basic pattern.
- Classes have their own characteristics as well
- a characteristic of a class definition that is not shared by its objects is called static
- if something is static then there is only one of it
- There will only be one of the static object
- if something is static then there is only one of it
- a characteristic of a class definition that is not shared by its objects is called static
When you run a Java application by typing java someClass, the first method that starts is:
- The main() method of someClass
what an object is
- An object can be anything
- Can be physical, mental, natural, artificial, human-made, etc..
- In java:
- They are an instance of a class
- Anytime we need to model an entity, we can create it as an object in code
- An object can have attributes to describe them,
- The values in the attributes refer to the object
- Ex. an ATM could have the attribute cash
- Here would be $50,000 in the machine
- Objects can also perform actions or behaviors
- They look like procedures(methods)
- A message is the passing of info or data between objects
- Another word for creating an object is instantiation
- Syntax
Characteristics of objects in real life AND software objects
- Identity
- Objects are distinct from each other
- State
- Various properties which can change
- Behavior
- it can do things and can have things done to it
The Von Neumann architecture consists of a single, shared memory for programs and data, a single bus for memory access, an arithmetic unit, and a program control unit.
Instance variables
- Instance variables are declared at the same level as methods within a class definition.
- They are usually given private access to restrict visibility.
- They can receive initial values either when they are declared or in a constructor

Constructors:
- Used to create objects
- Constructors in java are methods used to create the instance of the class.
- It's purpose is To initialize the instance variables/objects of an object
- Each instantiated object of the class has a separate copy or instance of that variable.
- We use constructors for a class When the class has instance variables
- often are used with values (called parameters) that are to be stored in the data part of the object that is created.
- A constructor MUST have the same name as the class.
- There are usually several different constructors in a class, each with different parameters.
- This is called Overloading
- However, all the constructors of a class create the same type of object.
- The amount of objects able to be constructed is as many as the program asks for
- There is no limit
- Syntax
When you don’t write a constructor for a class, the class will be created with a default no-argument constructor
Initialization vs declaration
- Declaration: String name;
- Initialization: String name = “Steve”;
Static methods and variables
- Static variables: Shared across all instances of a class.
- Static Methods: Belong to the class, not to any object
- The methods that belong to a class definition are called static methods.
- Note: A program can execute a static method without first creating an object!
- Main methods are static, so there is only one of them
- When something is static, it means
- Variables and methods that are part of the class definition, but not of its objects
OBJECTS - INSTANCES OF CLASSES
CONSTRUCTORS
Using String Objects
- The String class also has two instance variables: value, which contains the string’s characters, such as “Hello98”, and count, which records the number of characters in the string.
- An instance variable is a variable that is specific to a certain object.
- They are Variables that hold data for each object
- The value of an instance variable can be changed by any method in the class, but it is not accessible from outside the class
- An instance variable is a variable that is specific to a certain object.
- We can use object ,methods to perform certain actions
- Ex. .length()
To use one of the String methods in a program, we must first create a String object
- Declaring string:
- String str; // Declare a String variable named str
- Creating string object
- str = new String("Hello"); // Create a String containing the word "hello"
- UML Ex.
- We can also use a constructor with an empty parameter list.
- String str2 = new String(); // Create a String
- This creates an empty string
- Value = “”
- Count = 0
- This creates an empty string
- String str2 = new String(); // Create a String
- Ss
Difference between primitive and object variable default values
- Primitive types have a fixed size, but objects do not.
- An int in Java is always 32 bits. But a String or a Riddle object can vary in size.
- As soon as a primitive variable is declared, it is assigned a default value of the appropriate type.
- if an object variable is declared, and not assigned an object, its default value is null.
Concatenating (combining) strings
- concat(String) method can be used to concatenate two strings
- This method takes a String argument. And it returns a String that combines a String argument to the String that the method is called on.

- Another way to concatenate strings is by adding them together
- System.out.println(s1 + s2);
String equals method
- .equals() method is used to compare stings
- If they have the same characters → True
- If not → false
Inheritance
Wh inherit?
- Different kinds of objects sometimes have a certain amount in common with each other
- Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.
- This is called creating a subclass or a subtype
- In java, we accomplish this by using the keyword extends
- A class that is derived from another class inherits state and behaviour from that other class
- A subclass automatically takes on all the behavior and attributes of its base class.
- If you need to create several classes to describe types that aren’t identical but have many features in common, you can create a base class that defines all the common features.
- Then you can create subclasses that inherit the common features.
SIngle inheritance
- Java only allows single inheritance – you can only extend one class
- The class that is used to define a new one is called the parent class
- The subtype or the new class is called the child class
Is-a
- Inheritance relationships are usually referred to as an “is-a” relationship
- Examples:
- A giraffe is-a(n) animal
- a disk is-a circle
- tricycle is-a bike
- Etc…
Syntax
- public class <child_name> extends <parent_name> {
- <class definition>
- }

Abstract Classes
- They are declared abstracts
- It may include abstract methods
- They cannot be instantiated but they can be subclasses.
- When it is subclassed, the subclass will usually provide implementations for all of the abstract methods in the parent class.
- However, if it doesn’t, then the subclass must also be declared abstract
Polymorphism
Polymorphism is a feature of object-oriented languages whereby the same method call can lead to different behaviors depending on the type of object on which the method call is made.
- The term polymorphism means, literally, having many (poly) shapes (morphs)
- Polymorphism can only be achieved with/through inheritance
In java, there are 3 kidneys of polymorphism
- Overriding an inherited method.
- Implementing an abstract method.
- Implementing a Java interface.
Method overloading
- overloaded methods—that is, methods that have the same name but different parameter lists.
Method Overriding
- A polymorphic method is a method signature that behaves differently when it is invoked on different objects.
- An overridden method, such as the toString() method, is an example of a polymorphic method, because its use can lead to different behaviors depending upon the object on which it is invoked.
Overriding vs overloading:
- Overloading is about the same method having different signatures. Overriding is about the same method, and same signature but different classes connected through inheritance.
- Overloading is an example of compile-time polymorphism. Overriding is an example of run-time polymorphism

More on polymorphism ideas:
- Because a subclass is derived from a superclass, a superclass object can reference an object of the subclass
- Example: the following statements are valid because Circle inherits Shape:
- Shape myShape;
- Circle myCircle = new Circle();
- myShape = myCircle;
- Full example:
Abstract Classes
- An abstract class is a class that cannot be instantiated – however, it CAN be the parent of other classes
- The purpose of an abstract class is to be a parent to several related classes
- The parent class would define state and behaviour for all, however, the children would inherit this state and behaviour but implement according to its behaviour
- Abstract classes often have a partial implementation – like a method signature with no implementation – which is why they can’t be instantiated.
Defining an abstract class
Example:
- Generic greeting card
Abstract methods
- The abstract Method is used for creating blueprints for classes or interfaces.
- Here methods are defined but these methods don’t provide the implementation.
- Abstract Methods can only be implemented using subclasses or classes that implement the interfaces.
- These methods are sometimes referred to as subclasser responsibility because they have no implementation specified in the super-class.
- Thus, a subclass must override them to provide a method definition.
- Declaring:
- abstract type method-name(parameter-list);
- no method body is present
- Any concrete class(i.e. class without abstract keyword) that extends an abstract class must override all the abstract methods of the class.
- Rules:
- Any class that contains one or more abstract methods must also be declared abstract.
- If a class contains an abstract method it needs to be abstract and vice versa is not true.
- If a non-abstract class extends an abstract class, then the class must implement all the abstract methods of the abstract class else the concrete class has to be declared as abstract as well.
- abstract type method-name(parameter-list);
Getters and Setters
![]()
![]()
![]()
THIS KEYWORD
![]()
![]()
![]()
“SUPER” KEYWORD
![]()
![]()
![]()
UML
What is UML?
- UML = Unified Modeling Language
- It’s a graphical notation used to construct and visualize object oriented systems
- UML can be used to describe the structure of a system showing:
- Classes
- Attributes
- Operations
- Relationships among objects
UML - Unified Modeling Language: A way to illustrate object design
- UML can be used across languages
- In UML, objects are represented by rectangles that are labeled with a two-part label of the form id:Type. The object’s label is always underlined
- Ex.

- Lobby is the id and ATM is the type
- UML can have attributes and values
- Ex.

- Cash is the attribute, the amount of cash is the value
- There can be many other attributes such as balance, depositing/withdrawing money, etc…
- Ex.
- Classes:

- Class design in UML
UML notation
- A class represents a concept which encapsulates states(attributes) and behavior (operations)
- Each attribute has a type
- Each operation has a signature
- The only mandatory information is the class name

Relationships between classes
- UML helps us to convey how code should be implemented from diagrams.
- The following are a list of relationships that can be represented in our class diagrams:
- We will focus on a couple of these relationship

Visibility
- + private
- - public
- # protected
- ~ default
Objects are private methods are public
Inheritance
- Represented using a hollow arrow head
- Abstract class names appear in italics
- Points from child to parent element
Association and multiplicity
- These are relationships between UML class diagrams
- They are represented by a solid line between classes
- Associations are usually named using verbs that reflect real world problem
- Cardinally (multiplicity) is expressed as:
- One to one
- One to many
- Many to many

Other relationships

- Aggregation
- Represent “part-of” relationships
- Class2 is part of Class1
- Class1 and Class2 have separate lifespans

- Composition
- A special type of aggregation where parts are destroyed when the whole is destroyed.
- Class1 and Class2 have the same lifespan

Big example