Unit 3 codio

3.1 Abstraction and program design

Abstraction is the process of reducing complexity by focusing on the main idea.

ex. we don’t need to know every component of a car to be able to drive it.


Procedural Abstraction

Here’s how procedural abstraction helps to manage complexity:

  1. Method decomposition: Break large behaviors of the class into smaller, easier-to-manage methods representing an individual smaller behavior

  2. Code reuse: Write methods that perform common tasks to avoid repeating the same code

  3. Flexibility: Improve how a method works (make it faster, use less memory, etc.) without changing the rest of the program, as long as the method signature and purpose stay the same.


Data abstraction provides a separation between the abstract properties of a data type and the concrete details of its representation.

An attribute is a type of data abstraction that is defined in a class outside any method or constructor.

An instance variable is an attribute whose value is unique to each instance of the class.

A class variable is an attribute shared by all instances of the class


3.2 Impact Of program design

Key Considerations in Program Design

When designing your programs, keep these factors in mind:

  1. Modularity

    • Explanation: Modularity refers to dividing a program into independent, reusable components.

    • How OOP uses it:

      • Classes represent modular units, each encapsulating related data (attributes) and behaviors (methods).

      • Objects are instances of classes and interact through well-defined interfaces.

      • Example: A Student class with methods like calculateGPA() and enrollInCourse() can be reused in multiple programs involving student data.

  2. Encapsulation

    • Explanation: Encapsulation is the bundling of data and methods that operate on that data into a single unit, while restricting access to some components.

    • How OOP uses it:

      • Data members (attributes) of a class are usually marked as private.

      • Public methods (accessor and mutator) provide controlled access to private data.

      • Example: A BankAccount class hides the balance attribute and provides deposit() and withdraw() methods to modify it securely.

  3. Abstraction

    • Explanation: Abstraction involves hiding the complex implementation details and exposing only essential features.

    • How OOP uses it:

      • Classes and objects focus on “what” they do rather than “how” they do it.

      • Abstract classes and interfaces define behaviors that derived classes implement, allowing developers to work at a higher level of design.

      • Example: A Shape abstract class with a calculateArea()method lets derived classes like Circle and Rectangle implement the specific logic.

  4. Flexibility

    • Explanation: Flexibility refers to the ability to adapt or extend the program with minimal changes.

    • How OOP uses it:

      • Polymorphism enables the use of a single interface for different types (e.g., method overriding in subclasses).

      • Inheritance allows for extending existing classes to add or modify functionality.

      • Example: Adding a new Triangle class that extends the Shape class requires minimal changes to code using the Shape interface.

Example: A real-world example of a school management system.

  • Modularity: Separate Student, Teacher, and Course classes.

  • Encapsulation: Student class keeps grades private and provides methods to access or update them.

  • Abstraction: An abstract Person class defines shared behaviors (getName()), which Student and Teacher implement.

  • Flexibility: Adding a Staff class extending Person doesn’t require changes to existing code that interacts with Person objects.



3.3 Anatomy of a Class

Access modifiers:

  • public: Allows access from any other class. When a class, method, or constructor is declared public, it can be accessed by external classes.

  • private: Restricts access to the declaring class. Members (like variables or methods) marked private can only be accessed within the same class.


Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes.


Benefits of Data Encapsulation:

Encapsulation ensures:

  • Data security: Prevents external modification of sensitive data.

  • Code maintainability: Changes to internal implementation do not affect external code that interacts with the class.

  • Clear separation: Provides a clear distinction between a class’s internal implementation and its public interface.


Data encapsulation is a technique in which the implementation details of a class are kept hidden from external classes.


The toString method is a special method that returns a String representation of an object. Its primary purpose is to provide a textual representation of an object’s state that is human-readable and useful for debugging.

Student s = new Student("Alex", 101); System.out.println(s); // Might print something like: Student@15db9742
System.out.println(s); /*handles null better*/ == System.out.println(s.toString());


in practice:

public String toString() 
{
    return name + " (ID: " + id + ")";
}
System.out.println(s);

when name is Alex and ID is 101:

==Alex (ID: 101)


3.4 Constructors


3.5