OOP - Principles Flashcards
OOP PRINCIPLES:
- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
Encapsulation
DEFINE
Encapsulation is restricting direct access to make sure that “sensitive” data is hidden from users
Encapsulation
HOW TO ACHIEVE ENCAPSULATION
- Private instance variables
- Public setters and getters methods
Note: These public getters and setters’ method will help us to access and update private instance variables
Getter vs Setter
GETTER
- is used to retrieve (read) data
- is always return type
SETTER
- is used to set (write) data
- is mostly void type
Encapsulation
WHY DO WE NEED ENCAPSULATION
- Better control of data by public getter and setter methods
- Better control of class attributes and methods
- Class instance variables can be made read-only (if you only use the get method), or write-only (if you only use the set method)
- Flexible: the programmer can change one part of the code without affecting other parts
- Increased security of data
ADVANTAGES OF OOP PRINCIPLES
- Store and manipulate data & provide safety
- Reuse code - efficiency, saves us time and effort saves company money
- Maintain application
Inheritance
DEFINE
It is receiving or sharing class members (methods and instance variables) from one class (parent class) to another class (child classes) with the extends keyword.
Inheritance
HOW TO ACHIEVE INHERITANCE
- we use the “extends” keyword
WHAT CAN BE INHERITED?
-In the same package: public - protected - default
-Different packages: public - protected
NOTE: There is no way to inherit private class members because private class members can only be used in the same class
Inheritance
WHY DO WE USE INHERITANCE?
- It is useful for code reusability: with the help of inheritance, we can reuse attributes and methods of a parent class when we create a new child class
- In most cases, most of the methods and attributes are already created in parent and we don’t need to create those again in child classes
- By this way, we will be using common code with a parent while we will create only some specific methods and attributes for the child’s classes
Method Overriding and Method Overloading
METHOD OVERRIDING
- Method overriding is happening in child class (inheritance)
- You have a method in parent class but you are not satisfied with its implementation (body), and you would like to change.
- The method name, return type and arguments must be same
- And body should be different, otherwise it does not make sense to override
- THE PURPOSE OF OVERRIDING A METHOD: It is common to use when we want to specify body in the child classes method. So, we override the parent class’ methods in child
- @Override annotation is used to override a parent class’ method but it is optional
METHOD OVERLOADING
- It is happening in the same class and has nothing to do with inheritance
- It is having multiple methods in the same class and with the same name but changing either number or type of parameters
this keyword vs super keyword
- this keyword is a reference to current object
- super keyword is a reference to a current parent object
this() vs super()
- this() is used to chain constructors and call one constructor in another constructor of the same class
- super() is used to call super class constructor
- super() and this() must be on the first statement in the constructor if they are used to call overloaded constructors
Constructor Chaining
DEFINE
It is invoking one custom constructor in another
Inheritance
ACCESS MODIFIERS
- private members can NEVER be inherited (class only)
- public and protected members can ALWAYS be inherited
- default members can be inherited in the SAME PACKAGE only
Can a class extend to multiple classes
- No, a class can have only one parent (except Object)
- HOWEVER, one class can be parent to many other child classes