Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

OOP

- Unified Modeling Language (UML):


- It is not a programming language, it is rather a visual language.
- It consists of diagrams used to visualize the design of the system.
- One of these diagrams is the class diagram.
- A class contains members, and these members are instance variables/member variables and member
methods.

Section: OOP Concepts


- The main OOP concepts are:
- Encapsulation. - Abstraction
- Inheritance -Polymorphism

Encapsulation
- What is it?
- Encapsulation is the process of wrapping the data (variables) and code acting on that data
(methods) together as a single unit.
- Purpose:
- Data Hiding.
- Gain control over the data.
- How do we achieve it?
To achieve encapsulation:
- Use classes
- Make these classes fully encapsulated by:
- Declaring the variables of the class as private.
- Add getter and setter methods for each variable as public.
- Put the concept into a code form:
public class className {
private String name;
private int age;

public String getName() {


return name;
}
public int getAge() {
return age;
}
public void setName(String newName) {
name = newName;
}
public void setAge( int newAge) {
age = newAge;
}
}

Abstraction
- What is it?
- Abstraction is the process of hiding the implementation details, irrelevant information, and
showing only the functionality to the user of the class.
- Explanation: bear in mind that the real-life scenario is not knowing about the implementation
classes while using the abstract class as we make objects from these classes using factory
methods, so by using abstract classes and interfaces, we are hiding irrelevant information to the
user of this class.
- Purpose:
- Reduce programming complexity and effort.
- How do we achieve it?
To achieve abstraction:
- Use abstract class (0 to 100% abstract methods) “un-instantiable class”.
- Use interface (100% abstract methods).
- Note that abstract method means it has no body
- Put the concept into a code form:
public abstract class className {
private String name;
private int age;

public String getName() {


return name;
}
public int getAge() {
return age;
}
public void setName(String newName) {
name = newName;
}
public void setAge( int newAge) {
age = newAge;
}
public abstract void run();
}

public interface interfaceName {


// define the methods only- no public keyword (public by default).
String getName();
int getAge();
void setName(String newName);
void setAge( int newAge);
}

Inheritance
- What is it?
- Inheritance is
- Purpose:
-
- How do we achieve it?
- Put the concept into a code form:

Polymorphism
- Polymorphism applies to objects rather than to classes.
- What is it?
General Definition:
- Polymorphism means the ability of an entity to take many forms.
Types of polymorphism:
- Run-time polymorphism: refers to polymorphic references (it can hold many forms of
objects), which allows an object of a derived class to be used whenever an object of a base class
is required. That is, the base class acts as an interface for the derived class.
(from this definition, we see that an object of the base class type can take different forms of the
derived-class objects).
- To achieve runtime polymorphism: use method overriding and virtual keyword.
- Compile-time polymorphism: refers to:
- polymorphic methods and operators (there can be more
than one form of a method or operator in the same class).
- parametric classes (allow us to have many forms of a
particular class).
Note:
Compile-time or Run-time refers to the time when one of the forms is chosen.

- Purpose:
- Flexibility, i.e., extendibility which means an object or class can have its uses extended.
- How do we achieve it?
- To achieve runtime polymorphism: use method overriding and virtual keyword.
- To achieve compile-time polymorphism: use method overloading and operator overloading.
- Put the concept into a code form:
OOPs Miscellaneous:
Method Overriding Method Overloading

It is the redefinition of a base class function in its It means providing multiple definitions of the
derived class with the same signature. function in the same class by changing the signature.

Purpose: providing specific implementation. Purpose: increase the readability of the program.

Why do we need a virtual method?


- By default, early binding “aka static binding” happens (the target method is found at compile time based
on the type of the reference variable).
- So we can’t “override a method” and “make the reference to the object take different forms of objects”
(i.e, no benefits from reference polymorphism).
- To solve the problem, we use virtual methods, so late binding “aka dynamic binding” happens (the target
method is found at run time based on the type of the object).

You might also like