Date: 2 Mar 2022
OOP - Object Oriented Programming
Is a programming paradigm where everything is represented as an object.
Fundamentals
Objects
An object is an entity that has states and behaviors.
-
State tells us how the object looks or what properties it has.
-
Behavior tells us what the object does.
Classes
A class is a template or blueprint from which objects are created.
Imagine a class as cookie-cutter and objects as cookies.
Principes of object-oriented programming
- Encapsulation
- Inheritance
- Abstraction
- Polymorphism
Encapsulation
Is a process of wrapping code and data together into a single unit.
Imagine a capsule with mixture of several meds. This can be achieved using private
access modifiers that can’t be accessed by anything outside the class. And use getter
and setter
to access that value.
(In Java, these methods should follow JavaBeans naming standards.)
- Advantage
- We can make a class
read-only
orwrite-only
. - Control over the data. (Logic + Setter = Control)
- Data hiding. Other classes can’t access private members of a class directly.
- We can make a class
Inheritance
Is a mechanism in which one object acquires all the states and behaviors of a parent object.
- Uses parent-child relationship (IS-A relationship).
** A rule of thumb in java: make instance variables private
and instance methods public
.
public instance methods and private instance variables (using getters and setters) get inherited.
Types of Inheritance in Java
- Single
- Multilevel
- Hierarchical
- Multiple
- Hybrid
- Class allows single, multilevel and hierarchical inheritances.
- Interface allows multiple and hybrid inheritances.
Note: A class can extend only one class however it can implement any number of interfaces. An interface can extend more than one interfaces.
Relationships
- IS-A relationship Refers to inheritance or implementation
- Generalization Uses an IS-A relationship from a specialization class to generalization class.
- HAS-A relationship An instance of one class HAS-A reference to an instance of another class.
-
Aggregation In this relationship, the existence of class A and B are not dependent on each other.
-
Composition In this relationship, class B can not exist without clas A - but class A can exist without class B.
-
Advantage of inheritance
- Code resuse.
- You have more flexibility to change code.
- You can use polymorphism: method overriding requires IS-A relationship.
Abstraction
Is a process of hiding the implementation details and showing only functionality to the user.
In Java, we can achieve abstraction in two ways:
abstract
class (0 to 100%)interface
(100%)
The keyword abstract
can be applied to classes and methods. abstract
and final
or static
can never be together.
1. Abstract class
Can’t be instantiated (can’t create objects of abstract classes).
They can have :
- constrcutors
- static methods
- final methods
2. Abstract methods
Does;t have implementation (no method body and ends up with a semi colon). It shouldn’t be marked as private
.
3. Abstract class and Abstract methods
- If at least one abstract method exists inside a class then the whole class should be abstract.
- We can have abstract class with no abstract methods.
- We can have any number of abstract as well as non-abstract methods inside an abstract class at the same time.
- The first concrete sub class of an abstract class must provide implementation to all abstract methods.
- If this doesn’t happen, then the sub class also should be marked as abstract.
When do we want to mark a class as abstract?
- To force sub classes to implement abstract methods.
- To solve having actual objects of that class.
- To keep having a class reference.
- To retain common class code.
Interface
Is a blueprint of a class. An interface is 100% abstract. No constructors are allowed here. It represents an IS-A relationship
NOTE Interfaces only define required methods. We can not retain common code.
- An interface can have
only abstract methods
, not concrete methods. By default, interface methods arepublic
andabstract
. So inside the interface, we don’t need to specifypublic
andabstract
.
So when a class implements an interface’s method without specifying the access level of that method, the compiler will throw an error stating Cannot reduce the visibility of the inherited method from interface". So that implmented method's access level must be set to
public`.
NOTE By default, interface variables are public
, static
and final
.