Object-Oriented Programming (OOP)
What Is Object-Oriented Programming?
- Class is an abstract blueprint (the source of the plan) used to instantiate more specific, concrete objects based on that blueprint.
- Use objects to represent things you are programming about, variables as properties, and functions as methods.
- Creates an object, so the object is the implementation of the class that is physically available (someplace in the memory in the RAM is occupied for that)
- Every class has at least one constructor
this()— call to another constructor within the class
super()— call to a constructor in the parent class
- does not belong to a particular instance
- the variable/method shared among all instances (objects) of a class
- if we create many instances of that class, there is only one static variable assigned in the memory.
Don’t Be Tricked By Names!!!
Just different ways of calling them, but they are all operating the same thing (slightly different like method is a function that is attached to an object)
- variable, attribute, property, field
- function, method, routine, sub-routine
- parameter (method definition), argument (inside parenthesis when invoking the function)
4 Pillars in OOP
- the ability to hide variables within the class from outside access
- class parameters will be defined as private, we use getter and setter methods to access class variables
- focus on keeping the data safe from outside access and misuse, and implementation
- Benefit: reduce complexity + increase reusability
- shows only essential attributes and hides unnecessarily implementation detailed information from the users
- focus on removing unnecessary information and design
- Benefit: reduce complexity + isolate impacts of change
- a subclass extends all properties of the parent class into itself
- both child and parent classes share a set of attributes and methods
- a subclass inherits everything of its superclass has used the keyword
- Benefit: eliminate redundant code, good for reusability
- a single action can be performed in many forms without requiring more information
- allow the current part of the system to interact with a new object without concern for specific properties of the new objects
- hiding the details in communication, and can pass “is a” test
- when a class inherits the behaviors of another class but has the ability to not inherit everything and change some of its inherited behaviors. For example to write a method that does something different from the inherited method
overloading: methods share the same name with different parameters (occurs during compile time)
overriding: two methods with exactly the same name and same parameters, a subclass can also provide a new definition to a method that it inherits (occurs during run time)
- Benefit: code can be extended, more dynamic
Encapsulation — Access Control
- private: only available within the class, not outside the class
- default: package access, the member in another class but the same package
- protected: member in superclass in a different package and the same package
- public: different packages and different package
Abstraction — Abstract and Interfaces
- Specification inheritance can be achieved through abstract classes or interfaces in Java.
- You should favor composition over implementation inheritance when reusing the implementation.