OOP (W1)

Cards (26)

  • Object Oriented Programming (OOP)

    A programming paradigm that focuses on the use of objects to represent and manipulate data
  • OOP
    • Data is encapsulated within the objects
    • Objects are defined by their properties (attributes) & behaviors (methods)
    • Enables developers to write modular, reusable, and maintainable code
  • Objects
    • Instances of a class which are created from a class in java or any other languages
    • They have states and behavior
    • Correspond to things found in the real world i.e. real entities
    • Called run-time entities of the world
  • Instant variables

    Variables or data defined within a class
  • Code
    Always contained in the methods
  • Members of a class
    Methods & variables defined within a class
  • OOP concepts defined in Java
    • Abstraction
    • Encapsulation
    • Inheritance
    • Polymorphism
  • Abstraction
    • Represents simple things like objects, classes & variables as more complex underlying code and data
    • Lets you avoid repeating the same work multiple times
  • Encapsulation
    • Practice of keeping fields within a class private, then providing access to those fields via public methods
    • Protective barrier that keeps the data and code safe within the class itself
  • Inheritance
    • Lets programmers create new classes that share some of the attributes of existing classes
    • Lets us build on previous work without reinventing the wheel
  • Polymorphism
    • Allows programmers to use the same word in Java to mean different things in different contexts
    • One form is method overloading, another is method overriding
  • How Abstraction works

    Enables programmers to create complex systems by breaking them down into smaller, more manageable components
  • Abstract keyword
    Non-modifier, used for classes and methods
  • Abstract class

    Restricted class that cannot be used to create objects, must be inherited from another class
  • Abstract method

    Can only be used in an abstract class, and does not have a body. The body is provided by the subclass.
  • Data abstraction
    Process of hiding certain details and showing only essential information to the user
  • Interfaces
    Collection of methods, used to define a set of behaviors that a class should implement
  • How Encapsulation works
    Lets us reuse functionality without jeopardizing security
  • Benefits of Encapsulation
    • Data hiding - protects data from unauthorized access and manipulation
    • Modularity - breaks down complex systems into smaller, more manageable components
    • Flexibility - allows changes to internal implementation without affecting external interface
  • Access modifiers in Java
    • Public - variables and methods can be accessed from anywhere
    • Private - variables and methods can only be accessed within the class they are defined in
    • Protected - variables and methods can be accessed within the same class and its subclasses
  • How Inheritance works
    1. Lets a new class adopt the properties of another
    2. The inheriting class is called a subclass or child class
    3. The original class is called the parent or superclass
    4. We use the keyword 'extends' to define a new class that inherits from an old class
  • Benefits of Inheritance
    • Reusability - subclass can reuse code and functionality already defined in the superclass
    • Polymorphism - objects of different subclasses can be treated as objects of the same superclass
    • Flexibility - new features can be added to an existing class hierarchy without modifying existing code
  • How Polymorphism works
    1. Uses a reference to a parent class to affect an object in the child class
    2. Examples include method overriding and method overloading
  • Method overriding
    The child class can override a method of its parent class, allowing the same method to be used in different ways
  • Method overloading
    A single method may perform different functions depending on the context in which it's called
  • Benefits of Polymorphism
    • Flexibility - enables more flexible and adaptable code
    • Code reuse - promotes code reuse by allowing classes to inherit functionality
    • Simplification - enables the use of generic code that can handle different types of objects