DEV Community

Cover image for Java OOP Concepts
Pramuda Liyanage
Pramuda Liyanage

Posted on

Java OOP Concepts

================Java OOP Concepts==========

What is OOP?

OOP Stands for Object-Oriented Programming.Object-Oriented Programming is paradigm that provides many concepts,such as inheritance,polymorphism etc.It simplifies software development and maintenance by providing some concepts.

What are the OOPs Concepts?

There are,

Alt Text

Java Is an Object-Oriented Programming language.Everything in Java is associated with class and objects,along with it's attributes and methods.Classes and Objects are two main aspects of object-oriented programming.

Let us consider the above oop concepts one by one..

Objects=> Any Entity that has state and behaviour is known as a object.Examples are Chair,Pen,Table etc.

Class=> Class is a Collection of objects.It is a Logical Entity.So, Class is a Definition of object,template for objects, and object is an instance of a class.

Object-Oriented Programming is a methodology or pradigm to design a program using classes and objects

Alt Text

Inheritance=> Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class.

Important terminology:
**Super Class: The class whose features are inherited is known as superclass(or a base class or a parent class).

**Sub Class: The class that inherits the other class is known as subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.

**Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

Important point:

**In the inheritance the class which is give data members and methods is known as base or super or parent class.

**The class which is taking the data members and methods is known as sub or derived or child class.

**The data members and methods of a class are known as features.
The concept of inheritance is also known as re-usability or extendable classes or sub classing or derivation.

Why use Inheritance ?

*For Method Overriding (used for Runtime Polymorphism).
*It's main uses are to enable polymorphism and to be able to reuse code for different classes by putting it in a common super class
*For code Re-usability

Real Life Example of Inheritance in java:

The real life example of inheritance is child and parents, all the properties of father are inherited by his son.

Alt Text

Alt Text

Polymorphism=> If one tasks is performed in different ways,It is known as polymorphism.Polymorphism is derived from 2 greek words: poly and morphs.The word "poly" means many and "morphs" means forms. So polymorphism means many forms.In java We used method overloading and method overriding to achieve polymorphism.

Polymorphism is not a programming concept but it is one of the principal of OOPs.For many objects oriented programming language polymorphism principle is common but whose implementations are varying from one objects oriented programming language to another object oriented programming language.

Two types of Polymorphism

01)Compile time Polymorphism(Static or Early Binding)
02)Runtime Polymorphism(Dynamic or late Binding)

Alt Text

Real Life Example of Polymorphism in java:

Suppose if you are in class room that time you behave like a student, when you are in market at that time you behave like a customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.

Alt Text

Alt Text

Abstraction=> Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context.

Hiding of data is known as data abstraction. In object oriented programming language this is implemented automatically while writing the code in the form of class and object.

Real Life Example of Abstraction in java:

Another real life example of Abstraction is ATM Machine; All are performing operations on the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't know internal details about ATM.

Alt Text

Encapsulation=> Encapsulation is a process of wrapping of data and methods in a single unit is called encapsulation. Encapsulation is achieved in java language by class concept.

A java class is the example of encapsulation. Java bean is the fully encapsulated class because all the data members are private here.

Advantage of Encapsulation:

The main advantage of using of encapsulation is to secure the data from other methods, when we make a data private then these data only use within the class, but these data not accessible outside the class.

Benefits of encapsulation:

*Provides abstraction between an object and its clients.
*Protects an object from unwanted access by clients.
*Example: A bank application forbids (restrict) a client to change an Account's balance.

Real life example of Encapsulation in Java:

The common example of encapsulation is capsule. In capsule all medicine are encapsulated in side capsule.

Alt Text

Thank You...
Pramuda Liyanage
-Fullstack Developer-

Discussion (0)