Introduction to Object-Oriented Programming in Java
Java is an Object-Oriented Programming which was designed by James Gosling. It is a general-purpose programming language that is class-based and having concurrent programming features. It has multi-threading features too. It is static, safe and strongly typed programming language. It was developed and is maintained by Oracle Corporation (then Sun Corporation). Its’ file extension names are .java or .class. It first appeared in the year 1995. It is intended to develop applications which can be Written Once and Run Anywhere. It is most popular for the client-server kind of applications. It is licensed under GNU General Public License and Java Community Process. The latest version of Java is 10 which is released in March 2018.
Explanation of Object-Oriented Programming in Java
The Java Programming Language is based on Object-Oriented Programming Methodology or Paradigm that has different kinds of concepts such as Classes, Objects, Inheritance, Polymorphism, Encapsulation, and Abstraction which can be described as below:
Class: This is a blueprint of the object which defines the fields or attributes and methods where the real functionality lies within. These attributes and methods are called members and these members can be accessed based on the defined access modifiers during the declaration of members.
Object: An object is called as an instance of the Class which can be declared and instantiated by calling the Constructor of the Class. An object will have the state and the state will contain data which will be held by the attributes of the class.
Inheritance: This is the third step process in which the data will be inspected, cleaned, transformed and visualized by reducing useless information and transforming into important sets of information in order to obtain some valuable information out of the existing data.
Polymorphism: Polymorphism is defined as the process of performing a single task in different possible ways. In Java, Polymorphism can be achieved in two ways called method overloading and method overriding. Method overloading is also called Compile Time Polymorphism whereas Method Overriding is also called Run Time Polymorphism.
4.8 (7,996 ratings)
View Course
Encapsulation: This is the process of encapsulating which means hiding or binding or wrapping the code into a single unit or module which is defined as Class in Java. The encapsulation feature of object oriented programming can be achieved by using a class in Java. A plain old java object or a Java Bean is said to be encapsulated as the members of the class are private (access modifier) those which can be accessed only by using getters and setters methods in the class.
Abstraction: The object-oriented feature abstraction can be defined as the process of hiding the implementation of the functionalities by exposing only the required interfaces or accessing methods in order to invoke the methods of the Implementation class. The abstraction can be achieved in Java programming language by using Interface and Abstract Class.
The advantages of using Object Oriented Programming in Java
- It helps in developing the different types of application and their maintenance easily without extra costs.
- It helps in implementing the changes easily by making small changes to the design and thereby making the application more adaptable to the larger changes required by the customer.
- The modularity in the code helps in easy troubleshooting process and maintenance by fixing the bugs easily.
- Code reuse is the main
- It provides greater flexibility towards frequent functionality changes.
Applications of Object-Oriented Programming in Java
There are different applications of Object-Oriented Programming in Java and below are the examples in this conceptual area:
- Class: A class can be defined as below:
public class Employee {
private int employeeId;
private String employeeName;
public int getSalary(int basicPay, int da, int hra) {
int salary = basicPay + da + hra;
return salary;
}
}
In the above class employeeId, employee name and getSalary() method are the members of the class whereas employeeId and employee name are the attributes or fields and getSalary() is the method where real work gets done.
- Object: An object can be created as below for the above class Employee.
Employee employeeObject = new Employee();
In the above line, an object is created by using new keyword and Employee() is the empty arguments constructor that is used to create the object. the employee objects the reference made to the class Employee.
- Polymorphism: This can be achieved by method overriding and overloading.
public int getSalary(int basicPay, int da, int hra) {
int salary = basicPay + da + hra;
return salary;
}
In the above method another argument can be added to the method getSalary() by adding into the parenthesis as below:
public int getSalary(int basicPay, int da, int hra, int bonus) {
int salary = basicPay + da + hra + bonus;
return salary;
}
- Encapsulation: This can be achieved as below:
public class Employee {
private int employeeId;
private String employeeName;
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}
The above class Employee has two fields (private) and four methods (getters and setters) which will be used to access the above two private attributes.
- Abstraction: This is the process of hiding the implementation functionality.
In the above method getSalary(), the internal function of the addition of all the components of a salary is hidden inside the method and only this can be accessed by using the method name by passing the values as method arguments. In this way, the total salary will be obtained by passing the individual salary components to the method.
Conclusion – Object-Oriented Programming in Java
There are different and multiples areas of applications in the field of Web world, Standalone and many other areas for the Object-Oriented Programming in Java concept. The average utilization or application of object-oriented programming in Java has been in the top 5 positions for most of the enterprise applications and has been in almost every enterprise as of now is the most sought-after technology. There are huge numbers of tools available such as IDEs to develop applications using object-oriented programming in Java and a lot of companies that are using Java-based applications for their requirements because of the ease of development and maintenance. The standalone apps developed in Java are mostly being used by many companies for their in-house tools and are developed based on Java Swing GUI toolkit and now called as Java FX in its recent version. The recent version of Java 8 provides great functional programming features and parallel processing capabilities with its Stream API.
Recommended Articles:
This has been a guide to Object-Oriented Programming in Java. Here we have discussed the Different concepts and the applications of Object-Oriented Programming in Java. You may also look at the following article to learn more –