×
DocsConsole

DocsConsole

OOPS Concepts in Java

By | | Updated : 2019-11-06 | Viewed : 263 times

OOPS-Concepts-in-Java

Object Oriented Programming talks about Real-World Objects where Classes and Objects designed with the help of OOPs. Here The current article focused on all OOPs Concepts with good examples which made us easy to understand. we will learn about all OOPs concepts and Features which are known as basic pillars of Object Oriented Programming Language which helps us to design the well-defined System.

Object Oriented Programming

Object Oriented Programming is one type of methodology to implement a System in which the code talks about the Real World Objects like Employee, Student, University. Java is also one of the Object Oriented Programming Languages which is built upon the OOPs. In Object Oriented Programming Language every Class implemented in Java represents a Real World Object. So, it helps us in understanding to maintain and develop the system. It has come with so many Features/Concepts which can be used in the implementation of Real World Objects.

Java OOPS Concepts

To understand the Java in deeper it is highly recommendable to understand the OOPs concepts. In other words, the learning of OOPs is learning about how java works internally. By using OOPs concepts, the program can well be defined with Classes and Objects. These Objects are manipulated by the Class where the OOPs concepts come into the picture. We will learn one by one here with examples.

Class and Object

Class and Object are Main concepts of the Object Oriented Programming Languages. Once you understand these concepts you can easily write programmes to solve Real-time problems.

Class:

Class is a set of Syntax Statements/Instructions which are represented Data and Methods to work on the data. So, developer needs to write a set of syntax statements that will be executed by JVM in runtime. JVM will consider the Syntax Statements/Instructions for execution and perform data operations accordingly.

Employee.java
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 Object, i.e., Instance, is one of the basic concepts in OOPs that will contain the data in the forms of Fields and Methods. In other words, Object has wrapped with data (i.e., fields) and methods. Methods called by the object, play the main role in accessing the data to manipulate. Every Object has State and Behaviour. Object’s State is described with fields. And, Object’s behavior can be defined by method.

Field and Method

Fields are the basic unit of the Object in Object Oriented Programming. Fields can be Static or Non-Static. Non-Static Fields is created as part of Object Creation whereas Static Fields are instantiated at time of the Class Loading.

The Non-Static fields are created as a separate copy of each object at the time of Object creation. Unlike Non-Static fields, Static fields will be created as a unique and single copy at the time of class Loading. Here Static fields will be created as global access and common for all objects. Access Modifiers will be applicable to each field of the Object.

Non-static fields and Static fields example: Employee.java
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 name is a Non-static field which will be created a separate copy of each object. But the Static field name will be created only once at the time of class loading means object reference is not required to call the Static field. We can call Static field with an object reference. But the recommended one is calling with Class Name.

Encapsulation

Encapsulation, one of the OOPS concept, talks about the Data Hiding / Security. Encapsulation can be defined as wrapping up data with Methods. The Class is a wrapper for Data (Fields) and Methods. Class is used to provide the Encapsulation by setting the fields as private and methods as public. So, Fields of the class can be modified by the same class methods only.

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

Abstraction can be defined as showing the Object functionality with hiding internal details. To provide security object internal details should be hidden to the end-user. So, he can only see the object’s functionality. For example, in the case of a driving car, the user should have to know only about driving the car. He does not need to know how internally the car is working. In Java Abstraction can be achieved by the Interface and Abstract classes.

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

Polymorphism is the ability to create Methods and Operators that have more than one form. Polymorphism is nothing but performing the action in different forms. Polymorphism in java distinguished as two types. One is Static Polymorphism and another one is Dynamic Polymorphism. Generally, Object Oriented Programming polymorphism is applicable to methods and operators.

Static Polymorphism

The example of Static Polymorphism is Method Overloading and Operator Overloading. It is also called Compile Time Polymorphism/Binding where the resolution of the Method/operator decided at Compile time.

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 Object Oriented Programming is inheritance which talks about the acquiring properties of the parent class. Inheritance lets Child Class allow acquiring the properties of the parent class. Inheriting the Parent class properties into child class is called Inheritance.

The main usage of the Inheritance is reuse of the program. Developer can use the extends keyword for classes and implements keyword for interface. So new classes to be implement will be implemented on the existing the class.

    Inheritance can be categorized as

  • 1. Single Inheritance

  • 2. Multilevel Inheritance

  • 3. Hierarchical Inheritance

  • 4. Multiple Inheritance.

Leave A Reply