Skip to content

OOPS

  • Classes
    • Blueprint/template for the objects
    • has
      • fields/properties
      • methods/behaviour
      • constructor
        • By default, JVM injects a default constructor(no-args constructor)
  • Objects
    • Instances of the class
  • Constructor
    • used to initialize the internal state
    • [DEFAULT] No-Argument Constructor, the compiler adds a default, no-argument constructor
      • default value for primitives types are 0 or 0.0 or false
      • default value for reference types(objects) are null
    • Parameterized constructor
    • Copy Constructor
    • Chained Constructor this(arg1, arg2); or super(arg1, arg2)
      • this or super expression MUST be the first statement to use in the constructor
    • Value Objects
      • Don't change its instance state after they are created
      • Fields are marked as final i.e immutable instances
    • If too many constructor are there then, may be we can use the Creational Design Pattern for better object creation
      • Singleton
      • Factory
      • Abstract Factory
      • Builder etc
  • Access Modifiers
    • [default] package private - all members are visible within the same package accessible
    • public - we’re making it available to the whole world
    • private - accessible from the same class only
    • protected - same package (as with package-private access level), as well as from all subclasses of its class
  • Concrete Class
    • can create instance of using the new keyword (can be instantiated)
    • Eg: HashMap, HashSet, ArrayList, and LinkedList
  • Encapsulation
    • responsible for bundling methods and data, while information hiding is a consequence of encapsulation — it hides an object’s internal representation.
    • information hiding
    • achieved using
      • access modifiers like private
  • Abstraction
    • Flexibility to think in more general terms about the domain we are trying to model
    • achieved using
      • interface
        • blueprint for a class
        • Eg: Map, List, and Set
      • abstract classes
        • class that has unimplemented methods
        • Eg: AbstractMap and AbstractList.
  • Inheritence
    • enables us to reuse existing code or extend an existing type
    • subclass class inherits the non-static protected and public members from the superclass class
    • private and static members of a class are not inherited.
    • this and super keywords are extensively used to access the current and parent class members
    • Inheritence is a IS-A relation
    • Composition is a HAS-A kind of relation
  • Association
    • Aggregation - Have independent life-times for the dependent objects and can exist independently to the parent object (Eg: student-departement)
    • Composition (HAS-A relation) - child cannot exist independent (Eg: company-departement)