In this page, we will learn about the basics of Object Oriented Programming Concepts.Object oriented programming system is programming pattern based on object that contains metadata. The primary purpose of object-oriented programming language is to increase reusability, flexibility and maintainability of programs.
The main purpose of object-oriented programming language is to implement real world entities by using inheritance, encapsulation, abstraction and polymorphism.
What is Object?
Object is instance of a class having state and behavior.
Example: –
A car is an object because it has states like color, model, brand etc. as well as behavior like accelerate,break,gear change etc.
What is class?
In Object oriented programming concepts class is a collection of method and objects. Class is blueprint of individual objects are created.
Example:-
We can take human being is a class
There are four Pillars of Object Oriented Programming Concepts:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Abstraction:-
This is most important feature of object oriented programming concepts.Abstraction is process of hiding internal details and showing functionality. Java abstraction is achieved through abstract classes and interfaces. This will allow us to manage complexity.
Example:-
package com.javatechexpert;
abstract class Animal {
abstract void animalSound();
}
class Dog extends Animal {
public void animalSound() {
System.out.println("Bark Bark.");
}
}
class Cat extends Animal {
public void animalSound() {
System.out.println("Meows Meows ");
}
}
public class Abstraction {
public static void main(String[] args) {
Dog d1 = new Dog();
d1.animalSound();
Cat c1 = new Cat();
c1.animalSound();
}
}
Output:-
Bark Bark.
Meows Meows
Encapsulation:-
This is most important feature of object oriented programming concepts.Binding code and data together into single unit is known as encapsulation. Encapsulation is used to hide sensitive data that should not exposed to user.
How to achieve encapsulation?
- Provide getters and setter method to access and update the value of private variable.
- Declare class variable as private.
- Encapsulation can be achieved by using access modifier such as public ,private, protect and default.
Example:-
package com.javatechexpert;
public class Student{
private int studentId;
private String studentName;
private String department;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public static void main(String args[])
{
Student s1=new Student();
s1.setStudentId(1);
s1.setStudentName("Raj");
s1.setDepartment("Science");
System.out.println("Student Id: "+s1.getStudentId());
System.out.println("Student Name: "+s1.getStudentName());
System.out.println("Student Department: "+s1.getDepartment());
}
}
Output:-
Student Id: 1
Student Name: Raj
Student Department: Science
Inheritance:-
This is most important feature of object oriented programming concepts.Inheritance in java is one of the core concept of object programming language. When one object acquires all properties of parent object known as inheritance. Every class in java implicitly extends java.lang.object class.Object class is a top level of inheritance hierarchy in java.
As we know, a child inherits the properties from his parents. A similar concept is followed in Java, where we have two classes:
1. Parent class (Super or Base class)
2. Child class (Subclass or Derived class)
Example :-
package com.javatechexpert;
class Employee {
String designation = "Developer";
String companyName = "javatechexpert";
void does(){
System.out.println("working");
}
}
public class Company extends Employee{
String account = "banking";
public static void main(String args[]){
Company obj = new Company();
System.out.println(obj.companyName);
System.out.println(obj.designation);
System.out.println(obj.account);
obj.does();
}
}
Output:-
$javac Company.java $java -Xmx128M -Xms16M Company javatechexpert Developer banking working
Polymorphism:-
This is most important feature in OOPS.If same method performs many task is called as Polymorphism or in other words If something exist in many forms is called as Polymorphism.
There are two type of polymorphism
- Runtime Polymorphism(also known as Late Binding)
When type of object is determined at run time is known dynamic polymorphism.
Example:-
package com.javatechexpert;
class Car{
void run(){System.out.println("running.....");}
}
public class Audi extends Car{
void run(){System.out.println("running safely with 120km...");}
public static void main(String args[]){
Car c = new Audi();//upcasting
c.run();
}
}
Output:-
$javac Audi.java $java -Xmx128M -Xms16M Audi running safely with 120km
Compile time Polymorphism(also known as Early Binding):-
When type of object is determined at compile time is called static polymorphism.
package com.javatechexpert;
class Animal{
void eat(){System.out.println("animal is eating.");}
}
public class Cow extends Animal{
void eat(){System.out.println("Cow is eating.");}
public static void main(String args[]){
Animal a=new Cow();
a.eat();
}
}
Output:-
$javac Cow.java $java -Xmx128M -Xms16M Cow Cow is eating.