OOPs Concept in java programming
OOP :- OOP refers as Object Oriented Programming. OOP is a concept that is based on objects. As a name suggests it is Object oriented. Java is Object oriented programming language.
There are mainly four concepts in java :-
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Let's discuss all of above one by one.
1. Abstraction :-
In simple term, Abstraction means hiding complexity and showing essential details to the user. Means using the things without knowing background details about it.
Abstraction is the technique of hiding implementation.
Like, we use smartphone very efficiently and easy way, we don't need background details how it works. we simply use.
Why should we use Abstraction?
- Reduce complexity
- Force users to extend implementation rather than modify
- Support cross platform by changing the implementation per platform
In Java, we can use abstraction in two ways :-
- Using Abstract class
- Using Interface
2. Encapsulation
Encapsulation means wraps variables or functions into single unit. means we can create private fields in class so any other class can not access its data.
In encapsulation, data in a class is hidden for another classes, so it is also known as data-hiding.
For use private variables we can use public getter and setter method in that class. so we can access private variables.
Example of encapsulation
Why we should use Getter and Setter methods?
This question confuse every new programmer who learn Encapsulation in oops.
Why we should use getter and setter methods if we can directly access and use variable using defining public? Why we use getters and setters?
So, simple answer is we set limit in variable access using getter and setter methods. Like if anyone wants to get or set above name variable then there is only one option using getName() and setName() method.
Another main use of getter and setter, that we can put any validation in these methods. like in above example if we want to set name that is not have greater than 10 length. so we can easily add these type of validation in above method. so, anyone who want to set name then they must have to set name in less than 10 length.
3. Inheritance
Inheritance means use the properties of one class to another class. Inheritance provides ides of reusability of code.
extends keyword is used for access base or parent class properties in child or sub class.
Subclass defines own unique properties as well as extends another class ( parent class or base class ) and uses its properties. this is done by inheritance.
There is two class Student and Teacher that have common properties like name, DOB, department, etc. Student have some unique properties like student id, college fee, scholarship, etc. and Teacher have salary, teacher id, etc. so we can define another class that have common properties and extends that class into Student and Teacher class.
So, lets see simple example of inheritance.
public class OOPLanguage {
public void OOP() {
System.out.println("Object Oriented Programming");
}
}
public class Java extends OOPLanguage {
public static void main(String[] args) {
Java javaObj = new Java();
javaObj.OOP();
}
}
Output :-
Object Oriented Programming
4. Polymorphism
Polymorphism means there is two or more method whose name are same but its parameters are different.
Polymorphism means perform more than one action using single unit.
In java, we can perform polymorphism in two ways.
- Compile time polymorphism or static binding
- Run time polymorphism or dynamic binding
1. Compile time polymorphism
It is also knows as Method Overloading in java. Method overloading means there is same method name but different parameters in same class.
In compile time polymorphism, which method is call decides at compile time.
public class ClassOne{
public void print() {
System.out.println("Class one");
}
public void print(String str) {
System.out.println("Class one" + str);
}
}
2. Run time polymorphism
It is also known as Method Overriding in java. Method overriding means same method name and different parameters in same class.
public class ClassOne{
public void print() {
System.out.println("Class one");
}
}
public class ClassTwo {
public void print() {
System.out.println("Class two");
}
}
In run time polymorphism, which method is call decide at run time.
Other articles you may like :-
Comments
Post a Comment