OOP - Principles Flashcards

1
Q

OOP PRINCIPLES:

A
  1. Encapsulation
  2. Inheritance
  3. Abstraction
  4. Polymorphism
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Encapsulation

DEFINE

A

Encapsulation is restricting direct access to make sure that “sensitive” data is hidden from users

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Encapsulation

HOW TO ACHIEVE ENCAPSULATION

A
  1. Private instance variables
  2. Public setters and getters methods

Note: These public getters and setters’ method will help us to access and update private instance variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Getter vs Setter

A

GETTER

  • is used to retrieve (read) data
  • is always return type

SETTER

  • is used to set (write) data
  • is mostly void type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Encapsulation

WHY DO WE NEED ENCAPSULATION

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

ADVANTAGES OF OOP PRINCIPLES

A
  • Store and manipulate data & provide safety
  • Reuse code - efficiency, saves us time and effort saves company money
  • Maintain application
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Inheritance

DEFINE

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Inheritance

HOW TO ACHIEVE INHERITANCE

A
  • 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Inheritance

WHY DO WE USE INHERITANCE?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Method Overriding and Method Overloading

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

this keyword vs super keyword

A
  • this keyword is a reference to current object

- super keyword is a reference to a current parent object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

this() vs super()

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Constructor Chaining

DEFINE

A

It is invoking one custom constructor in another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Inheritance

ACCESS MODIFIERS

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can a class extend to multiple classes

A
  • No, a class can have only one parent (except Object)

- HOWEVER, one class can be parent to many other child classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

WHAT IS IS-A-RELATIONSHIP

WHAT IS HAS-A-RELATIONSHIP

A

WHAT IS IS-A-RELATIONSHIP?
-Inheritance

WHAT IS HAS-A-RELATIONSHIP?
Book - Author

17
Q

Method overriding vs overloading

A
  • 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)
18
Q

Abstraction

DEFINE

A

It is known as hiding the implementation and providing the functionality. Abstraction only makes sense when used with inheritance.

19
Q

Abstraction

ABSTRACTION CLASS RULES

A
  • 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
20
Q

Abstraction

INTERFACES RULES

A
  • 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
21
Q

Abstraction

TYPES

A
  1. abstract classes

2. interfaces

22
Q

Abstraction Class and Interface

SIMILARITIES

A
  • 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
23
Q

Abstraction Class and Interface

DIFFERENCES

A
  • 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.
24
Q

Polymorphism

DEFINE

A
  • 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
25
Q

Polymorphism

TYPES

A
  1. STATIC POLYMORPHISM/ static binding / Compile time polymorphism -> OVERLOADING
  2. DYNAMIC POLYMORPHISM- dynamic binding - Runtime polymorphism -> OVERRIDING
26
Q

Compiler vs Runtime

Static and Dynamic polymorphism

A
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”

27
Q

Polymorphism

FYI

A

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