The Main OOP Concepts In Java
I brought back a topic that is important to you. That is oop concepts in java. These oop concepts are very important and very interesting. So first let’s know what oop is.
The programming worldview where everything is addressed as an article is known as an object-oriented programming language.
Some popular object-oriented programming languages are Java, C#, Python, C++ etc.
This is based on objects. Objects are associated with real-world entities like table, pencil, laptop, flower etc. Oriented Programming is a system to plan a program utilizing classes and objects. There are several main concepts in JAVA.
• Class
• Object
• Inheritance
• Abstraction
• Polymorphism
• Encapsulation
Class
A class includes data members and methods that characterize each object of the class. A programmer may define a class using Java or may use predefined classes that come in class libraries. An object is an instance of a class.
Example :
class Person {
// Variables
// Methods
}
Object
Real-world consists of objects (Table, Computer, bicycle). It consists of two characteristics. There are state and behavior.
EX: Bicycles have state (current gear, speed) and behavior (changing gear, applying brakes).
Inheritance
Inheritance is consists of the word “Inherit”, which means “To Derive”. Defined as the tendency of one class to derive properties and characteristics from other classes. It provides additional functionalities to extract features from the base class and imply it into other derived classes significantly.
This concept helps to avoid duplication and reduce redundancy.
In Java, a subclass is defined using the keyword “extends”
Example:
Class A{}
Class B extends A{}
Abstraction
Data abstraction refers to the act of representing essential features without including the background details. It is concerned with separating the behavior of a data object from its re-presentation.
Also, abstract class is incomplete and missing methods body. In JAVA, use abstract keyword to identify the abstract classes.
Example:
abstract class shape{
abstract void area{}
}
Polymorphism
Polymorphism in OOP is the ability to treat an object of any subclass of a base class as if it were an object of the base class. A base class has, so, many forms: the base class itself, and any of its subclasses.
There are two kinds of polymorphism in JAVA:
Overloading: Two or more methods with different signatures
Overriding: Replacing an inherited method with another having the same signature
Example:
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println(“int i = “ + I);
}
static void myPrint(double d) { // same name, different parameters System.out.println(“double d = “ + d);
} }
Encapsulation
The process of binding data members and functions in a class is known as encapsulation. Data is not accessible to the outside world and only those functions which are declared in the class can access it.
So, we want to use set and get methods to access it.
Example:
public class Student{
private string name;
public String setName(){ //set method
this.name=name;
}
public String getName(){//get method
return name;
}}
I trust you all are understand with all the object oriented programming concepts that we have talked about above. It easy to create Java application more secure,reusable using OOPs concepts.