OOPS Concepts in Java with Real-time Examples

OOPS Concepts in Java with Real-time Examples

oops.jpg

As we all know Object-Oriented Programming Concepts are very important for programming. Without having an idea about OOPS concepts, you will not be able to design systems in the object-oriented programming model.

Oops(Object-oriented programming System) is a programming paradigm based on the concept of “Objects” that contain data and methods. The primary purpose of object-oriented programming is to increase the flexibility and maintainability of programs. Object oriented programming brings together data and its behavior(methods) in a single location(object) makes it easier to understand how a program works.

List of the core Oops Concepts:-

1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation

OOPs List.png

1. Object

The Object is the real-time entity having some state and behavior. In Java, Object is an instance of the class having the instance variables like the state of the object and the methods as the behavior of the object. The object of a class can be created by using the new keyword in Java Programming language.

2. Class

A class is a group of objects which have common properties. It is a template or blueprint from which objects are created. In other word, a class is the group of objects exhibiting same behavior will come under the same group. obj nd class.jpg

Java class and corresponding templates

class temp.png

3. Inheritance

Inheritance is another important concept in object-oriented programming. Inheritance in Java is a mechanism by which one class acquires the properties and behaviors of the parent class. It’s essentially creating a parent-child relationship between classes. In Java, we will use inheritance mainly for code re-usability and maintainability.

The Class from where the properties are inherit is known as Super Class

The Class to which the properties are inherited is known as Sub Class

IS-A relationship between a super class and its sub classes by using extends keyword.

A subclass inherits all the non-private members (fields, methods, and nested classes) from its super class. Constructors are not members, so they are not inherited by sub classes, but the constructor of the super class can be invoked from the subclass.

inheritance.png

Types of Inheritance

java-types-of-inheritance.jpg

Why multiple inheritance is not allowed in java?

In multiple inheritance a "Sub class" will have more than one "Super class". When we create an object of Sub class either directly or indirectly Super class constructor should be call in order to complete constructor chaining process.But since, we have more than one Super class. There is an ambiguity in calling Super class constructor .Because of this ambiguity constructor chaining process will remain incomplete,Hence Multiple inheritance is not allowed in java.

4. Polymorphism

Polymorphism is the concept where an object behaves differently in different situations. java-polymorphism.jpg There are two types of polymorphism – compile time polymorphism and runtime polymorphism. poly2.jpg

5. Abstraction

Abstraction is the concept of hiding the internal details and describing things in simple terms. Consider the below example

All are performing operations on the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't know the internal details about ATM.abstraction-realtime-example.png

6. Encapsulation

Encapsulation is the mechanism that binds the data members of the class with the methods together into a single entity.

Capsule, it is wrapped with different medicines. In a capsule, all medicine is encapsulated inside a capsule.

A Java class is an example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here. encapsulation-in-java.png A jar file is an encapsulation of byte code files.

That’s all for a quick round-up on OOPS concepts.

Summary

Java is a robust and scalable object-oriented programming language that is based on the concept of objects and classes. It offers features like inheritance, abstraction, encapsulation, and polymorphism for developing an efficient and reliable code.