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
WHAT IS IS-A-RELATIONSHIP
WHAT IS HAS-A-RELATIONSHIP
WHAT IS IS-A-RELATIONSHIP?
-Inheritance
WHAT IS HAS-A-RELATIONSHIP?
Book - Author
Method overriding vs overloading
- Overriding is implementing a different body to the inherited method while overloading is having 2 or more methods with the same name but with different arguments
- Overloading always occurs in the same class while overriding always occurs in different classes (child class)
Abstraction
DEFINE
It is known as hiding the implementation and providing the functionality. Abstraction only makes sense when used with inheritance.
Abstraction
ABSTRACTION CLASS RULES
- Abstract classes cannot be instantiated
- It allows us to have constructors, these constructors will be inherited and used in the child classes
- Abstract class cannot be final
- Abstract classes can have both abstract methods and concrete methods
- Concrete methods = non-abstract method
- Abstract method = it is the method that has no body (implementation)
- Abstract methods must be overridden in all child classes
- abstract methods cannot be static, final or private
Abstraction
INTERFACES RULES
- Interfaces are used to define additional functions for child classes
- Interfaces allows multiple inheritance meaning one class can implement many interfaces. Also, one interface can extend to many other interfaces
- Interfaces cannot be instantiated, and they cannot have constructors
- It is designed to have abstract methods. However, later versions of Java allows to have default and static methods in the interfaces.
- Every member in the interface is always public by default
- Instance variables in an interface are always public static final by default
Abstraction
TYPES
- abstract classes
2. interfaces
Abstraction Class and Interface
SIMILARITIES
- Both are used to achieve abstraction
- Both cannot be instantiated
- Both can have abstract methods
- If a child extends from an abstract class or implements an interface, all abstract methods from these parents must be overridden immediately
Abstraction Class and Interface
DIFFERENCES
- one class can extend to one abstract class while it can implement many interfaces. Single inheritance vs multiple inheritance
- to extend from an abstract, “extends” keyword is used but to implement an interface or more, “implements” keyword is used
- abstract classes can have constructor, but interfaces cannot
- in the abstract class, you can have private, default, protected and public class members(methods, instance variables), but in the interfaces, you can have only public members.
Polymorphism
DEFINE
- Polymorphism is many shapes or many forms
- It is one object being able to take on many shapes
- One of the purposes of having polymorphism is to create polymorphic collections like array or lists
Polymorphism
TYPES
- STATIC POLYMORPHISM/ static binding / Compile time polymorphism -> OVERLOADING
- DYNAMIC POLYMORPHISM- dynamic binding - Runtime polymorphism -> OVERRIDING
Compiler vs Runtime
Static and Dynamic polymorphism
Static polymorphism -> Overloading class A{ method1(){ } method1(int i){ } } A a = new A(); a.method1(); a.method1(2);
2. Dynamic polymorphism -> Overriding class B{ method1(){ // B class method } } class C extends B{ @Override method1(){ // C class method } } B b1 = new C();
//Compiler will think that method1 in the class B will execute.
//Runtime will find out which method to execute exactly. Object being created is C and that is why it will execute the method overridden in the class C and the result will be “C class method”
Polymorphism
FYI
Car
Honda extends Car
Civic extends Honda
Civic c1 = new Civic(); Honda h1 = new Civic(); Car c11 = new Civic();
List list = new ArrayList<>();
- List is an interface and cannot be instantiated
- ArrayList is a class that we create object of it
ArrayList implements List
String is a String
String is an Object