You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -35,6 +35,7 @@ So, Objects from the same blueprint, or more formally the same class, share the
35
35
36
36
It’s enough for Objects and Classes, Let’s move on to other important concepts in OOP.
37
37
38
+
38
39
Abstraction
39
40
Abstraction means you start focusing on the common properties and behaviors of some Objects, and we automatically will discard what’s unimportant or irrelevant.
40
41
@@ -51,19 +52,20 @@ And As a way to discard what’s unimportant, we should focus on how the class s
51
52
52
53
Later on, maybe you will be having a specific car brand that has a property or a behavior that’s not common between other car brands, and it’s specific to it’s type. In this case, you will have to do of what’s called “Inheritance”, and we will get into it later.
53
54
55
+
54
56
Encapsulation
55
57
Instead of having a procedural, long program, we start to decompose our program into small reusable, manageable chunks. Encapsulation implies the idea of breaking down our program into small mini-programs; classes, where each class has a set of related attributes and behaviors.
56
58
57
59
Encapsulation also implies the idea of hiding the content of a Class, unless it’s necessary to expose. We need to restrict the access to our class as much as we can, so that we can change the properties and the behaviors only from inside the class.
58
60
59
-
60
61
Encapsulation
61
62
But, How can we do that?
62
63
Start building our blueprints, or classes and define their properties and behaviors. Then, restrict access to the inner works of that class by hiding the attributes and methods so that they’re only accessible from inside the class scope.
63
64
64
65
What’s the difference between Encapsulation and Abstraction?
65
66
Encapsulation is a strategy used as part of abstraction. When we encapsulate, we abstract away the implementation.
66
67
68
+
67
69
Abstraction is a more generic term, and it’s often not possible without hiding the object’s class content by encapsulation; if a class exposes its internal content, thus, it cannot be abstracted.
68
70
69
71
Why should we hide attributes and methods of an object?
@@ -84,6 +86,7 @@ Well, the rule is, as much as possible.
84
86
85
87
So, the idea of encapsulation is that you enclose your object’s attributes and methods, and then you hide everything about that object except what is absolutely necessary to expose.
86
88
89
+
87
90
Inheritance
88
91
Abstraction is, instead of creating different classes, we can instead create one generic class that has the common, and essential properties and behavior of those classes, while Inheritance is inheriting these common properties and behaviors, so that we can create a new class, but instead of writing it from scratch, we can base it on an existing class.
89
92
@@ -104,6 +107,16 @@ So, a Customer IS-A Person, and an Employee IS-A Person, and so on. The term tha
104
107
Multiple Inheritance
105
108
In C++, & Python allow you to inherit from more than one Super Class; Multiple Inheritance. But, it can get confusing as it’s much more common to inherit from one Super class. In Java & C#, You only inherit from one Super Class.
106
109
110
+
There are five types of Inheritance-
111
+
1. Single Inheritance
112
+
2. Multiple Inheritance
113
+
3. Multilevel Inheritance
114
+
4. Hybrid Inheritance
115
+
5. Hierarchical Inheritance.
116
+
117
+
Multiple and Hybrid Inheriance is not directly applied in Java however these can be applied by using interfaces.
118
+
119
+
107
120
Polymorphism
108
121
Polymorphism; is the state where an object can take the shape of many different forms, and lets us do the right thing at the right time.
109
122
@@ -118,4 +131,13 @@ It’s worth mentioning that the speak method in the Animal class will be trigge
118
131
Polymorphism is the flexibility, that triggers the correct behavior.
119
132
120
133
Putting All Together — Animals Project
134
+
135
+
There are two types of polymorphism -
136
+
1. Compile-time polymorphism
137
+
2. Run-time polymorphism
138
+
139
+
Compile-time polymorphism includes method or function overloading and operator overloading but, operator overloading is not applicable in Java. It is also known as Static binding, Early binding and overloading as well.
140
+
141
+
Run-time polymorphism includes method overriding. It is also known as Dynamic binding, Late binding and overriding as well.
142
+
121
143
I’ve introduced the concepts of OOP. Here is a complete snippet written in Java that shows a real example combining all of the OOP concepts together.
0 commit comments