0% found this document useful (0 votes)
3 views4 pages

OOPs Concepts in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

OOPs Concepts in Java

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

OOPs concepts in Java

Object-oriented programming System(OOPs) 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
behaviour(methods) in a single location(object) makes it easier to understand
how a program works.

What is an Object

Object: is a bundle of data and its behaviour(often known as methods).

Objects have two characteristics: They have states and behaviors.

Examples of states and behaviors


Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door

Example 2:
Let’s take another example.
Object: Car
State: Color, Brand, Weight, Model
Behavior: Break, Accelerate, Slow Down, Gear change.

Characteristics of Objects:
1. Abstraction
2. Encapsulation
3. Message passing

Abstraction: Abstraction is a process where you show only “relevant” data and
“hide” unnecessary details of an object from the user.
Encapsulation: Encapsulation simply means binding object state(fields) and
behaviour(methods) together. If you are creating class, you are doing
encapsulation.

Message passing
A single object by itself may not be very useful. An application contains many
objects. One object interacts with another object by invoking methods on that
object. It is also referred to as Method Invocation. See the diagram below.

What is a Class in OOPs Concepts


A class can be considered as a blueprint using which you can create as many
objects as you like. For example, here we have a class Website that has two data
members (also known as fields, instance variables and object states). This is just
a blueprint, it does not represent any website, however using this we can create
Website objects (or instances) that represents the websites.

Object Oriented Programming features


These four features are the main OOPs Concepts that you must learn to
understand the Object Oriented Programming in Java

Abstraction
Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user. For example, when you login to
your bank account online, you enter your user_id and password and press login,
what happens when you press login, how the input data sent to server, how it
gets verified is all abstracted away from the you.

Encapsulation
Encapsulation simply means binding object state(fields) and behavior(methods)
together. If you are creating class, you are doing encapsulation.

Encapsulation example in Java

How to
1) Make the instance variables private so that they cannot be accessed directly
from outside the class. You can only set and get values of these variables
through the methods of the class.
2) Have getter and setter methods in the class to set and get the values of the
fields.

Inheritance
The process by which one class acquires the properties and functionalities of
another class is called inheritance. Inheritance provides the idea of reusability of
code and each sub class defines only those features that are unique to it, rest of
the features can be inherited from the parent class.

1. Inheritance is a process of defining a new class based on an existing class


by extending its common data members and methods.
2. Inheritance allows us to reuse of code, it improves reusability in your java
application.
3. The parent class is called the base class or super class. The child class
that extends the base class is called the derived class or sub class or child
class.

Note: The biggest advantage of Inheritance is that the code in base class need
not be rewritten in the child class.
The variables and methods of the base class can be used in the child class as
well.
Types of Inheritance:
Single Inheritance: refers to a child and parent class relationship where a class
extends the another class.

Multilevel inheritance: refers to a child and parent class relationship where a


class extends the child class. For example class A extends class B and class B
extends class C.

Hierarchical inheritance: refers to a child and parent class relationship where


more than one classes extends the same class. For example, class B extends
class A and class C extends class A.

Multiple Inheritance: refers to the concept of one class extending more than
one classes, which means a child class has two parent classes. Java doesn’t
support multiple inheritance

Polymorphism

Polymorphism is a object oriented programming feature that allows us to perform


a single action in different ways. For example, lets say we have a class Animal that
has a method animalSound(), here we cannot give implementation to this method as
we do not know which Animal class would extend Animal class.

You might also like