Q. What is OOPS Concepts?
A: Object-oriented programming System(OOPs) is a programming paradigm based on the concept of “objects” that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs.
Q. What is an Object?
A: Object: is a bundle of data and its behaviour(often known as methods).
Objects have two characteristics: They have states and behaviors.
Examples of states and behaviors
Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
class House {
String address;
String color;
double are;
void openDoor() {
//Write code here
}
void closeDoor() {
//Write code here
}
...
...
}
So if I had to write a class based on states and behaviours of House. I can do it like this: States can be represented as instance variables and behaviours as methods of the class.
Example 2:
Let’s take another example.
Object: Car
State: Color, Brand, Weight, Model
Behavior: Break, Accelerate, Slow Down, Gear change.
Object Oriented Programming features:
a. Abstraction
b. Encapsulation
c. Inheritance
d. Polymorphism
Abstraction: Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
Encapsulation:Encapsulation simply means binding object state(fields) and behavior(methods) together. If you are creating class, you are doing encapsulation.
Encapsulation example in Java
How to
1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class.
2) Have getter and setter methods in the class to set and get the values of the fields.
class EmployeeCount
{
private int numOfEmployees = 0;
public void setNoOfEmployees (int count)
{
numOfEmployees = count;
}
public double getNoOfEmployees ()
{
return numOfEmployees;
}
}
public class EncapsulationExample
{
public static void main(String args[])
{
EmployeeCount obj = new EmployeeCount ();
obj.setNoOfEmployees(5613);
System.out.println("No Of Employees: "+(int)obj.getNoOfEmployees());
}
}
Output:
No Of Employees: 5613
The class EncapsulationExample
that is using the Object of class EmployeeCount
will not able to get the NoOfEmployees directly. It has to use the setter and getter methods of the same class to set and get the value.
So what is the benefit of encapsulation in java programming
Well, at some point of time, if you want to change the implementation details of the class EmployeeCount, you can freely do so without affecting the classes that are using it.
Inheritance:The process by which one class acquires the properties and functionalities of another class is called inheritance. Inheritance provides the idea of reusability of code
Types of Inheritance:
Single Inheritance:refers to a child and parent class relationship where a class extends the another class.
Multilevel Inheritance:refers to a child and parent class relationship where a class extends the child class. For example class A extends class B and class B extends class C.
Hierarchical inheritance: refers to a child and parent class relationship where more than one classes extends the same class. For example, class B extends class A and class C extends class A.
Multiple Inheritance:refers to the concept of one class extending more than one classes, which means a child class has two parent classes. Java doesn’t support multiple inheritance. Java doesn’t allow multiple inheritance to avoid the ambiguity caused by it.
Polymorphism: polymorphism is a object oriented programming feature that allows us to perform a single action in different ways.
public abstract class Animal{
...
public abstract void animalSound();
}
public class Lion extends Animal{
...
@Override
public void animalSound(){
System.out.println("Roar");
}
}
and
public class Dog extends Animal{
...
@Override
public void animalSound(){
System.out.println("Woof");
}
}
Types of Polymorphism:
1. Method Overloading: Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different.
2. Method Overriding:
IS-A & HAS-A Relationships
A Car IS-A Vehicle and HAS-A License then the code would look like this:
public class Vehicle{ }
public class Car extends Vehicle{
private License myCarLicense;
}
Note:Generalization is the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. Shared characteristics can be attributes or methods.
Specialization means creating new subclasses from an existing class. If it turns out that certain attributes or methods only apply to some of the objects of the class, a subclass can be created.
Reference:https://beginnersbook.com/2013/04/oops-concepts/