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:
Method decomposition: Break large behaviors of the class into smaller, easier-to-manage methods representing an individual smaller behavior
Code reuse: Write methods that perform common tasks to avoid repeating the same code
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:
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
Studentclass with methods likecalculateGPA()andenrollInCourse()can be reused in multiple programs involving student data.
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
BankAccountclass hides the balance attribute and providesdeposit()andwithdraw()methods to modify it securely.
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
Shapeabstract class with acalculateArea()method lets derived classes likeCircleandRectangleimplement the specific logic.
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
Triangleclass that extends theShapeclass requires minimal changes to code using theShapeinterface.
Example: A real-world example of a school management system.
Modularity: Separate
Student,Teacher, andCourseclasses.Encapsulation:
Studentclass keepsgradesprivate and provides methods to access or update them.Abstraction: An abstract
Personclass defines shared behaviors (getName()), whichStudentandTeacherimplement.Flexibility: Adding a
Staffclass extendingPersondoesn’t require changes to existing code that interacts withPersonobjects.
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
privatecan 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@15db9742System.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)