What are OOPS Concepts in Java?

We used to hear OOPS very often whenever we come across JAVA language. In this post we are going to have a look at the OOPS concepts of the Java. Some of the object oriented languages are: C#, C++, PHP, Java, Python etc.

Main concepts of OOPS are:
  • Inheritance
  • Polymorphism
  • Abstraction
  • Encapsulation
1. Object: 
A Java object is a combination of data and procedures working on the available data. An object has a state and behavior. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created from templates known as classes. In Java, an object is created using the keyword "new".

Syntax of creating an object:

MyClass obj = new MyClass();

In above example, 
  • MyClass is class name, 
  • 'obj' is the object name (object name can be anything) 
  • 'new' is keyword of java which instantiates the object (or in simple language it creates the object
  • 'MyClass()'is the constructor of 'MyClass'

2. Class: 
Before create objects in Java, you need to define a class. A class is a blueprint for the object. We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.
Since, many houses can be made from the same description, we can create many objects from a class.
Syntax to define a class:
class MyClass {
   // variables
   // methods
A class in java can contain:
  • Fields
  • Methods
  • Constructors
  • Blocks
  • Nested class and interface

3. Inheritance: 
If an object acquires all the properties of the parent class, it is known as inheritance. It provides code reusability. It is used to achieve runtime polymorphism. For detailed summary see this post: What is Inheritance in Java?

4. Polymorphism: 
Polymorphism in Java is a concept by which we can perform a single action in different ways. We can perform polymorphism in java by method overloading and method overriding. 

5. Abstraction: Abstraction in Java is to hide the implementation details from the user and providing the functionality. It can be achieved by Abstract classes or Interfaces.

6. Encapsulation: Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. For detailed summary see this post: What is Encapsulation in Java?

Click here to find complete Java Tutorials Series

Java Control Statements   << Previous     ||     Next >>   Java Inheritance

Follow Us

YouTube Channel | Facebook Page | Telegram Channel | Quora Space
Feel free to ask queries or share your thoughts in comments or email us.


Popular posts from this blog

10+ Best Demo Websites for Selenium Automation Testing Practice

Automate Amazon like E-Commerce Website with Selenium WebDriver

Handle Static and Dynamic Web Table in Selenium WebDriver

How to Automate Google Search with Selenium WebDriver

Top 7 Web Development Trends in 2022

25+ Most Important Selenium WebDriver Commands Tutorial

50+ Most Important Selenium WebDriver Interview Questions and Answers

How to Find Broken Links on Webpage with Selenium WebDriver