OOPs in Java | What are the oops concepts in Java?


What is OOPs Concept in Java?

OOPs stands for Object Oriented Programming. It is a programming paradigm that is based on the concept of objects, which have properties and behaviors. 

OOPs in Java are based on four main concepts:

1. Encapsulation: 

  • Encapsulation is the process of hiding the internal details of an object from the outside world. 
  • It is achieved by using access modifiers (such as private, protected, and public) to control access to the properties and methods of an object. 

For example, a class called "Car" can have properties like "speed" and "fuel level" that are private, meaning they can only be accessed and modified by the methods within the class.

Learn More about Encapsulation hereEncapsulation in Java

2. Inheritance: 

  • Inheritance is the process of creating a new class that is a modified version of an existing class. 
  • The new class is called the derived class or the child class, and the existing class is called the base class or the parent class.
  •  Inheritance allows a derived class to inherit properties and methods from the base class and also allows the derived class to add or override properties and methods. 

For example, a class called "SportsCar" can inherit properties and methods from a class called "Car" and also can have additional properties and methods.

Learn More About Inheritance hereInheritance in Java

3. Polymorphism: 

  • Polymorphism is the ability of an object to take on many forms. 
  • In Java, polymorphism is achieved through method overriding and method overloading. 
  • Method overriding is when a derived class provides a different implementation of a method that is already present in the base class. 
  • Method overloading is when a class has multiple methods with the same name but different parameter lists. 

For example, you can have a class called "Animal" with a method called "speak()" that is overridden in derived classes like "Dog" and "Cat" to make them produce different sounds.

Learn More about Polymorphism herePolymorphism in Java

4.  Abstraction: 

  • Abstraction is the process of hiding the complexity of an object and showing only the necessary details to the user. 
  • In Java, abstraction is achieved through abstract classes and interfaces. 
  • An abstract class is a class that cannot be instantiated and is meant to be extended by other classes. 
  • An interface is a collection of abstract methods (methods without a body) that must be implemented by any class that implements the interface. 

For example, if you want to create a class called "Shape" that will be inherited by other classes like "Circle" and "Rectangle" but you don't want to allow instantiation.

Learn More About Abstraction hereAbstraction in Java

Post a Comment

Post a Comment (0)

Previous Post Next Post