Feature of OOPs
Oops is a programming paradigm that provides a set of rules for designers and developers to follow while creating software, which makes it simpler to create and maintain software.
i) Object
ii) Class
iii) Data Abstraction and Encapsulation
iv) Inheritance
v) Polymorphism
i. Objects & Class
An object is a real-world entity with defined attributes and behaviors. It can represent a physical item or an abstract concept.
Example: Pencil
A class is a logical grouping of related objects that share common characteristics and functions, making them easier to manage.
Example: Multiple cars of the same type having similar functions
ii. Data Abstraction
Data abstraction hides the information with implementation and only shows essential services to the user. Abstractions are used in two ways in the class
- Abstract class
- Interface
iii. Data Encapsulation
Data encapsulation in Java is the process of wrapping data (variables) and code (methods) into a single unit (class) and restricting direct access to the data.
iv. Polymorphism
Oops is an acronym for "many forms" and polymorphism is one of those concepts. Compile-time polymorphism and run-time polymorphism are the two types of polymorphism used in Java.Polymorphism is possible in Java through overloading and overriding.
V. Inheritance
Inheritance is a very important feature of oops.It is an IS-A relationship, i.e., One class feature used by another class used for the “reusability” of code. Java does not support multiple and hybrid inheritance.
Code Example
class Student {
private int id; // private data
private String name;
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
}
class Test {
public static void main(String[] args) {
Student s = new Student();
s.setId(101);
s.setName("Ravi");
System.out.println(s.getId());
System.out.println(s.getName());
}
}
FAQs
Java is object-oriented because it supports OOP features such as:
1. Object & class
2. Encapsulation
3. Inheritance
4. Polymorphism
5. Abstraction
Java is robust because
1. It has strong memory management using Garbage Collector (gc()).
2. It supports exception handling.
3. It prevents memory corruption.
Java is simple because:
1. It has easy syntax.
2. It does away with complex features like pointers and operator overloading as compared to C++.
Java is high performance due to:
i. Just-In-Time (JIT) compiler
ii. Efficient memory management
Java is dynamic because it allows for dynamic memory allocation and runtime class loading.
RANREV INFOTECH