Review of OOP

Cards (12)

  • Object-Oriented Programming (OOP)

    A computer programming model that organizes software design around data, or objects, rather than functions and logic
  • Object
    A data field that has unique attributes and behavior
  • Structure of Object Oriented Programming
    • Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods
    • Objects are instances of a class created with specifically defined data. Objects can correspond to real-world objects or an abstract entity
    • Methods are functions that are defined inside a class that describe the behaviors of an object
    • Attributes are defined in the class template and represent the state of an object
  • Main Principles of OOP
    • Encapsulation: containing information in an object, exposing only selected information.
    • Inheritance: child classes inherit data and behaviors from parent class.
    • Abstraction: only exposing high level public methods for accessing an object.
    • Polymorphism: many methods can do the same task.
  • Encapsulation
    Containing information in an object, exposing only selected information
  • Advantages of Encapsulation
    • Data Hiding: The user will have no idea about the inner implementation of the class
    • Increased Flexibility: We can make the variables of the class read-only or write-only
    • Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements
    • Testing code is easy: Encapsulated code is easy to test for unit testing
  • Abstraction
    The process of hiding certain details and showing only essential information to the user
  • Abstract Classes and Abstract Methods
    • An abstract class is a class that is declared with an abstract keyword
    • An abstract method is a method that is declared without implementation
    • An abstract class may or may not have all abstract methods. Some of them can be concrete methods
    • A method defined abstract must always be redefined in the subclass, thus making overriding compulsory OR either make the subclass itself abstract
    • Any class that contains one or more abstract methods must also be declared with an abstract keyword
    • There can be no object of an abstract class. That is, an abstract class can not be directly instantiated with the new operator
    • An abstract class can have parameterized constructors and the default constructor is always present in an abstract class
  • Polymorphism
    The ability of a message to be displayed in more than one form
  • Types of Polymorphism in Java
    • Compile-time Polymorphism (Static Polymorphism): Achieved by function overloading or operator overloading
    • Runtime Polymorphism (Dynamic Method Dispatch): A process in which a function call to the overridden method is resolved at Runtime
  • Abstraction
    can be achieved with either abstract classes or interfaces.
  • Encapsulation
    is defined as the wrapping up of data under a single unit. It is the mechanism that binds together code and the data in manipulates.