Object-Oriented Programming Concepts in java

Photo by Michiel Leunens on unsplash

Object-Oriented Programming is a programming paradigm which is based on the concept of objects. Here objects contains 2 things data and code. Data can be in the form of fields. In other words fields can be called attributes or properties.

Concepts in Object-Oriented Programming

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

Objects are conceptually similar to real-world objects. They too consist of state and related behavior. An object stores it’s state in fields and exposes it’s behavior through methods. In Java we can create object using ‘new’ keyword.
Example:-

Output:-

Your room is Living Room

A class is the blueprint from which individual objects are created.
In real world too you can find many individual objects all of the same kind.
Example:-

Output:-

Your NewsPaper has 16 pages.

3. Inheritance
Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road bikes and tandem bikes all share the characteristics of bicycles( current speed, current gear).
Object-oriented programming allows classes to inherit commonly used state and behavior from other classes.In Java programming language, each class is allowed to have one direct super-class, and each super-class can have any number of sub-classes.

Example:-

Output:-

Number of gear=4

There are 3 types of inheritance in java. These are

a). Single Inheritance:
In this type of inheritance one class inherit properties of it’s parent class. For Example Car class can inherit properties of vehicle class.
Example:-

Output:-

Number of wheels=4
Vehicle run on petrol

b). Multi-level Inheritance:
In this type of inheritance parent class is having their own parent class like grand parents in real life.

Example:-

Output:-

Child
Parent
Grandparent

c). Hierarchy Inheritance:
In this type of inheritance one parent can have more than one child like we have siblings in our real life.

Example:-

Output:-

Car capacity is to carry 4 people.
It run on petrol
Tractor can lift 100 kg of sand.
It run on petrol

4. Encapsulation

The process of combining data and code together is called encapsulation.

We can use private keyword and use setter and getter to implement encapsulation in java.

Example:-

Output:-

Bhagat

5. Abstraction

Abstraction is the process to show only necessary things to user and hide implementation. Like in real life we apply brake in bike and it stop but we don’t know how internally it works.

We can implement abstraction using abstract class or interfaces in java.

Example:-

Output:-

Number of wheels in Car 4

6. Polymorphism

Polymorphism in java is a concept by which we can perform single action in different ways. Using this objects can take many forms.

In java polymorphism is of 2 types.

  1. Compile time polymorphism: is implemented using method overloading.
  2. Run time polymorphism : is implemented using method overriding.

Example:

Output:-

7
12

Conclusion

Object oriented programming helps in solving real world problems. For more detail study you can refer to Object-Oriented Programming Concepts.