Java OOPs Concepts
What is OOPs ?
Object-Oriented Programming is a paradigm that provides many concepts, such as
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
- Coupling
- Association
- Aggregation
- Composition
Object :
Any entity that has state and behavior is known as an object.
An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An Object contains both the data and the function, which operates on the data. For example - Human, Animal, Pen, Table, Car, etc.
Class :
The class is a group of similar entities. It is only an logical component and not the physical entity. For example, if you had a class called “Expensive Cars” it could have objects like Mercedes, BMW, Toyota, etc
Inheritance:
Inheritance is the concept of using the features of parent(inherited by child) class by the child(that inherits the parent) class .Inheritance is use for re-usability of code.
We use extend keyword in java to apply inheritance.
Syntax:
class derived-class extends base-class
{
//methods and fields
}
For Example :
class Mammal
{
}
// class Cow inherits the features of Mammal calss
class Cow extends Mammal
{
}
Polymorphism:
With the concept of Polymorphism we can use the same set of code in different aspects.
Polymorphism in Java is mainly of 2 types :
- Overloading
- Overriding
One form of polymorphism in Java is method overloading. When different methods have same name but diff signature.
The other form is method overriding. That’s when the different method have same name and signature in parent class and child class.
For example:
// Java program to demonstrate Polymorphism
public class Sum {
// Overloaded sum().
// This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
// Overloaded sum().
// This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
// Overloaded sum().
// This sum takes two double parameters
public double sum(double x, double y)
{
return (x + y);
}
// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
Output:
30
60
31.0
Abstraction:
Data Abstraction is the property by virtue of which only the essential details are displayed to the user.The The basic outcome of abstraction is to show "what to do", "how to do" is hidden. The trivial or the non-essentials units are not displayed to the user.
The abstract keyword is a non-access modifier, used for classes and methods:
- Abstract class: is a restricted class that cannot be used to create objects (to access it, it must be inherited from another class).
- Abstract method: can only be used in an abstract class, and it does not have a body. The body is provided by the subclass (inherited from).
An abstract class can have both abstract and regular methods.
To access the abstract class, it must be inherited from another class.
For Example :
abstract class Animal { // Abstract method (does not have a body)
public abstract void animalSound();
public void sleep() { // Regular method
System.out.println("Zzz");
}
}
// Subclass (inherit from Animal)
class Pig extends Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println("The pig says: wee wee");
}
}
class MyMainClass {
public static void main(String[] args) {
Pig myPig = new Pig(); // Create a Pig object
myPig.animalSound();
myPig.sleep();
}
}
Output:
The pig says: wee wee
Zzz
Encapsulation:
Encapsulation is an OOPs technique of wrapping the data and code. In this OOPs concept, the variables of a class are always hidden from other classes.
It can only be accessed using the methods of their current class.
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
- declare class variables/attributes as private
- provide public get and set methods to access and update the value of a private variable
For example :
class EncapsulationDemo{
private int ssn;
private String empName;
private int empAge;
//Getter and Setter methods
public int getEmpSSN(){
return ssn;
}
public String getEmpName(){
return empName;
}
public int getEmpAge(){
return empAge;
}
public void setEmpAge(int newValue){
empAge = newValue;
}
public void setEmpName(String newValue){
empName = newValue;
}
public void setEmpSSN(int newValue){
ssn = newValue;
}
}
public class EncapsTest{
public static void main(String args[]){
EncapsulationDemo obj = new EncapsulationDemo();
obj.setEmpName("Mario");
obj.setEmpAge(32);
obj.setEmpSSN(112233);
System.out.println("Employee Name: " + obj.getEmpName());
System.out.println("Employee SSN: " + obj.getEmpSSN());
System.out.println("Employee Age: " + obj.getEmpAge());
}
}
Output:
Employee Name: Mario
Employee SSN: 112233
Employee Age: 32
Coupling :
Coupling refers to the knowledge or information or dependency of another class.
In Java, we use private, protected, and public modifiers to display the visibility level of a class, method, and field.
Tight Coupling: It is when a group of classes are highly dependent on one another.
Loose Coupling: When an object gets the object to be used from external sources, we call it loose coupling.
Comments
Post a Comment