Oops Concepts In Java
By AmarSivas | | Updated : 2021-01-05 | Viewed : 8795 times

Table of Contents:
Object Oriented Programming
Java OOPS Concepts
To understand the Java in deeper it is highly recommendable to understand the
Class and Object
Class:
Class is a set of
package com.docsconsole.tutorials.apackage;
//Class for Employee which contains the Data i.e., Variables and Methods
public class Employee {
String name = "David Raj";
public String getName() {
return this.name;
}
public void changeName() {
this.name = "David John";
}
public static void main(String[] args) {
Employee employee = new Employee(); // Object is created
System.out.println("The Name of Employee : " + employee.name);
employee.changeName();// Method is used to change the name.
System.out.println("The Name of Employee after changing the name: " + employee.name);
}
}
Here JVM will create the Data in the form of Employee object to manipulate the data based on the syntax i.e. logic.
Object:
The
Field and Method
Fields are the basic unit of the Object in
The
package com.docsconsole.tutorials.bpackage;
//Class for Employee which contains the Data i.e., Fields (Variables) and Methods
public class Employee {
// Non-static Field (Variable)
String name = "David Raj";
// Static Field (Variable)
static int SALARY = 10000;
public String getName() {
return this.name;
}
public void changeName(String name) {
this.name = name;
}
public static void main(String[] args) {
System.out.println("The salaray is " + Employee.SALARY);// Salary is common for every employee so made it as Static field.
// Static filed can be called with Class name
Employee employee = new Employee(); // object is created
System.out.println("The Employee Name is: " + employee.name);// Non-Static filed should be called with object reference
System.out.println("Employee salary is: " + employee.SALARY);// Static filed can be called with object reference but it is not recommendable
Employee employee1 = new Employee(); // object is created
System.out.println("The Employee Name is: " + employee1.name);// Non-Static filed should be called with object reference
System.out.println("Employee salary is: " + employee1.SALARY);// Static filed can be called with object reference but it is not recommendable
employee.changeName("David John");
System.out.println("The Non-Static Field nameeis " + employee.name);// Name of employee object is changed.
System.out.println("The Non-Static Field nameeis " + employee1.name);// Name of employee1 object is not changed.
}
}
Here
Encapsulation
Encapsulation, one of the OOPS concept, talks about the
package com.docsconsole.tutorials.cpackage;
//Class for Employee which contains the Data i.e., Variables and Methods
public class Employee {
// Non-static Field
String name = "David Raj";
// Static Field
int salary = 10000;
// getter and setter methods for all Non-static Fields
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
}
Here other class Objects cannot be allowed to manipulate the data (fields) which are declared as private. It should be manipulated by the same class methods. So, the security of the class is achieved here.
Abstraction
The main advantage of the abstraction is to solve the issues related to the design. When design the system we need to write first the rules then we can go for implementation. So here to define the rules we should use the Abstraction.
Polymorphism
Static Polymorphism
The example of Static Polymorphism is Method Overloading and Operator Overloading. It is also called
Method Overloading: Methods in the class with same the name and different arguments are said to be overloaded.
Operator Overloading: The operator behaves differently as per context is called as that operator is overloaded.
Dynamic Polymorphism:
The example of Dynamic Polymorphism is Method Overriding. The decision making of the Method’s resolution is taken at Run time.
Method Overriding: Inherited method form parent class can be overridden in the child class. When the child class Method has a signature i.e., method name and arguments same as the parent class method then the parent class method is said to be overridden.
Inheritance
The basic concept of the
The main usage of the Inheritance is reuse of the program. Developer can use the
Inheritance can be categorized as
-
1. Single Inheritance
-
2. Multilevel Inheritance
-
3. Hierarchical Inheritance
-
4. Multiple Inheritance.