OOP???
object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts:
- Inheritance
- Polymorphism →(types →Compile Time,Run Time)
- Abstraction
- Encapsulation
Inheritance
When one object acquires all the properties and behaviors of a parent object, it is known as inheritance. It provides code re-usability. It is used to achieve run time polymorphism.
and it can achieved by using key word — ->(extended ) and and final class can’t be inherited
e.g
class A{
////code A
}
class B extends A{
//copy of code A and some extra code of B
}
Polymorphism
If one task is performed in different ways, it is known as polymorphism. For example: to convince the customer differently, to draw something, for example, shape, triangle, rectangle, etc.
In Java, we use method overloading and method overriding to achieve polymorphism.
- Compile Time Polymorphism (Method Overloading)
Method Overloading in which method(subprogram,subroutine) have same name and different parameters.
e.g
int sumOftwo(int i,int j )
{
}
int sumOfthree(int i,int j,int k)
{
}
Compile Time polymorphism can be achieved in same class..
2. Run Time Polymorphism (Method Overriding)
In Method Overriding is achieved when class must be inherited and their Method prototype must be free from Final keyword if method is with final keyword method can never be override.
and method overridden and overriding have same name and same parameters in classes
class A{
public void hello()
{
S.O.P(“ HELLO”);
}
}
class B{
public void hello()
{
S.O.P(“ HELLO WORLD”);
}
}
Abstraction
Hiding internal details and showing functionality is known as abstraction. For example phone call, we don’t know the internal processing.
In Java, we use abstract class and interface to achieve abstraction.
Encapsulation
Binding (or wrapping) code and data together into a single unit are known as encapsulation. For example, a capsule, it is wrapped with different medicines.
and it can be acheived by using access modifiers;
class Account
{
private int acc_no;
private int password;
//code…………
}
